Map集合
Map父接口
Map接口使用
代码演示:
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Test01 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
//1.put添加
map.put("CHN","中国");
map.put("USA","美国");
map.put("UK","英国");
System.out.println("元素个数:" + map.size());
System.out.println(map.toString());
//2.删除
map.remove("USA");
System.out.println("删除后元素个数:" + map.size());
//3.遍历
System.out.println("---------keySet()方法---------");
Set<String> keyset = map.keySet();
for (String str:keyset
) {
System.out.println(str + ":" + map.get(str));
}
System.out.println("---------entrySet()方法(效率更高)---------");
Set<Map.Entry<String,String>> entries = map.entrySet();
for (Map.Entry<String,String> entry:entries
) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
//4.判断
System.out.println(map.containsKey("CHN"));
System.out.println(map.containsValue("美国"));
}
}
代码结果;
Map集合的实现类
HashMap
代码演示:
package com.ben.qe;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Test02 {
public static void main(String[] args) {
HashMap<Student,String> hashMap = new HashMap<>();
Student s1 = new Student("www",25);
Student s2 = new Student("sss",29);
Student s3 = new Student("aaa",26);
//1.添加元素
hashMap.put(s1,"信息");
hashMap.put(s2,"通信");
hashMap.put(s3,"计测");
//重写方法后不可添加重复数据 hashMap.put(new Student("www",25),"信息");
System.out.println("元素个数:" + hashMap.size());
System.out.println(hashMap.toString());
hashMap.remove(s1);
System.out.println("删除之后:" + hashMap.size());
//3.遍历
System.out.println("---------keySet方法---------");
for (Student key:hashMap.keySet()
) {
System.out.println(key.toString() + ":" + hashMap.get(key));
}
System.out.println("---------entrySet方法---------");
Set<Map.Entry<Student,String>> entries = hashMap.entrySet();
for (Map.Entry<Student,String> entry:entries
) {
System.out.println(entry.toString());
}
//4.判断
System.out.println(hashMap.containsKey(s2));
System.out.println(hashMap.containsValue("信息"));
}
}
结果:
HashMap总结
Hashtable
TreeMap
代码演示:
package com.ben.qe;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class Test03 {
public static void main(String[] args) {
TreeMap<Student,String> treeMap = new TreeMap<>();
Student s1 = new Student("www",25);
Student s2 = new Student("sss",26);
Student s3 = new Student("aaa",23);
//1.添加
treeMap.put(s1,"信息");
treeMap.put(s2,"通信");
treeMap.put(s3,"计算");
System.out.println("元素个数:" + treeMap.size());
System.out.println(treeMap.toString());
//2.删除
treeMap.remove(s1);
System.out.println("删除后元素:" + treeMap.size());
//3.遍历
System.out.println("--------keySet方法----------");
for (Student key:treeMap.keySet()
) {
System.out.println(key + ":" + treeMap.get(key));
}
System.out.println("--------entrySet方法----------");
Set<Map.Entry<Student,String>> entries = treeMap.entrySet();
for (Map.Entry<Student,String> entry:entries
) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
//4.判断
System.out.println(treeMap.containsKey(s2));
System.out.println(treeMap.containsValue("信息"));
}
}
结果:
Collections工具类
代码演示:
package com.ben.qe;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Test04 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(15);
list.add(85);
list.add(6);
list.add(20);
list.add(520);
//1.sort排序
System.out.println("排序前:" + list.toString());
Collections.sort(list);
System.out.println("排序后:" + list.toString());
//2.二分查找binarySearch
int i = Collections.binarySearch(list,520);
System.out.println(i);
//3.复制copy
List<Integer> dest = new ArrayList<>();
for (int j = 0; j < list.size(); j++) {
dest.add(0);
}
Collections.copy(dest,list);
System.out.println(dest.toString());
//4.reverse反转
Collections.reverse(list);
System.out.println("反转之后:" + list.toString());
//5.shuffle打乱
Collections.shuffle(list);
System.out.println("打乱之后:" + list.toString());
//6.集合转为数组
System.out.println("-------集合转为数组--------");
Integer[] arr = list.toArray(new Integer[10]);
System.out.println(arr.length);
System.out.println(Arrays.toString(arr));
//7.数组转集合
System.out.println("-------数组转为集合--------");
String[] name = {"www","sss","ddd"};
//集合是受限集合,不能添加和删除
List<String> list1 = Arrays.asList(name);
System.out.println(list1);
System.out.println("----把基本类型数组转为集合时需要修改包装类---");
Integer[] nums = {1,2,3,5,4};
List<Integer> list2 = Arrays.asList(nums);
System.out.println(list2);
}
}
结果: