import java.util.*;
public class mapsComparison {
public static void main(String[] args) {
//没有顺序
Map<Integer,String> map=new HashMap<>();
map.put(1,"java1班");
map.put(2,"java2班");
map.put(4,"java4班");
map.put(3,"java3班");
System.out.println("hashmap的输出结果"+map.toString());
//按添加顺序
Map<Integer,String> map2=new LinkedHashMap<>();
map2.put(1,"java1班");
map2.put(2,"java2班");
map2.put(4,"java4班");
map2.put(3,"java3班");
System.out.println("linkedhashmap的输出结果"+map2.toString());
//二叉树顺序,左小右大
Map<String,String> map3=new TreeMap<>();
map3.put("one","java1班");
map3.put("two","java2班");
map3.put("4","java4班");
map3.put("3","java3班");
System.out.println("treemap的输出结果"+map3.toString());
//获取所有的key得到一个set
Set<String> strings = map3.keySet();
System.out.println(strings);
//通过遍历key来得到所有的value
for(String s:strings){
System.out.println(map3.get(s));
}
}
}
打印的结果为:
hashmap的输出结果{1=java1班, 2=java2班, 3=java3班, 4=java4班} linkedhashmap的输出结果{1=java1班, 2=java2班, 4=java4班, 3=java3班} treemap的输出结果{3=java3班, 4=java4班, one=java1班, two=java2班} keyset得到的结果为:[3, 4, one, two] java3班 java4班 java1班 java2班
|