1,HashMap
key是整型时会默认排序,而且是升序;
key不是整型时,默认不排序,并且遍历取值时是随机的顺序,不是原先放入的顺序。
2,LinkedHashMap
key是整型或者不是整型都不排序,LinkedHashMap保证按放入顺序遍历。
3,TreeMap
key是整型或者不是整型都排序,LinkedHashMap默认会排序。
遍历
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
*@2017年8月28日@上午9:46:45
*@Endless
*@Map.java
*/
public class Maps {
public static void main(String[] args) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
//遍历map中的键
map.put(1, 233);
map.put(2, 333);
map.put(3, 666);
map.forEach((k, v)->System.out.println("Key = " +k));//key遍历秒
/
long t1=System.currentTimeMillis();
map.keySet().forEach(k->System.out.println("Key = " +k));//key遍历
map.keySet().iterator().forEachRemaining(k->System.out.println("Key = " +k));//key遍历
map.forEach((k, v)->System.out.println("Value = "+v));//value遍历
map.values().forEach(v->System.out.println("Value = "+v));//value遍历
map.values().iterator().forEachRemaining(v->System.out.println("Value = "+v));//value遍历
map.forEach((k, v)->System.out.println("Key = " + k + ", Value = " +v));//key和value遍历
map.entrySet().forEach(m->System.out.println("Key = "+m.getKey()+" , Value = "+m.getValue()));//key和value遍历
map.entrySet().iterator().forEachRemaining((m)->System.out.println("K="+m.getKey()+" ,V="+m.getValue()));//key和value遍历
long t2=System.currentTimeMillis();//
System.out.println("用时:"+(t2-t1));
System.out.println(map.keySet());//key的集合
System.out.println(map.values());//value的集合
Iterator<Integer> keyite=map.keySet().iterator();//key集合的迭代器
Iterator<Integer> valite=map.values().iterator();//value集合的迭代器
Iterator<Entry<Integer, Integer> > kvlite=map.entrySet().iterator();//map的集合的迭代器
keyite.forEachRemaining(k->System.out.println(k));//key遍历
valite.forEachRemaining(v->System.out.println(v));//value遍历
long t3=System.currentTimeMillis();
while(keyite.hasNext()){
System.out.println(keyite.next());//key遍历
}
while(valite.hasNext()){
System.out.println(valite.next());//value遍历
}
while(kvlite.hasNext()){
System.out.println();
// System.out.println(kvlite.next().getKey());//key遍历
// System.out.println(kvlite.next().getValue());//value 遍历
System.out.println(kvlite.next());//key+value//遍历
}
for (Integer key : map.keySet()) {
System.out.println("Key = " + key); //key遍历
//System.out.println(map.get(key));//value遍历
}
for(Integer value:map.values()){
System.out.println("Value = "+value);//value遍历
//System.out.println(map.containsValue(value));//value是否存在
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); //key和value遍历
}
Set<Entry<Integer, Integer>> s = map.entrySet();
Iterator<Entry<Integer, Integer>> it = s.iterator();
Entry<Integer, Integer> entry;
int Key;
Integer value;
while (it.hasNext()) {
entry = it.next();
//Key=s.iterator().next().getKey();
Key = entry.getKey();
value = entry.getValue();
System.out.println(Key + ":\t" + value);
}
long t4=System.currentTimeMillis();
System.out.println("用时:"+(t4-t3));
}
}
其他
System.out.println(map.getOrDefault(2, null));//等同于get();
Map<Integer, Integer> mmmm = new HashMap<Integer, Integer>();
mmmm.put(0, 999);
mmmm.putAll(map);//复制map追加到mmmm
System.out.println(mmmm);
Map< String,Integer>map =new LinkedHashMap<String,Integer>();
map.put("洪荒时代", 1);
map.put("部落时代", 3);
map.put("石器时代", 2);
map.put("帝国时代", 4);
TreeMap<Integer,String>mm=new TreeMap<Integer, String>();
map.forEach((x,y)->mm.put(y, x) //key value交换
forEach((x,y)->System.out.println(x+y));
排序
Collections.sort(new ArrayList<>(map.entrySet()),new Comparator<Map.Entry<String,Integer>>(){
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return (o1.getValue()-o2.getValue());
}
}); System.out.println(map);
Collections.sort(new ArrayList<>(map.entrySet()),(x,y)->y.getValue().compareTo(x.getValue()));
Key不可重复 ,否则覆盖Value
Map< String,Integer>maps =new HashMap<String,Integer>();
for(char x:"abcabc".toCharArray()){
Integer num = (Integer) maps.get(x+"");
if (num == null){
num = 1;
}else{
num = num + 1;
}maps.put(x+"", num);//覆盖
}
System.out.println(maps);