Day18
集合框架
Map集合的概述和特点
Map集合功能的概述
Map集合遍历
Set<String> keySet = map.keySet();
Iterator<String> it = keySet.iterator();
while(it.hasNext()) {
String key = it.next();
Integer value = map.get(key);
System.out.println(key + "=" + value);
}
for(String key : map.keySet()) {
System.out.println(key + "=" + map.get(key));
}
Map集合的遍历之键值对对象找键和值
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
Iterator<Map.Entry<String, Integer>> it = entrySet.iterator();
while(it.hasNext()) {
Map.Entry<String, Integer> en = it.next();
String key = en.getKey();
Integer value = en.getValue();
System.out.println(key + "=" + value);
}
for(Entry<String, Integer> en : map.entrySet()) {
System.out.println(en.getKey() + "=" + en.getValue());
}
HashMap集合键是Student值是String的案例
A:案例演示
public class Student {
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
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 int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public static void main(String[] args) {
HashMap<Student, String> hm = new HashMap<>();
hm.put(new Student("张三", 23), "北京");
hm.put(new Student("张三", 23), "上海");
hm.put(new Student("李四", 24), "广州");
hm.put(new Student("王五", 25), "深圳");
System.out.println(hm);
}
LinkedHashMap的概述和使用
* LinkedHashMap可以保证怎么存就怎么取
public static void main(String[] args) {
LinkedHashMap<String, Integer> lhm = new LinkedHashMap<>();
lhm.put("张三", 23);
lhm.put("李四", 24);
lhm.put("赵六", 26);
lhm.put("王五", 25);
System.out.println(lhm);
TreeMap集合键是Student值是String的案例
package com.heima.map;
import java.util.Comparator;
import java.util.TreeMap;
import com.heima.bean.Student;
public class Demo7_TreeMap {
public static void main(String[] args) {
TreeMap<Student, String> tm = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getName().compareTo(s2.getName());
return num == 0 ? s1.getAge() - s2.getAge() : num;
}
});
tm.put(new Student("张三", 23), "北京");
tm.put(new Student("李四", 13), "上海");
tm.put(new Student("赵六", 43), "深圳");
tm.put(new Student("王五", 33), "广州");
System.out.println(tm);
}
public static void demo1() {
TreeMap<Student, String> tm = new TreeMap<>();
tm.put(new Student("张三", 23), "北京");
tm.put(new Student("李四", 13), "上海");
tm.put(new Student("王五", 33), "广州");
tm.put(new Student("赵六", 43), "深圳");
System.out.println(tm);
}
}
package com.heima.map;
import java.util.Comparator;
import java.util.TreeMap;
import com.heima.bean.Student;
public class Demo7_TreeMap {
public static void main(String[] args) {
TreeMap<Student, String> tm = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getName().compareTo(s2.getName());
return num == 0 ? s1.getAge() - s2.getAge() : num;
}
});
tm.put(new Student("张三", 23), "北京");
tm.put(new Student("李四", 13), "上海");
tm.put(new Student("赵六", 43), "深圳");
tm.put(new Student("王五", 33), "广州");
System.out.println(tm);
}
public static void demo1() {
TreeMap<Student, String> tm = new TreeMap<>();
tm.put(new Student("张三", 23), "北京");
tm.put(new Student("李四", 13), "上海");
tm.put(new Student("王五", 33), "广州");
tm.put(new Student("赵六", 43), "深圳");
System.out.println(tm);
}
}
案例演示
package com.heima.test;
import java.util.HashMap;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String s = sc.nextLine();
char[] arr = s.toCharArray();
HashMap<Character, Integer> hm = new HashMap<>();
for (char c : arr) {
hm.put(c, !hm.containsKey(c) ? 1 : hm.get(c) + 1);
}
for (Character key : hm.keySet()) {
System.out.println(key + "=" + hm.get(key));
}
}
}
HashMap嵌套HashMap
package com.heima.map;
import java.util.HashMap;
import com.heima.bean.Student;
public class Demo8_HashMapHashMap {
public static void main(String[] args) {
HashMap<Student, String> hm88 = new HashMap<>();
hm88.put(new Student("张三", 23), "北京");
hm88.put(new Student("李四", 24), "北京");
hm88.put(new Student("王五", 25), "上海");
hm88.put(new Student("赵六", 26), "广州");
HashMap<Student, String> hm99 = new HashMap<>();
hm99.put(new Student("唐僧", 1023), "北京");
hm99.put(new Student("孙悟空",1024), "北京");
hm99.put(new Student("猪八戒",1025), "上海");
hm99.put(new Student("沙和尚",1026), "广州");
HashMap<HashMap<Student, String>, String> hm = new HashMap<>();
hm.put(hm88, "第88期基础班");
hm.put(hm99, "第99期基础班");
for(HashMap<Student, String> h : hm.keySet()) {
String value = hm.get(h);
for(Student key : h.keySet()) {
String value2 = h.get(key);
System.out.println(key + "=" + value2 + "=" + value);
}
}
}
}
HashMap和Hashtable的区别
A:面试题
*HashMap和Hashtable的区别
* 共同点:
* 底层都是哈希算法,都是双列集合
* 区别:
* 1,HashMap是线程不安全的,效率高,JDK1.2版本
* Hashtable是线程安全的,效率低,JDK1.0版本的
* 2,HashMap可以存储null键和null值
* Hashtable不可以存储null键和null值
B:案例演示
*HashMap和Hashtable的区别
Collections类的概述和使用
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("c");
list.add("d");
list.add("g");
list.add("f");
System.out.println(Collections.max(list));
Collections.reverse(list);
Collections.shuffle(list);
System.out.println(list);
}
public static void demo2() {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("c");
list.add("d");
list.add("f");
list.add("g");
System.out.println(Collections.binarySearch(list, "c"));
System.out.println(Collections.binarySearch(list, "b")); 不存在返回负的插入点减一
}
public static void demo1() {
ArrayList<String> list = new ArrayList<>();
list.add("c");
list.add("a");
list.add("a");
list.add("b");
list.add("d");
System.out.println(list);
Collections.sort(list);
System.out.println(list);
}
}
案例演示(斗地主洗牌,发牌)
package com.heima.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class Test3 {
public static void main(String[] args) {
String[] num = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
String[] color = { "红桃", "黑桃", "方片", "梅花" };
HashMap<Integer, String> hm = new HashMap<>();
ArrayList<Integer> list = new ArrayList<>();
int index = 0;
for (String s1 : num) {
for (String s2 : color) {
hm.put(index, s2.concat(s1));
list.add(index);
index++;
}
}
hm.put(index, "小王");
list.add(index);
index++;
hm.put(index, "大王");
list.add(index);
Collections.shuffle(list);
TreeSet<Integer> gaojin = new TreeSet<>();
TreeSet<Integer> longwu = new TreeSet<>();
TreeSet<Integer> me = new TreeSet<>();
TreeSet<Integer> dipai = new TreeSet<>();
for (int i = 0; i < list.size(); i++) {
if (i >= list.size() - 3) {
dipai.add(list.get(i));
} else if (i % 3 == 0) {
gaojin.add(list.get(i));
} else if (i % 3 == 1) {
longwu.add(list.get(i));
} else {
me.add(list.get(i));
}
}
lookPoker(hm, gaojin, "高进");
lookPoker(hm, longwu, "龙五");
lookPoker(hm, me, "冯佳");
lookPoker(hm, dipai, "底牌");
}
public static void lookPoker(HashMap<Integer, String> hm, TreeSet<Integer> ts, String name) {
System.out.print(name + "的牌是:");
for (Integer i : ts) {
System.out.print(hm.get(i) + " ");
}
System.out.println();
}
}
泛型固定下边界
class CompareByAge implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getAge() - s2.getAge();
return num == 0 ? s1.getName().compareTo(s2.getName()) : num;
}
}
public static void main(String[] args) {
TreeSet<Student> ts1 = new TreeSet<>(new CompareByAge());
ts1.add(new Student("张三", 33));
ts1.add(new Student("李四", 13));
ts1.add(new Student("王五", 23));
ts1.add(new Student("赵六", 43));
TreeSet<BaseStudent> ts2 = new TreeSet<>(new CompareByAge());
ts2.add(new BaseStudent("张三", 33));
ts2.add(new BaseStudent("李四", 13));
ts2.add(new BaseStudent("王五", 23));
ts2.add(new BaseStudent("赵六", 43));
System.out.println(ts2);
}
public static void demo1() {
ArrayList<Student> list1 = new ArrayList<>();
list1.add(new Student("张三", 23));
list1.add(new Student("李四", 24));
ArrayList<BaseStudent> list2 = new ArrayList<>();
list2.add(new BaseStudent("王五", 25));
list2.add(new BaseStudent("赵六", 26));
list1.addAll(list2);
System.out.println(list1);
}
简单总结