目录
Map集合
双列集合,他的数据结构只跟键有关,跟值没有关系。
HashMap数据结构是哈希表(数组+链表+红黑树),元素唯一,无序。
HashMap<String, String> hm = new HashMap<>();
//一个键,只能映射一个值,键相同,值覆盖
//第一次存储该键,返回的是null
String name = hm.put("文章", "马伊琍");
System.out.println(name);
//当你第二次,存储相同的键,返回的是该键上次对应的旧值。
name = hm.put("文章", "姚笛");
System.out.println(name);
hm.put("大郎", "金莲");
hm.put("王宝强", "马蓉");
hm.put("贾乃亮", "李小璐");
hm.put("陈思诚", "佟丽娅");
System.out.println(hm);
// null
马伊琍
{贾乃亮=李小璐, 文章=姚笛, 王宝强=马蓉, 陈思诚=佟丽娅, 大郎=金莲}
HashMap<String, Student> hm = new HashMap<>();
hm.put("s001", new Student("李杰", 20));
hm.put("s002", new Student("王轲祥", 19));
hm.put("s003", new Student("师肖杰", 20));
hm.put("s004", new Student("李子煊", 20));
hm.put("s005", new Student("周晓慧", 18));
hm.put("s001", new Student("刘德华", 20));
hm.put("s002", new Student("郭富城", 20));
System.out.println(hm);
//存储键是Student 类型,值是String类型
//所有的双列集合,他的数据结构只跟键有关,跟值没有关系。
//HashMap 他键的唯一性,是靠键重写hashCode 和equals()方法。来保证的,如果键不重写,在无法保证键的唯一性。
HashMap<Student, String> hm = new HashMap<>();
hm.put(new Student("李杰", 20), "s001");
hm.put(new Student("李杰", 20), "s001");
hm.put(new Student("王轲祥", 19), "s002");
hm.put(new Student("师肖杰", 20), "s003");
hm.put(new Student("李子煊", 20), "s004");
hm.put(new Student("周晓慧", 18), "s005");
hm.put(new Student("刘德华", 20), "s001");
hm.put(new Student("郭富城", 20), "s002");
System.out.println(hm); //不唯一,要重写方法
方法
添加功能
V put(K key,V value): 添加元素
删除功能
V remove(Object key):根据键删除键值对元素,并把值返回
HashMap<String, String> hm = new HashMap<>();
hm.put("文章", "马伊琍");
hm.put("大郎", "金莲");
hm.put("王宝强", "马蓉");
hm.put("贾乃亮", "李小璐");
hm.put("陈思诚", "佟丽娅");
//根据键,删除一个
hm.remove("文章");
System.out.println(hm);
//{贾乃亮=李小璐, 王宝强=马蓉, 陈思诚=佟丽娅, 大郎=金莲}
//全部清空集合
hm.clear();
判断功能
boolean containsKey(Object key):判断集合是否包含指定的键
boolean containsValue(Object value):判断集合是否包含指定的值
boolean isEmpty():判断集合是否为空
//判断有没有该键
boolean b = hm.containsKey("文章");
System.out.println(b);
//判断有没有该值
boolean b1 = hm.containsValue("李小璐");
System.out.println(b1);
//判断集合是否为空
boolean empty = hm.isEmpty();
System.out.println(empty);
获取功能
HashMap<String, String> hm = new HashMap<>();
hm.put("文章", "马伊琍");
hm.put("大郎", "金莲");
hm.put("王宝强", "马蓉");
hm.put("贾乃亮", "李小璐");
hm.put("陈思诚", "佟丽娅");
/* //方式1:键找值
String value = hm.get("文章");
System.out.println(value);
//获取所有的键的集合
Set<String> keySet = hm.keySet();
System.out.println(keySet);
遍历
方式一
//方式1:键找值
Set<String> keySet = hm.keySet();
for (String key : keySet) {
String value = hm.get(key);
System.out.println(key + "===" + value);
}
//获取所有值的集合
Collection<String> values = hm.values();
System.out.println(values);
方式二
//方式2: 获取整个键值对 对象 Node
//获取所有键值对,对象的集合
Set<Map.Entry<String, String>> entries = hm.entrySet();
for (Map.Entry<String, String> node : entries) {
String key = node.getKey();
String value = node.getValue();
System.out.println(key + "====" + value);
LinkedHashMap
// LinkedHashMap 底层数据结构是链表和哈希表,链表保证键有序,哈希表保证键唯一
//双列集合的数据结构,只跟键有关,跟值没有关系
LinkedHashMap<String, String> lh = new LinkedHashMap<>();
lh.put("文章", "马伊琍");
lh.put("大郎", "金莲");
lh.put("王宝强", "马蓉");
lh.put("贾乃亮", "李小璐");
lh.put("陈思诚", "佟丽娅");
System.out.println(lh); //{文章=马伊琍, 大郎=金莲, 王宝强=马蓉, 贾乃亮=李小璐, 陈思诚=佟丽娅}
TreeMap
TreeMap<Integer, String> treeMap = new TreeMap<>();
treeMap.put(20, "李白");
treeMap.put(22, "李清照");
treeMap.put(1, "杜甫");
treeMap.put(2, "辛弃疾");
treeMap.put(50, "柳宗元");
treeMap.put(6, "李商隐");
treeMap.forEach(new BiConsumer<Integer, String>() {
@Override
public void accept(Integer key, String value) {
System.out.println(key + "=========" + value);
}
});
//1=========杜甫
2=========辛弃疾
6=========李商隐
20=========李白
22=========李清照
50=========柳宗元
TreeMap<Student, String> hm = new TreeMap<>();
hm.put(new Student("李杰", 20), "s001");
hm.put(new Student("李杰", 20), "s001");
hm.put(new Student("王轲祥", 19), "s002");
hm.put(new Student("师肖杰", 20), "s003");
hm.put(new Student("李子煊", 20), "s004");
hm.put(new Student("周晓慧", 18), "s005");
hm.put(new Student("刘德华", 20), "s001");
hm.put(new Student("郭富城", 20), "s002");
hm.forEach(new BiConsumer<Student, String>() {
@Override
public void accept(Student student, String s) {
System.out.println(student.getAge() + "===" + student.getName() + "====" + s);
}
});
//自然排序
public 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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public 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);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public int compareTo(Student s) {
int num = this.age - s.age;
int num2 = num == 0 ? this.name.compareTo(s.name) : num;
return num2;
}
}
public static void main(String[] args) { //比较器排序
TreeMap<Student, String> hm = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int i = s1.getAge() - s2.getAge();
int j = i == 0 ? s1.getName().compareTo(s2.getName()) : i;
return j;
}
});
hm.put(new org.xingyun.demo4.Student("李杰", 20), "s001");
hm.put(new org.xingyun.demo4.Student("李杰", 20), "s001");
hm.put(new org.xingyun.demo4.Student("王轲祥", 19), "s002");
hm.put(new org.xingyun.demo4.Student("师肖杰", 20), "s003");
hm.put(new org.xingyun.demo4.Student("李子煊", 20), "s004");
hm.put(new org.xingyun.demo4.Student("周晓慧", 18), "s005");
hm.put(new org.xingyun.demo4.Student("刘德华", 20), "s001");
hm.put(new org.xingyun.demo4.Student("郭富城", 20), "s002");
hm.forEach(new BiConsumer<Student, String>() {
@Override
public void accept(Student student, String s) {
System.out.println(student.getAge() + "===" + student.getName() + "====" + s);
}
});
}
HashTable
Hashtable<String, String> ht = new Hashtable<>();
// Hashtable 他键或者值,不允许是null值
//线程安全的,效率低
ht.put(null, "abc");HashMap<String, String> hm = new HashMap<>();
// HashMap可以存储 null值 null键
//线程不安全,效率高
hm.put(null, null);
例
需求:统计字符串中每个字符出现的次数
"aababcabcdabcde", 获取字符串中每一个字母出现的次数要求结果:a(5) b(4) c(3) d(2) e(1)
*/
String str = "aababcabcdabcdezzyyyjjjj"; // a(5) b(4) c(3) d(2) e(1)
/*
* 分析:数据的特点
* a----5
* b-----4
* c-----3
* d------2
* e------1
*
* 符合键值对应关系的数据,我们就会用双列集合来存储
*
* */
LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i); //截取每个字符,往集合中放,并统计每个字符的个数 a 1 a
if (!map.containsKey(ch)) {
map.put(ch, 1);
} else {
Integer value = map.get(ch);
value++;
map.put(ch, value);
}
}
System.out.println(map);
//遍历集合拼串 a(5) b(4) c(3) d(2) e(1)
StringBuilder sb = new StringBuilder();
map.forEach(new BiConsumer<Character, Integer>() {
@Override
public void accept(Character key, Integer value) {
sb.append(key).append("(").append(value).append(")");
}
});
String s = sb.toString();
System.out.println(s);
集合嵌套
public static void main(String[] args) {
/*
*
*
* 基础班
张三 20
李四 22
就业班
王五 21
赵六 23
*
* */
HashMap<String, Integer> jcMap = new HashMap<>();
jcMap.put("张三", 20);
jcMap.put("李四", 22);
HashMap<String, Integer> jyMap = new HashMap<>();
jyMap.put("王五", 21);
jyMap.put("赵六", 23);
HashMap<String, HashMap<String, Integer>> maxMap = new HashMap<>();
maxMap.put("基础班", jcMap);
maxMap.put("就业班", jyMap);
/*
*
*
* 基础班
张三 20
李四 22
就业班
王五 21
赵六 23
*
* */
maxMap.forEach(new BiConsumer<String, HashMap<String, Integer>>() {
@Override
public void accept(String key, HashMap<String, Integer> value) {
System.out.println(key);
value.forEach(new BiConsumer<String, Integer>() {
@Override
public void accept(String minKey, Integer minValue) {
System.out.println("\t\t" + minKey + "\t\t" + minValue);
}
});
}
});
System.out.println("=========================================");
Set<String> keySet = maxMap.keySet();
for (String key : keySet) {
System.out.println(key);
HashMap<String, Integer> minMap = maxMap.get(key);
Set<String> minKeySet = minMap.keySet();
for (String minKey : minKeySet) {
Integer minValue = minMap.get(minKey);
System.out.println("\t\t" + minKey + "\t\t" + minValue);
}
}
System.out.println("=================================");
Set<Map.Entry<String, HashMap<String, Integer>>> entries = maxMap.entrySet();
for (Map.Entry<String, HashMap<String, Integer>> node : entries) {
String key = node.getKey();
System.out.println(key);
HashMap<String, Integer> minMap = node.getValue();
Set<Map.Entry<String, Integer>> entries1 = minMap.entrySet();
for (Map.Entry<String, Integer> minNode : entries1) {
String minKey = minNode.getKey();
Integer minValue = minNode.getValue();
System.out.println("\t\t" + minKey + "\t\t" + minValue);
}
}
}
public class MyTest2 {
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("杨过");
LinkedHashMap<String, ArrayList<String>> maxMap = new LinkedHashMap<>();
maxMap.put("三国演义", sgList);
maxMap.put("笑傲江湖", xaList);
maxMap.put("神雕侠侣", sdList);
maxMap.forEach(new BiConsumer<String, ArrayList<String>>() {
@Override
public void accept(String key, ArrayList<String> value) {
System.out.println(key);
value.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println("\t" + s);
}
});
}
});
}
}
Collections类
Collections成员方法
public static <T> void sort(List<T> list): 排序,默认按照自然顺序
public static <T> int binarySearch(List<?> list,T key): 二分查找
public static <T> T max(Collection<?> coll): 获取最大值
public static void reverse(List<?> list): 反转
public static void shuffle(List<?> list): 随机置换
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
return a - b;
}
});
System.out.println(list);
//二分查找,元素必须有序
int index = Collections.binarySearch(list, 24);
System.out.println(index);
Integer max = Collections.max(list);
Integer min = Collections.min(list);
System.out.println(max);
System.out.println(min);
Collections.reverse(list);
System.out.println(list);
Collections.shuffle(list);
System.out.println(list);