package exercise;import java.util.HashMap;
import java.util.Map;public class Demo1 {public static void main(String[] args) {Map<String, String> map = new HashMap<>();//在添加数据的时候,如果键不存在,那么直接把键值对对象添加到map集合当中,方法返回null//在添加数据的时候,如果键是存在的,那么会把原有的键值对对象覆盖,会把被覆盖的 值 进行返回。map.put("sunshine", "23");String sunshine = map.put("sunshine", "20");System.out.println(sunshine);map.put("jiuselu", "24");map.put("lulushui", "25");System.out.println(map);System.out.println("----------------------------------");String remove = map.remove("sunshine");System.out.println(remove);System.out.println(map);System.out.println("----------------------------------");map.clear();System.out.println(map);System.out.println("----------------------------------");boolean b = map.containsKey("sunshine");System.out.println(b);boolean b1 = map.containsValue("23");System.out.println(b1);System.out.println("----------------------------------");boolean empty = map.isEmpty();System.out.println(empty);System.out.println("----------------------------------");System.out.println(map.size());}
}
package exercise;import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;public class Demo2 {public static void main(String[] args) {Map<String, String> m = new HashMap<>();m.put("sunshine", "23");m.put("jiuselu", "24");m.put("lulushui", "25");Set<String> keys = m.keySet();keys.forEach(new Consumer<String>() {@Overridepublic void accept(String key) {String value = m.get(key);System.out.println(key + "=" + value);}});}
}
package exercise;import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;public class Demo2 {public static void main(String[] args) {Map<String, String> m = new HashMap<>();m.put("sunshine", "23");m.put("jiuselu", "24");m.put("lulushui", "25");//获取所有键值对对象entrySet<Map.Entry<String, String>> entries = m.entrySet();for (Map.Entry<String, String> entry : entries) {System.out.println(entry.getKey() + "=" + entry.getValue());}}
}
package exercise;import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;public class Demo3 {public static void main(String[] args) {Map<String, String> m = new HashMap<>();m.put("sunshine", "23");m.put("jiuselu", "24");m.put("lulushui", "25");m.forEach((key, value) -> System.out.println(key + "=" + value));}
}
package exercise;import java.util.HashMap;
import java.util.Objects;
import java.util.function.BiConsumer;public class Demo4 {public static void main(String[] args) {HashMap<Student, String> h = new HashMap<>();h.put(new Student("sunshine", 23), "hei");h.put(new Student("sunshine", 23), "long");h.put(new Student("lulushi", 25), "jiang");h.forEach(new BiConsumer<Student, String>() {@Overridepublic void accept(Student student, String s) {System.out.println(student + "=" + s);}});}
}class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}/*** 获取** @return name*/public String getName() {return name;}/*** 设置** @param name*/public void setName(String name) {this.name = name;}/*** 获取** @return age*/public int getAge() {return age;}/*** 设置** @param age*/public void setAge(int age) {this.age = age;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age && Objects.equals(name, student.name);}@Overridepublic int hashCode() {return Objects.hash(name, age);}public String toString() {return "Student{name = " + name + ", age = " + age + "}";}
}
package exercise;import java.util.*;public class Deno5 {public static void main(String[] args) {//定义数组,存储4个景点String[] arr = {"A", "B", "C", "D"};//利用随机数模拟80个同学投票,并把投票的结果存储起来ArrayList<String> list = new ArrayList<>();Random r = new Random();for (int i = 0; i < 80; i++) {int index = r.nextInt(arr.length);list.add(arr[index]);}//2.如果要统计的东西比较多,不方便使用计数器思想//我们可以定义map集合,利用集合进行统计HashMap<String, Integer> hm = new HashMap<>();for (String name : list) {//判断当前景点在map集合当中是否存在if (hm.containsKey(name)) {//存在//获取当前景点已经存在的次数Integer count = hm.get(name);//表示当前景点又被投了一次count++;//把新的次数再次添加到集合中hm.put(name, count);} else {//不存在hm.put(name, 1);}}System.out.println(hm);//求最大值int max = 0;Set<Map.Entry<String, Integer>> entries = hm.entrySet();for (Map.Entry<String, Integer> entry : entries) {Integer count = entry.getValue();if (max < count) {max = count;}}System.out.println(max);//判断哪个景点的次数和最大值一样,如果一样,打印出来for (Map.Entry<String, Integer> entry : entries) {Integer count = entry.getValue();if (max == count) {System.out.println(entry.getKey());}}}
}
package exercise;import java.util.LinkedHashMap;public class Demo6 {public static void main(String[] args) {LinkedHashMap<String, Integer> l = new LinkedHashMap<>();l.put("sunshine", 23);l.put("jiuselu", 24);l.put("lulushui", 25);System.out.println(l);}
}
需求一:
tips:o1-o2:升序o2-o1:降序
package exercise;import java.util.Comparator;
import java.util.TreeMap;public class Demo7 {public static void main(String[] args) {TreeMap<Integer, String> tp = new TreeMap<>(new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {return o2 - o1;}});tp.put(1, "芭蕉");tp.put(2, "榴莲");tp.put(3, "荔枝");System.out.println(tp);}
}
需求二:
package exercise;import java.util.TreeMap;public class Demo8 {public static void main(String[] args) {TreeMap<Student, String> tp = new TreeMap<>();tp.put(new Student("sunshine", 23), "hei");tp.put(new Student("jiuselu", 24), "long");tp.put(new Student("lulushui", 25), "jiang");System.out.println(tp);}
}class Student implements Comparable<Student> {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}/*** 获取** @return name*/public String getName() {return name;}/*** 设置** @param name*/public void setName(String name) {this.name = name;}/*** 获取** @return age*/public int getAge() {return age;}/*** 设置** @param age*/public void setAge(int age) {this.age = age;}public String toString() {return "Student{name = " + name + ", age = " + age + "}";}//o.getAge-this.getAge:按照年龄降序排列//this.getAge-o.getAge:按照年龄升序排列@Overridepublic int compareTo(Student o) {int i = o.getAge() - this.getAge();i = i == 0 ? this.getName().compareTo(o.getName()) : i;return i;}
}
package exercise;import java.util.Map;
import java.util.Set;
import java.util.TreeMap;public class Demo9 {public static void main(String[] args) {String str = "aababcabcdabcde";TreeMap<Character, Integer> tm = new TreeMap<>();for (int i = 0; i < str.length(); i++) {char c = str.charAt(i);if (tm.containsKey(c)) {Integer count = tm.get(c);count++;tm.put(c, count);} else {tm.put(c, 1);}}Set<Map.Entry<Character, Integer>> entries = tm.entrySet();for (Map.Entry<Character, Integer> entry : entries) {System.out.print(entry.getKey() + "(" + entry.getValue() + ")");}}
}