java中HashMap的基本⽅法使⽤遍历,添加词,等等
package test;男生手链
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map.Entry;
import java.util.Set;
德音孔昭
public class test6 {
public static void main(String[] args) {
HashMap<String,Integer> map =new HashMap<>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
办公室副主任map.put("d", 4);
// Map map=new HashMap();
爆肚儿// map.put("a", "aaa");
// map.put("b", "bbb");
// map.put("c", "ccc");
// map.put("d", "ddd");
//hashMap遍历⽅法1
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
虾皮不能和什么一起吃Object key = ();
System.out.println("(key) is :"+(key));
}
//遍历⽅法2
Set<Map.Entry<String, Integer>> Set();
Iterator<Map.Entry<String, Integer>> it=t.iterator();
while(it.hasNext()){
//System.out.(0) );
张皇失措Map.Entry<String, Integer> ();
System.out.Key()+":"+e.getValue());
}
关于生活的经典语录System.out.ainsValue(1) );
System.out.ainsKey("a") );
System.out.println(map.size() );
System.out.ve("a") );
System.out.println(map.size() );
System.out.ainsKey("a") );
System.out.ainsValue(1) );
//ArrayList <Integer> list =(ArrayList<Integer>)map.values();
//上⾯会出现异常,因为map.values()返回的是⼀个Collection<Integer>对象,可⽤下⾯的⽅法解决
ArrayList <Integer> list =new ArrayList<Integer>(map.values());
Iterator <Integer>ite=list.iterator();
while(ite.hasNext()){
//System.out.(0) );
System.out.() );
}
map.clear();眼角泪沟
System.out.println(map.isEmpty() );
}
}