Map集合概述和特点)
Map接口概述 查看API可以知道: 将键映射到值的对象 一个映射不能包含重复的键 每个键最多只能映射到一个值 Map接口和Collection接口的不同 Map是双列的,Collection是单列的 Map的键唯一,Collection的子体系Set是唯一的 Map集合的数据结构针对键有效,跟值无关;Collection集合的数据结构是针对元素有效
Map集合的功能概述
Map集合的功能概述 a:添加功能 V put(K key,V value):添加元素。这个其实还有另一个功能?替换 如果键是第一次存储,就直接存储元素,返回null 如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值 b:删除功能 void clear():移除所有的键值对元素 V remove(Object key):根据键删除键值对元素,并把值返回 c:判断功能 boolean containsKey(Object key):判断集合是否包含指定的键 boolean containsValue(Object value):判断集合是否包含指定的值 boolean isEmpty():判断集合是否为空 d:获取功能 Set<Map.Entry<K,V>> entrySet(): 返回一个键值对的Set集合 V get(Object key):根据键获取值 Set keySet():获取集合中所有键的集合 Collection values():获取集合中所有值的集合 e:长度功能 int size():返回集合中的键值对的对数
Map集合的遍历之键值对对象找键和值
public class MyTest4 {
public static void main(String[] args) {
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "bbb");
hashMap.put(2, "ccc");
hashMap.put(3, "ddd");
hashMap.put(4, "eee");
hashMap.put(5, "fff");
//entrySet() 获取键值对 对象 放到Set集合里面去
Set<Map.Entry<Integer, String>> entries = hashMap.entrySet();
//K getKey ()
//返回与此项对应的键。
//V getValue ()
//返回与此项对应的值。
for (Map.Entry<Integer, String> entry : entries) {
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"==="+value);
}
}
}
HashMap集合键是String值是Student的案例
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 o) {
int num=this.age-o.age;
int num2=num==0?this.name.compareTo(o.name):num;
return num2;
}
}
public class MyTest {
public static void main(String[] args) {
TreeMap<Student, String> treeMap = new TreeMap<>();
treeMap.put(new Student("张三0", 23), "111");
treeMap.put(new Student("张三0", 23), "222");
treeMap.put(new Student("张三0", 23333), "222");
treeMap.put(new Student("张三1", 24), "444");
treeMap.put(new Student("张三2", 26), "555");
treeMap.put(new Student("张三3", 25), "666");
treeMap.put(new Student("张三4", 28), "777");
Set<Map.Entry<Student, String>> entries = treeMap.entrySet();
for (Map.Entry<Student, String> entry : entries) {
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"==="+value);
}
}
}
LinkedHashMap的概述和使用
LinkedHashMap的概述: Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序 LinkedHashMap的特点: 底层的数据结构是链表和哈希表 元素有序 并且唯一 元素的有序性由链表数据结构保证 唯一性由 哈希表数据结构保证 Map集合的数据结构只和键有关
集合嵌套HashMap嵌套HashMap
public class MyTest {
public static void main(String[] args) {
//HashMap 嵌套 HashMap
HashMap<String, Integer> jc = new HashMap<>();
jc.put("张三", 20);
jc.put("李四", 22);
HashMap<String, Integer> jy = new HashMap<>();
jy.put("王五", 21);
jy.put("赵六", 23);
HashMap<String, HashMap<String, Integer>> maxMap = new HashMap<String, HashMap<String, Integer>>();
maxMap.put("基础班", jc);
maxMap.put("就业班", jy);
//遍历集合
Set<String> keySet = maxMap.keySet();
for (String s : keySet) {
System.out.println(s);
HashMap<String, Integer> minMap = maxMap.get(s);
Set<String> minKey = minMap.keySet();
for (String s1 : minKey) {
System.out.println("\t" + s1 + "\t" + minMap.get(s1));
}
System.out.println();
}
System.out.println("------------------------");
Set<Map.Entry<String, HashMap<String, Integer>>> entries = maxMap.entrySet();
for (Map.Entry<String, HashMap<String, Integer>> en : entries) {
String key = en.getKey();
System.out.println(key);
HashMap<String, Integer> minMap = en.getValue();
Set<Map.Entry<String, Integer>> entries1 = minMap.entrySet();
for (Map.Entry<String, Integer> stringIntegerEntry : entries1) {
String key1 = stringIntegerEntry.getKey();
Integer value = stringIntegerEntry.getValue();
System.out.println("\t"+key1+" "+value);
}
System.out.println();
}
}
}
集合嵌套之HashMap嵌套ArrayList
public class MyTest2 {
public static void main(String[] args) {
// HashMap 嵌套 ArrayList
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("杨过");
sdList.add("尹志平");
LinkedHashMap<String, ArrayList<String>> maxMap = new LinkedHashMap<>();
maxMap.put("三国演义", sgList);
maxMap.put("笑傲江湖", xaList);
maxMap.put("神雕侠侣", sdList);
Set<String> keySet = maxMap.keySet();
for (String key : keySet) {
System.out.println(key);
ArrayList<String> list = maxMap.get(key);
for (String s : list) {
System.out.println("\t"+s);
}
System.out.println();
}
}
}
集合嵌套之ArrayList嵌套HashMap
public class MyTest3 {
public static void main(String[] args) {
//ArrayList 嵌套 HashMap
HashMap<String, String> sgMap = new HashMap<>();
sgMap.put("周瑜", "小乔");
sgMap.put("吕布", "貂蝉");
HashMap<String, String> sdMap = new HashMap<>();
sdMap.put("郭靖", "黄蓉");
sdMap.put("杨过", "小龙女");
HashMap<String, String> xaMap = new HashMap<>();
xaMap.put("令狐冲", "任盈盈");
xaMap.put("林平之", "岳灵珊");
ArrayList<HashMap<String, String>> maxList = new ArrayList<>();
maxList.add(sgMap);
maxList.add(sdMap);
maxList.add(xaMap);
for (HashMap<String, String> map : maxList) {
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);
}
System.out.println();
}
}
}
HashMap和Hashtable的区别
HashMap和Hashtable的区别: 查看API可以知道 HashMap: 线程不安全,效率高.允许null值和null键 Hashtable: 线程安全 , 效率低.不允许null值和null键
ollections工具类的概述和常见方法
Collections类概述: 针对集合操作 的工具类 Collections成员方法 public static void sort(List list): 排序,默认按照自然顺序 public static int binarySearch(List<?> list,T key): 二分查找 public static T max(Collection<?> coll): 获取最大值 public static void reverse(List<?> list): 反转 public static void shuffle(List<?> list): 随机置换
模拟斗地主洗牌和发牌
public class MyTest {
public static void main(String[] args) {
ArrayList<String> pokerBox = new ArrayList<>();
//生成54张牌放到牌盒里面
String[] colors = {"♠", "♥", "♦", "♣"};
String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for (String color : colors) {
for (String num : nums) {
pokerBox.add(color.concat(num));
}
}
//手动添加大小王
pokerBox.add("★");
pokerBox.add("☆");
//洗牌
Collections.shuffle(pokerBox);
Collections.shuffle(pokerBox);
Collections.shuffle(pokerBox);
//发牌
ArrayList<String> 高进 = new ArrayList<>();
ArrayList<String> 刀仔 = new ArrayList<>();
ArrayList<String> 星仔 = new ArrayList<>();
ArrayList<String> 底牌 = new ArrayList<>();
// 高进 = (ArrayList<String>) pokerBox.subList(0,17);
// 高进 0 3 6 9
//刀仔 1 4 7 10
// 星仔 2 5 8 11
for (int i = 0; i < pokerBox.size(); i++) {
if (i >= pokerBox.size() - 3) {
底牌.add(pokerBox.get(i));
} else if (i % 3 == 0) {
高进.add(pokerBox.get(i));
} else if (i % 3 == 1) {
刀仔.add(pokerBox.get(i));
} else {
星仔.add(pokerBox.get(i));
}
}
//看牌
lookPoker("高进",高进);
lookPoker("刀仔", 刀仔);
lookPoker("星仔", 星仔);
lookPoker("底牌", 底牌);
}
private static void lookPoker(String name, ArrayList<String> list) {
System.out.println(name);
for (String s : list) {
System.out.print(s+" ");
}
System.out.println();
}
}