练习使用HashMap
来源:互联网 发布:纪梵希散粉 知乎 编辑:程序博客网 时间:2024/05/16 17:03
import java.util.*;
public class TestHashmap{
public static void main(String[] args)
{
MapTest map=new MapTest();
map.printMap();
}
}
class MapTest{
HashMap hash=new HashMap();
public void printMap()
{
hash.put("1","value2");
hash.put("2",null);
hash.put("3","value3");
hash.put("4","value4");
String value=(String)hash.get("3");
System.out.println("key 3<===>"+value);
Set set=hash.keySet();
System.out.println("set size="+set.size());
Iterator it=set.iterator();
while(it.hasNext())
{
String temp2=(String)it.next();
System.out.println("keys contained:"+temp2);
}
Collection valuesList=hash.values();//no exception will be reported if collection
//has null values; instead, the null value will be printed as "null" if iterated.
System.out.println("valuesList size="+valuesList.size());
Iterator it2=valuesList.iterator();
while(it2.hasNext())
{
String temp2=(String)it2.next();
System.out.println("values contained:"+temp2);
}
List valueList=new ArrayList(hash.values());
for(int i=0;i<valueList.size();i++)
{
String temp=(String)valueList.get(i);
System.out.println("value"+i+"="+temp); //the sequence is not sure
//can not make sure that the values are displayed according to the input order
}
}
}
public class TestHashmap{
public static void main(String[] args)
{
MapTest map=new MapTest();
map.printMap();
}
}
class MapTest{
HashMap hash=new HashMap();
public void printMap()
{
hash.put("1","value2");
hash.put("2",null);
hash.put("3","value3");
hash.put("4","value4");
String value=(String)hash.get("3");
System.out.println("key 3<===>"+value);
Set set=hash.keySet();
System.out.println("set size="+set.size());
Iterator it=set.iterator();
while(it.hasNext())
{
String temp2=(String)it.next();
System.out.println("keys contained:"+temp2);
}
Collection valuesList=hash.values();//no exception will be reported if collection
//has null values; instead, the null value will be printed as "null" if iterated.
System.out.println("valuesList size="+valuesList.size());
Iterator it2=valuesList.iterator();
while(it2.hasNext())
{
String temp2=(String)it2.next();
System.out.println("values contained:"+temp2);
}
List valueList=new ArrayList(hash.values());
for(int i=0;i<valueList.size();i++)
{
String temp=(String)valueList.get(i);
System.out.println("value"+i+"="+temp); //the sequence is not sure
//can not make sure that the values are displayed according to the input order
}
}
}
- 练习使用HashMap
- Java中HashMap和HashSet的使用练习
- Java 小练习 HashMap
- 练习——HashMap
- HashMap使用
- 使用HashMap
- HashMap使用
- HashMap使用
- hashmap 使用
- Map接口和HashMap练习
- HashMap练习(存取对象)
- java Map-HashMap、TreeMap练习
- 黑马程序员——HashMap综合练习
- 集合框架——HashMap练习
- HashMap 使用小结
- HashMap使用的注意事项
- HashMap 使用小结
- HashMap 使用小结
- 让gtk+应用程序的主题风格即时生效
- 与关键字的高亮显示相关
- 英语谚语精选(English Proverb Collection)
- 汉字输入教学系统中词组切分方法的设计
- 有趣的String类
- 练习使用HashMap
- Linux中的Ramdisk与Initrd
- 根据性格选择运动
- 练习使用工具类Collections
- 细说Java之util类
- a byte of python初学笔记 001
- 集合
- 长沙下雪了~
- 请问,如何将得到的字符串进行分段处理