一、HashMap
- Map集合存储键值对 数据对象,一个键只能对应一个值。键相同时,旧值会被覆盖。
Map集合称作为双列集合。
public static void main(String[] args) {
//所有Map集合的数据结构,只跟键有关,跟值没关系,一个键只能对应一个值。键相同,值覆盖
HashMap<String, String> map = new HashMap<>();
//当我们第一次存储键值对 数据时,返回的是null
String str1 = map.put("文总", "马莉");
//当我们再次存储同一个键相同的数据时,会覆盖掉旧值。但返回的是这个键所对应的旧值
String str2 = map.put("文总", "姚笛");
map.put("贾亮","李璐");
map.put("陈成","佟娅");
System.out.println(str1);
System.out.println(str2);
//键相同,值覆盖
System.out.println(map);
}
- Map集合的数据结构,只跟键有关,跟值没关系
public static void main(String[] args) {
HashMap<Student, String> hashMap = new HashMap<>();
hashMap.put(new Student("张三",23),"s001");
hashMap.put(new Student("张三",23),"s002");
hashMap.put(new Student("李四",23),"s003");
hashMap.put(new Student("王五",23),"s004");
System.out.println(hashMap);
}
- Map集合的功能概述
HashMap<String, String> map = new HashMap<>();
map.put("文总", "马琍");
map.put("贾亮", "李璐");
map.put("陈成", "佟娅");
//删除某一对
String s = map.remove("文总");//根据键,删除键值对。返回的是,键所对应的那个值
System.out.println(s);
map.clear();//清空集合中的元素
boolean b = map.isEmpty();//判断集合是否为空
int size = map.size();//获取集合的长度
boolean b1 = map.containsKey("贾亮");//判断有没有这个键
boolean b2 = map.containsValue("佟娅");//判断有没有这个值
String s = map.get("文总");//键找值
- 遍历Map集合
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("文总", "马琍");
map.put("贾亮", "李璐");
map.put("陈成", "佟娅");
//方式一:获取所有键的集合
Set<String> keySet = map.keySet();
//遍历键值,键找值
for (String key: keySet) {
System.out.println(key+"==="+map.get(key));
}
//方式二:获取出,键值对 对象的集合
Set<Map.Entry<String, String>> entries = map.entrySet();
//遍历键值对 集合,通过键值对 对象,里面的方法取出键和值
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"==="+value);
}
}
- HashMap允许存储null键和null值
public static void main(String[] args) {
HashMap<String, Integer> hashMap = new HashMap<>();
//HashMap键的数据结构是哈希表,键唯一,无序
//HashMap允许存储null键和null值,线程不安全,效率高
hashMap.put("abc",100);
hashMap.put(null,200);
hashMap.put("bbb",null);
hashMap.put(null,null);
Set<String> keySet = hashMap.keySet();
for (String key : keySet) {
System.out.println(key+"==="+hashMap.get(key));
}
System.out.println("..............");
Set<Map.Entry<String, Integer>> entrySet = hashMap.entrySet();
for (Map.Entry<String, Integer> entry : entrySet) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+"==="+value);
}
}
二、LinkedHashMap
键唯一且有序,链表保证了键有序,哈希表保证了键唯一
public static void main(String[] args) {
//LinkedHashMap键唯一且有序,链表保证了键有序,哈希表保证了键唯一
LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put(10,"abc");
linkedHashMap.put(20, "abc");
linkedHashMap.put(30, "abc");
linkedHashMap.put(40, "abc");
linkedHashMap.put(50, "abc");
linkedHashMap.put(60, "abc");
linkedHashMap.put(70, "abc");
linkedHashMap.put(70, "abcdddd");
System.out.println(linkedHashMap);
}
三、TreeMap
- 数据结构,只跟键有关
public static void main(String[] args) {
TreeMap<Student, Integer> treeMap = new TreeMap<>();
treeMap.put(new Student("zhangsan",1),1);
treeMap.put(new Student("zhangsan", 10), 1);
treeMap.put(new Student("zhangsan", 12), 1);
treeMap.put(new Student("zhangsan", 122), 1);
treeMap.put(new Student("zhangsan", 144), 1);
treeMap.put(new Student("zhangsan", 109), 1);
Set<Map.Entry<Student, Integer>> entries = treeMap.entrySet();
for (Map.Entry<Student, Integer> entry : entries) {
Student key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key.getName()+"==="+key.getAge()+"==="+value);
}
}
- 键 的数据结构是二叉树,键唯一,且可以对键进行排序
public static void main(String[] args) {
//空参构造,自然排序
TreeMap<Integer, String> treeMap = new TreeMap<>();
treeMap.put(14,"abc");
treeMap.put(16, "abc");
treeMap.put(155, "abc");
treeMap.put(11, "abc");
treeMap.put(12, "abc");
System.out.println(treeMap);
}
public static void main(String[] args) {
//有参构造,比较器排序
TreeMap<Student, Integer> treeMap = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int i = s1.getAge() - s2.getAge();
int i1 = i == 0 ? s1.getName().compareTo(s2.getName()) : i;
return i1;
}
});
treeMap.put(new Student("zhangsan",1),1);
treeMap.put(new Student("zhangsan", 10), 1);
treeMap.put(new Student("zhangsan", 12), 1);
treeMap.put(new Student("zhangsan", 122), 1);
treeMap.put(new Student("zhangsan", 144), 1);
treeMap.put(new Student("zhangsan", 109), 1);
Set<Map.Entry<Student, Integer>> entries = treeMap.entrySet();
for (Map.Entry<Student, Integer> entry : entries) {
Student key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key.getName()+"==="+key.getAge()+"==="+value);
}
}
四、案例演示
- 需求:获取字符串中每一个字母出现的次数
要求结果:a(5) b(4) c(3) d(2) e(1)
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入字符串");
String line = scanner.nextLine();
HashMap<Character, Integer> hashMap = new HashMap<>();
for (int i = 0; i < line.length(); i++) {
char ch = line.charAt(i);
if(!hashMap.containsKey(ch)){
hashMap.put(ch,1);
}else{
Integer integer = hashMap.get(ch);
integer++;
hashMap.put(ch,integer);
}
}
StringBuffer stringBuffer = new StringBuffer();
Set<Map.Entry<Character, Integer>> entries = hashMap.entrySet();
for (Map.Entry<Character, Integer> entry : entries) {
Character key = entry.getKey();
Integer value = entry.getValue();
stringBuffer.append(key).append('(').append(value).append(')').append('\t');
}
System.out.println(stringBuffer);
}
五、集合嵌套
1.HashMap 嵌套 HashMap
基础班
张三 20
李四 22
就业班
王五 21
赵六 23
public static void main(String[] args) {
HashMap<String, Integer> jchashMap = new HashMap<>();
jchashMap.put("张三", 20);
jchashMap.put("李四",22);
HashMap<String, Integer> jyhashMap = new HashMap<>();
jyhashMap.put("王五",21);
jyhashMap.put("赵六",23);
HashMap<String, HashMap<String, Integer>> map = new HashMap<>();
map.put("基础班",jchashMap);
map.put("就业班",jyhashMap);
Set<Map.Entry<String, HashMap<String, Integer>>> entries = map.entrySet();
for (Map.Entry<String, HashMap<String, Integer>> entry : entries) {
String key = entry.getKey();
System.out.println(key);
HashMap<String, Integer> value = entry.getValue();
Set<String> keySet = value.keySet();
for (String k : keySet) {
System.out.println("\t"+k+"\t"+value.get(k));
}
System.out.println();
}
}
2.HashMap 嵌套 ArrayList
三国演义
吕布
周瑜
笑傲江湖
令狐冲
林平之
神雕侠侣
郭靖
杨过
public static void main(String[] args) {
ArrayList<String> sgList = new ArrayList<>();
sgList.add("吕布");
sgList.add("周瑜");
ArrayList<String> xalist = new ArrayList<>();
xalist.add("令狐冲");
xalist.add("林平之");
ArrayList<String> sdlist = new ArrayList<>();
sdlist.add("郭靖");
sdlist.add("杨过");
HashMap<String, ArrayList<String>> hashMap = new HashMap<>();
hashMap.put("三国演义",sgList);
hashMap.put("笑傲江湖",xalist);
hashMap.put("神雕侠侣",sdlist);
Set<Map.Entry<String, ArrayList<String>>> entries = hashMap.entrySet();
for (Map.Entry<String, ArrayList<String>> entry : entries) {
String key = entry.getKey();
System.out.println(key);
ArrayList<String> value = entry.getValue();
for (String s : value) {
System.out.println("\t"+s);
}
System.out.println();
}
}
3.ArrayList嵌套HashMap
周瑜-- - 小乔
吕布-- - 貂蝉
郭靖-- - 黄蓉
杨过-- - 小龙女
令狐冲-- - 任盈盈
林平之-- - 岳灵珊
public static void main(String[] args) {
HashMap<String, String> hashMap1 = new HashMap<>();
hashMap1.put("周瑜","小乔");
hashMap1.put("吕布","貂蝉");
HashMap<String, String> hashMap2 = new HashMap<>();
hashMap2.put("郭靖","黄蓉");
hashMap2.put("杨过","小龙女");
HashMap<String, String> hashMap3 = new HashMap<>();
hashMap3.put("令狐冲","任盈盈");
hashMap3.put("林平之","岳灵珊");
ArrayList<HashMap<String, String>> list = new ArrayList<>();
list.add(hashMap1);
list.add(hashMap2);
list.add(hashMap3);
for (HashMap<String, String> hashMap : list) {
Set<Map.Entry<String, String>> entries = hashMap.entrySet();
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"---"+value);
}
System.out.println();
}
}