1.Map集合
无序、以键值对的形式添加元素,键不能重复,值可以重复,它没有继承Collection接口。
2.Map集合的特点
2.1:无序
元素添加的顺序与输出不一致
Map<String,Object> map = new HashMap<>();
map.put("name","张三");
map.put("age",18);
map.put("score",100);
System.out.println(map);
//输出结果
//{score=100, name=张三, age=18}
2.2:以键值对方式存储数据
Map<String,String> map=new HashMap<String,String>();
map.put("zs", "123");
map.put("ls", "456");
map.put("ww", "789");
2.3:键唯一,值不唯一
当键相同时,值被覆盖了
Map<String,Object> map = new HashMap<>();
map.put("name","张三");
map.put("age",18);
map.put("score",100);
map.put("age",22);
键不相同时,值添加成功
Map<String,Object> map = new HashMap<>();
map.put("name","张三");
map.put("age",18);
map.put("score",100);
map.put("txj","张三");
3.遍历方式
3.1:获取所有的Key
Map<String,String> map=new HashMap<String,String>();
map.put("zs", "123");
map.put("ls", "456");
map.put("ww", "789");
//获取Map集合中所有的键
Set<String> keys = map.keySet();
for (String key : keys) {
System.out.println(key);
}
3.2:获取所有的value
Map<String,String> map=new HashMap<String,String>();
map.put("zs", "123");
map.put("ls", "456");
map.put("ww", "789");
//获取Map集合中所有的键
Set<String> keys = map.keySet();
for (String key : keys) {
System.out.println(key);
}
3.3:获取所有的键值对
System.out.println("-----获取所有的键值对entrySet------");
Map<String,Object> map = new HashMap<>();
map.put("name","张三");
map.put("age",18);
map.put("score",100);
Set<Map.Entry<String,Object>> entries = map.entrySet();
entries.forEach(a->{
System.out.println(a.getKey()+"="+a.getValue());
});
4.HashMap与Hashtable的区别
4.1HashMap是Hashtable的轻量级实现(非线程安全的实现),他们都完成了Map接口,主要区别在于HashMap允许空(null)键值(key),由于非线程安全,在只有一个线程访问的情况下,效率要高于Hashtable。
//HashMap 可以输出null值
// Map<String,Object> map = new HashMap<>();
Map<String,Object> map = new Hashtable<>();
Set<String> keys = map.keySet();
map.put("name","张三");
map.put("age",18);
map.put("score",100);
map.put(null,null);
System.out.println(map);
//Hashtable 不能输出null值
Map<String,Object> map = new Hashtable<>();
Map<String,Object> map = new Hashtable<>();
Set<String> keys = map.keySet();
map.put("name","张三");
map.put("age",18);
map.put("score",100);
map.put(null,null);
System.out.println(map);
4.2HashMap允许将null作为一个entry的key或者value,而Hashtable不允许。、
4.3HashMap把Hashtable的contains方法去掉了,改成containsvalue和containsKey。因为contains方法容易让人引起误解。
containsKey:查询集合里是否包含所要的key 有返回true 无则返回false(个人理解)
Map<String,Object> map = new Hashtable<>();
Set<String> keys = map.keySet();
map.put("name","张三");
map.put("age",18);
map.put("score",100);
map.put(null,null);
System.out.println(map);
map.put("txj","张三");
//containsKey 查询集合里是否包含所要的key 有返回true 无则返回false
System.out.println(map.containsKey("txj"));
containsvalue:查询集合里是否包含所要的Value 有返回true 无则返回false(个人理解)
Map<String,Object> map = new Hashtable<>();
Set<String> keys = map.keySet();
map.put("name","张三");
map.put("age",18);
map.put("score",100);
map.put(null,null);
System.out.println(map);
map.put("txj","张三");
//containsValue 查询集合里是否包含所要的Value 有返回true 无则返回false
System.out.println(map.containsValue(18));
4.4Hashtable继承自Dictionary类,而HashMap是Java1.2引进的Map interface的一个实现
最大的不同是,Hashtable的方法是Synchronize的,而HashMap不是,在多个线程访问Hashtable时,不需要自己为它的方法实现同步,而HashMap 就必须为之提供外同步。 Hashtable和HashMap采用的hash/rehash算法都大概一样,所以性能不会有很大的差异。
就HashMap与HashTable主要从三方面来说。
-
历史原因:Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现;
-
同步性:Hashtable是线程安全的,也就是说是同步的,而HashMap是线程序不安全的,不是同步的;
-
值:只有HashMap可以让你将空值作为一个表的条目的key或value
5.TreeMap
5.1:默认升序排序
Map<String,String> map = new TreeMap<>();
map.put("txj","100");
map.put("wyy","90");
map.put("xy","120");
map.put("txy","150");
System.out.println(map);
5.2:reverseOrder降序排序
Map<String,Object> map=new TreeMap<>(Comparator.reverseOrder());
map.put("2","ls");
map.put("1","ww");
map.put("4","zs");
map.put("3","xl");
map.forEach((k,v)->{
System.out.println(k+"="+v);
});
5.3:Value值排序
Map<String,Object> map=new TreeMap<String,Object>();
map.put("2","ls");
map.put("1","ww");
map.put("4","zs");
map.put("3","xl");
//按照map中的value属性排序
List<Map.Entry<String,Object>> lst=
new ArrayList<Map.Entry<String,Object>>(map.entrySet());
Collections.sort(lst, new Comparator<Map.Entry<String, Object>>() {
@Override
public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
return -o1.getValue().toString().compareTo(o2.getValue().toString());
}
});
lst.forEach(s->{
System.out.println(s);
});
6.其他工具类
6.1:Collections:工具类,提供一组静态方法操作Collection集合
6.2:Array:工具类,提供了一组静态方法操作数组