Collections
是一个工具类
演示:
public void test(){
ArrayList<String> aList1 = new ArrayList<>();
ArrayList<String> aList2 = new ArrayList<>();
aList1.add("a");
aList1.add("c");
aList1.add("b");
aList1.add("d");
aList2.add("b2");
aList2.add("d2");
aList2.add("a2");
aList2.add("c2");
/*
*参数列表中的符号...
* 例如,addAll(T... e);——>指该方法可以传入T类型的参数[0-无穷)
*/
Collections.addAll(aList1,"e");
Collections.addAll(aList2,"x2","y2");
Collections.addAll(aList1);
System.out.println("1: " + aList1);
System.out.println("2: " + aList2);
//将集合排序
Collections.sort(aList1);
System.out.println("1: " + aList1);
/*
*binarySearch 使用该方法对集合进行元素查找时,要对集合先进行升序排列,
* 否则查找到的元素不确定
*/
System.out.println("1-》b: " + Collections.binarySearch(aList1,"b"));
//集合拷贝
/*
*copy(目标集合,原集合)
* 目标集合size() >= 原集合size()
*/
Collections.copy(aList2,aList1);
System.out.println("1: " + aList1);
System.out.println("2: " + aList2);
//最值
System.out.println("1max: " + Collections.max(aList1));
System.out.println("1min: " + Collections.min(aList1));
//反转
Collections.reverse(aList1);
System.out.println("1反转: " + aList1);
}
结果:
1: [a, c, b, d, e] 2: [b2, d2, a2, c2, x2, y2] 1: [a, b, c, d, e] 1-》b: 1 1: [a, b, c, d, e] 2: [a, b, c, d, e, y2] 1max: e 1min: a 1反转: [e, d, c, b, a] |
Map<K,V>
- 接口
- Collection是单列集合
- Map<K,V>(键,值)是双列集合:存储数据(不定长,引用数据类型)
- Map存储时,存储一对键值 ——》 键值对
- 键:key;值:value
- 存储时,键和值得类型可以不一致
- 将键映射到值,一个映射不能包含重复的键,每个键只能映射一个值
解释:
Map集合只能通过键找值,没有通过值找键
Map集合不能存储重复的键,但值可以重复
键和值是一一对应的关系
HashMap
- 是Map的实现类
- 是双列集合
- 键不允许重复
- 值可以重复
- 允许存储null值、null键
- 不同步,不保证线程安全
- HashMap底层是哈希表,不保证迭代顺序 ——》 无序
应用场景:凡是需要同时存储两个数据 或者 数据之间有映射关系的都可以考虑
HashMap与Hashtable:
大致相同;
HashMap不同步,不保证线程安全;Hashtable同步,保证线程安全
HashMap允许存储null值、null键;Hashtable不允许存储null值、null键
HashMap的去重原理:
与HashSet一致。准确的说,HashSet的去重原理就是依靠HashM实现的,二者原理一样,即想要做到键去重,那么键的数据类型就要做到重写hashCode()、equals().。
演示(1):
public void test1() {
HashMap<Integer,String> hashMap = new HashMap<>();
//存储,返回旧值
String oldV = hashMap.put(2,"two");
String oldV2 = hashMap.put(2,"one");
System.out.println(oldV + "\n" + oldV2 + "\n" + hashMap);
}
结果:
null two {2=one} |
演示(2):
public void test2() {
HashMap<Integer,String> hashMap = new HashMap<>();
hashMap.put(2,"二");
hashMap.put(3,"三");
hashMap.put(1,"一");
hashMap.put(2,"二");
hashMap.put(4,"四");
hashMap.put(1,"一");
System.out.println(hashMap);
System.out.println("大小:" + hashMap.size());
hashMap.remove(2);
System.out.println("移除2后:" + hashMap);
System.out.println("键为4,值为:" + hashMap.get(4));
HashMap<Integer,String> hashMap2 = new HashMap<>();
hashMap2.putAll(hashMap);
System.out.println("把HashMap存入HashMap2后:\n"
+ hashMap2);
System.out.println("hashMap2是否为空:" + hashMap.isEmpty());
hashMap2.clear();
System.out.println("hashMap2清空后,是否为空:" + hashMap.isEmpty());
}
结果:
{1=一, 2=二, 3=三, 4=四} 大小:4 移除2后:{1=一, 3=三, 4=四} 键为4,值为:四 把HashMap存入HashMap2后: {1=一, 3=三, 4=四} hashMap2是否为空:false hashMap2清空后,是否为空:false |
三种视图遍历方式:
演示(1):
public void test3(){
/*
*keySet()会返回一个Set集合,其中包含全部的键
* Map的遍历(1):得到键集,遍历键集,再通过键集找到值
*/
HashMap<Integer,String> hashMap = new HashMap<>();
hashMap.put(2,"二");
hashMap.put(3,"三");
hashMap.put(4,"四");
hashMap.put(1,"一");
Set<Integer> keys = hashMap.keySet();
System.out.println("迭代器:");
Iterator<Integer> iterator = keys.iterator();
while (iterator.hasNext()) {
Integer key = iterator.next();
System.out.println(key + "=" + hashMap.get(key));
}
System.out.println("增强for循环:");
for (Integer key : keys) {
System.out.println(key + "=" + hashMap.get(key));
}
}
结果:
迭代器: 1=一 2=二 3=三 4=四 增强for循环: 1=一 2=二 3=三 4=四 |
演示(2):
public void test4() {
/*
*Map遍历(2):得到值集,遍历值集
*/
HashMap<Integer,String> hashMap = new HashMap<>();
hashMap.put(2,"二");
hashMap.put(3,"三");
hashMap.put(4,"四");
hashMap.put(1,"一");
Collection<String> values = hashMap.values();
for (String value : values) {
System.out.print(value + " , ");
}
}
结果:
一 , 二 , 三 , 四 , |
演示(3):
public void test5() {
/*
*Map.Entry 代表键值对这一整体
* entrySet()返回Set集合,存储键值对象
*/
HashMap<Integer,String> hashMap = new HashMap<>();
hashMap.put(2,"二");
hashMap.put(3,"三");
hashMap.put(4,"四");
hashMap.put(1,"一");
Set<Map.Entry<Integer,String>> entries = hashMap.entrySet();
for (Map.Entry<Integer,String> entry : entries) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
}
结果:
1=一 2=二 3=三 4=四 |
LinkedHashMap
- 按照插入顺序进行迭代排序
- 方法的使用与HashMap的演示基本相同
TreeMap
- 是Map的实现类
- 键不允许重复,值允许重复
- 基于红黑树实现
- 对存入元素根据其键的自然顺序排序
演示:
三种视图遍历
参考HashMap
去重排序原理
参考TreeSet,因为TreeSet去重排序就是基于TreeMap实现的
ConcurrentHashMap比HashMap安全,比Hashtable效率高