Map集合
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fabcb07187e808de94cde5f8581d1321.png)
- Map接口的特点
- 用于存储任意键值对(key-value)
- 键:无序,无下标,不允许重复
- 值:无序,无下标,允许重复
- Map接口的常用方法
- V get(Object key): 返回 Map 集合中指定键对象所对应的值。V 表示值的数据类型
- V put(K key, V value): 向 Map 集合中添加键-值对,返回 key 以前对应的 value,如果没有, 则返回 null
- V remove(Object key): 从 Map 集合中删除 key 对应的键-值对,返回 key 对应的 value,如果没有,则返回null
- Set entrySet(K,V): 返回 Map 集合中所有键-值对的 Set 集合,此 Set 集合中元素的数据类型为 Map.Entry
- Set keySet(K): 返回 Map 集合中所有键对象的 Set 集合
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("cn","china");
map.put("cn","中国");
map.put("en","english");
map.put("us","american");
System.out.println(map.size());
System.out.println(map);
//遍历
//Set<String> set = map.keySet();
for (String s : map.keySet()) {
System.out.println(s+":"+map.get(s));
}
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
System.out.println(entry);
//System.out.println(entry.getKey()+":"+entry.getValue());
}
Map集合的实现类
- HashMap:
- 线程不安全,运行效率快;允许用null作为key或value
- 存储结构:哈希表
- HashMap() :
构造一个空的 HashMap ,默认初始容量(16)和默认负载系数(0.75)
public static void main(String[] args) {
HashMap<Student, String> sh = new HashMap<>();
Student s1 = new Student("sun", 111);
Student s2 = new Student("qian", 222);
Student s3 = new Student("wang", 333);
sh.put(s1,"beijing");
sh.put(s2,"shanghai");
sh.put(s3,"beijing");
System.out.println(sh);
for (Student student : sh.keySet()) {
System.out.println(student+":"+sh.get(student));
}
for (Map.Entry<Student, String> stu : sh.entrySet()) {
//System.out.println(stu);
System.out.println(stu.getKey()+"地址:"+stu.getValue());
}
System.out.println(sh.containsKey(s1));
System.out.println(sh.containsValue("nanjing"));
System.out.println(sh.isEmpty());
}
- 总结
- HashMap刚创建时,table是null,为了节省空间,当添加第一个元素时,table调整为16
- 当元素个数大于阈值(16*0.75)时,会进行扩容,扩容后大小时原来的2倍,目的是减少调整元素的个数
- jdk1.8 当每个链表长度大于8,并且数组元素个数大于等于64时,会调整为红黑树,目的是提高执行效率
- jdk1.8 当链表长度小于6时,会调整成链表
- jdk1.8以前,链表头插入,jdk1.8以后是尾插入
- HashTable(不常使用):
- JDK1.0版本,线程安全,运行效率慢;不允许null作为key或value
- Properties:继承了HashTable
- Hashtable的子类,要求key和value都是String。通常用于配置文件的读取
- TreeMap
- 存储结构:红黑树
- 实现了SortedMap接口(是Map的子接口),可以对key自动排序
public static void main(String[] args) {
TreeMap<Student, String> tm = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int n1 = o1.getName().compareTo(o2.getName());
int n2 = o1.getId()-o2.getId();
return n1==0?n2:n1;
}
});
Student s1 = new Student("sun", 111);
Student s2 = new Student("qian", 222);
Student s3 = new Student("wang", 333);
tm.put(s1,"北京");
tm.put(s2,"上海");
tm.put(s3,"南京");
tm.put(new Student("wang", 333),"深圳");
System.out.println(tm.size());
System.out.println(tm);
// tm.remove(s3);
// System.out.println(tm.size());
// System.out.println(tm);
//遍历
for (Student key : tm.keySet()) {
System.out.println(key+":"+tm.get(key));
}
for (Map.Entry<Student, String> en : tm.entrySet()) {
System.out.println(en);
}
Collections工具类
- 概念:集合工具类,定义了除了存取以外的集合常用方法
- 方法:
- reverse(List<?> list):反转集合中元素的顺序
- shuffle(List<?> list):随机重置集合元素的顺序
- sort(List list):升序排序(元素类型必须实现Comparable接口)
public static void main(String[] args) {
ArrayList<Integer> in = new ArrayList<>();
in.add(20);
in.add(2);
in.add(56);
in.add(78);
in.add(17);
Collections.sort(in);
System.out.println(in);
//binarySEarch二分查找
System.out.println(Collections.binarySearch(in, 56));
//copy复制,要保证两个数组大小一样
ArrayList<Integer> in2 = new ArrayList<>();
for (int i = 0; i < in.size(); i++) {
in2.add(0);
}
Collections.copy(in2,in);
System.out.println(in2);
//reverse反转
Collections.reverse(in2);
System.out.println(in2);
//shuffle随机打乱
Collections.shuffle(in2);
System.out.println(in2);
}
//list转数组
Integer[] arr = in.toArray(new Integer[0]);
System.out.println(arr.length);
System.out.println(Arrays.toString(arr));
//数组转集合
//此集合受限,不能增删
List<Integer> list = Arrays.asList(arr);
System.out.println(list);
//基本数据类型转集合,需要修改为包装类型
Integer[] nums = {100,23,45,67};
List<Integer> list1 = Arrays.asList(nums);
System.out.println(list1);