HashMap
1.HashMap基本使用
package com.lu.day08.map;import java.util.HashMap;
import java.util.Map;
import java.util.Set;public class MapDome {public static void main(String[] args) {HashMap<String , String> map = new HashMap<>();//添加后者修改->如果key不存在则是添加,如果key已经存在就是修改map.put("曹雪芹","红楼梦");map.put("施耐庵","水浒传");map.put("吴承恩","西游记");map.put("杜甫","登高");System.out.println(map);//通过key获取值,如果key不存在则,获取到的值为nullSystem.out.println(map.get("吴承恩"));System.out.println(map.get("李白"));map.remove("吴承恩");System.out.println("--------------");map.put("杜甫","琵琶行");System.out.println(map);HashMap<Integer, String> map1 = new HashMap<>();map1.put(1,"one");map1.put(2,"two");map1.put(3,"three");map1.put(4,"four");System.out.println(map1.containsKey(7));System.out.println(map1.containsValue("one"));System.out.println(map1.isEmpty());System.out.println(map.size());//获取键集合Set<String> strings = map.keySet();for (String key : strings) {System.out.println(key+"-->"+map.get(key));}System.out.println("-----------------");//entrySet 返回一个存放键值对set集合//entry是一个map接口中的内部类->通过put(k,v)放置的键值对,实际上存到了entry对象中->对象由HashMap中的内部类中的node实现//实现其中有两个属性(key,value)Set<Map.Entry<String, String>> entries = map.entrySet();for (Map.Entry<String, String> entry : entries) {System.out.println(entry.getKey()+"-->"+entry.getValue());}System.out.println("-----------------");//函数式的遍历方法forEachmap.forEach((k,v)->{System.out.println(k+"-->"+v);});//只能key取value不能反过来}}
TreeMap基本使用
1.TreeMap基本使用
package com.lu.day08.map;import java.util.Comparator;
import java.util.TreeMap;public class TreeMapDemo {public static void main(String[] args) {//TreeMap只能对key排序Comparator<Integer> c = (o1,o2)->o2-o1;TreeMap<Integer, String> map = new TreeMap<>(c);map.put(1, "a");map.put(2, "b");map.put(3, "c");System.out.println(map);}
}
LinkedHashMap
package com.lu.day08.map;import java.util.LinkedHashMap;
import java.util.LinkedHashSet;public class LinkedHashMapDemo {public static void main(String[] args) {//保证存取有序LinkedHashMap<Integer, String> map = new LinkedHashMap<>();map.put(1, "张三");map.put(2, "李四");map.put(3, "王五");map.put(4, "赵六");map.forEach((k, v) -> {System.out.println(k + ":" + v);});LinkedHashSet<Integer> set = new LinkedHashSet<>();set.add(1);set.add(2);set.add(3);set.forEach(System.out::println);}
}
集合嵌套
package com.lu.day08.map;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class Test {public static void main(String[] args) {HashMap<String, List<String>> map = new HashMap<>();ArrayList<String> xiaomi = new ArrayList<>();xiaomi.add("小米1");xiaomi.add("小米2");xiaomi.add("小米3");map.put("小米", xiaomi);ArrayList<Map<String,Double>> list = new ArrayList<>();HashMap<String, Double> clothMap = new HashMap<>();clothMap.put("耐克",2300.0);clothMap.put("贵人鸟",7800.0);clothMap.put("凡客",50.0);}
}
Collections
1.集合辅助工具类
package com.lu.day08.collections;import java.util.ArrayList;
import java.util.Collections;public class CollectionsDemo {public static void main(String[] args) {ArrayList<Integer> list = new ArrayList<>();list.add(1);list.add(5);list.add(2);list.add(4);list.add(3);System.out.println(list);// 反转Collections.reverse(list);System.out.println(list);// 排序Collections.sort(list);System.out.println(list);// 随机Collections.shuffle(list);System.out.println(list);}
}
可变参数
package com.lu.day08.collections;/*** 可变参数*/
public class VaryParam {public static void main(String[] args) {print(1,2,3,4,5,6,7,8,9);}public static void print(int... a){for (int j : a) {System.out.println(j);}}
}
不可变集合
package com.lu.day08.collections;import java.util.List;
import java.util.Map;
import java.util.Set;/*** jdk9新增不可变集合*/
public class Test {public static void main(String[] args) {//创建list不可变集合List<Integer> integers = List.of(1, 2, 3);integers.forEach(System.out::println);//创建set不可变集合Set<Integer> integers1 = Set.of(1, 2, 3);System.out.println(integers1);//创建map不可变集合Map<String, Integer> a = Map.of("A", 1, "B", 2, "C", 3);System.out.println(a);}
}