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集合的继承体系图解
Map集合的遍历之键找值
public class MyTest {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("文章", "马伊琍");
map.put("贾乃亮", "李小璐");
map.put("陈思成", "佟丽娅");
键找值
//String value = map.get("文章");
//System.out.println(value);
//遍历 键找值
//获取所有键的集合
Set<String> keySet = map.keySet();
//遍历键集 键找值
for (String key : keySet) {
System.out.println(key+"=="+map.get(key));
}
}
}
Map集合的遍历之键值对对象找键和值
public class MyTest2 {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("文章", "马伊琍");
map.put("贾乃亮", "李小璐");
map.put("陈思成", "佟丽娅");
//获取出,键值对 对象 的集合
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集合键是Stirng值是String的案例
public class MyTest3 {
public static void main(String[] args) {
// HashMap 键的 数据结构是哈希表,键唯一(键唯一,靠键重写equals方法,来保证,合理的 重写hashCode方法,是想让元素,减少碰撞) 无序
// HashMap 允许存储null键和null值 线程不安全效率高
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("abc",100);
hashMap.put(null,200);
hashMap.put("bbb",null);
hashMap.put(null,null);
System.out.println(hashMap);
Set<String> keySet = hashMap.keySet();
for (String key : keySet) {
System.out.println(key+"=="+hashMap.get(key));
}
System.out.println("----------------------");
Set<Map.Entry<String, Integer>> entries = hashMap.entrySet();
for (Map.Entry<String, Integer> entry : entries) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+"==="+value);
}
}
}
HashMap集合键是String值是Student的案例
public class Student {
private String name;
private int age;
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 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 String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
---------------------------------------------------------------------------------------
public class MyTest {
public static void main(String[] args) {
//存储键是String 类型 值Student类型
HashMap<String, Student> hashMap = new HashMap<>();
hashMap.put("s001",new Student("张三",23));
hashMap.put("s001", new Student("李四", 24));
hashMap.put("s002", new Student("王五", 25));
hashMap.put("s003", new Student("赵六", 26));
System.out.println(hashMap);
}
}
LinkedHashMap的概述和使用
- LinkedHashMap的概述:
Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序 - LinkedHashMap的特点:
底层的数据结构是链表和哈希表 元素有序 并且唯一
元素的有序性由链表数据结构保证 唯一性由 哈希表数据结构保证
Map集合的数据结构只和键有关 - LinkedHashMap的案例:
public class MyTest4 {
public static void main(String[] args) {
//键唯一且有序,链表保证了键有序,哈希表保证了键唯一
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集合键是Student值是Integer的案例
public class Student implements Comparable<Student>{
private String name;
private int age;
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 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 String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Student s) {
int i = this.age - s.age;
int j=i==0?this.name.compareTo(s.name):i;
return j;
}
}
---------------------------------------------------------------------------------------
public class MyTest6 {
public static void main(String[] args) {
//Map集合的数据结构,只跟键有关
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);
treeMap.put(new Student("zhangsan", 156), 1);
treeMap.put(new Student("zhangsan", 124), 1);
treeMap.put(new Student("zhangsan", 124), 1);
Set<Map.Entry<Student, Integer>> entries = treeMap.entrySet();
for (Map.Entry<Student, Integer> en : entries) {
Student key = en.getKey();
Integer value = en.getValue();
System.out.println(key.getName()+"=="+key.getAge()+"======="+value);
}
}
}
统计字符串中每个字符出现的次数
public class Demo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一段字符串");
String s = sc.nextLine();
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(map.containsKey(c)){
Integer integer = map.get(c);
integer++;
map.put(c,integer);
}else {
map.put(c,1);
}
}
Set<Character> set = map.keySet();
for (Character character : set) {
System.out.print(character+"("+map.get(character)+")");
}
}
}
集合嵌套的案例
请编写程序,完成集合嵌套,并遍历
HashMap嵌套HashMap
HashMap嵌套ArrayList
传智播客
jc 基础班
张三 20
李四 22
jy 就业班
王五 21
赵六 23
public class Demo {
public static void main(String[] args) {
HashMap<String, Integer>
jichu = new HashMap<>();
jichu.put("张三",20);
jichu.put("李四",22);
HashMap<String, Integer>
jiuye = new HashMap<>();
jiuye.put("王五",21);
jiuye.put("赵六",23);
HashMap<String,HashMap<String, Integer>> map = new HashMap<>();
map.put("jc\t基础班",jichu);
HashMap<String,HashMap<String, Integer>> map1 = new HashMap<>();
map1.put("jy\t就业班",jiuye);
ArrayList<HashMap<String,HashMap<String, Integer>>> list = new ArrayList<>();
list.add(map);
list.add(map1);
HashMap<String,ArrayList<HashMap<String, HashMap<String, Integer>>>> map2= new HashMap<>();
map2.put("传智播客",list);
Set<Map.Entry<String,ArrayList<HashMap<String, HashMap<String, Integer>>>>>entries = map2.entrySet();
for (Map.Entry<String,ArrayList<HashMap<String, HashMap<String, Integer>>>>entry : entries) {
System.out.println(entry.getKey());
ArrayList<HashMap<String, HashMap<String, Integer>>>list1 = entry.getValue();
for (HashMap<String, HashMap<String,Integer>> hashMap : list1) {
Set<String> set =hashMap.keySet();
for (String s : set) {
System.out.println("\t"+s);
HashMap<String,Integer> hashMap1 = hashMap.get(s);
Set<Map.Entry<String, Integer>> set1 = hashMap1.entrySet();
for (Map.Entry<String, Integer> hs : set1){
System.out.println("\t\t\t"+hs.getKey()+" "+hs.getValue());
}
}
}
}
}
}
HashMap和Hashtable的区别
- HashMap和Hashtable的区别:
HashMap: 线程不安全,效率高.允许null值和null键
Hashtable: 线程安全 , 效率低.不允许null值和null键
Collections工具类的概述和常见方法讲解
- 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 PokerGame2 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 54; i++) {
list.add(i);
}
ArrayList<String> list1 = new ArrayList<>();
list1.add("?");
list1.add("☀");
list1.add("♠2");
list1.add("♥2");
list1.add("♣2");
list1.add("♦2");
list1.add("♠A");
list1.add("♥A");
list1.add("♣A");
list1.add("♦A");
list1.add("♠K");
list1.add("♥K");
list1.add("♣K");
list1.add("♦K");
list1.add("♠Q");
list1.add("♥Q");
list1.add("♣Q");
list1.add("♦Q");
list1.add("♠J");
list1.add("♥J");
list1.add("♣J");
list1.add("♦J");
for (int i =10; i > 2; i--) {
list1.add("♠"+i);
list1.add("♥"+i);
list1.add("♣"+i);
list1.add("♦"+i);
}
TreeMap<Integer, String> map = new TreeMap<>();
for (int i = 0; i < 54; i++) {
map.put(list.get(i),list1.get(i));
}
//洗牌
Collections.shuffle(list);
Collections.shuffle(list);
Collections.shuffle(list);
ArrayList<Integer> 周星驰 = new ArrayList<>();
ArrayList<Integer> 周润发 = new ArrayList<>();
ArrayList<Integer> 刘德华 = new ArrayList<>();
TreeSet<Integer> 底牌 = new TreeSet<>();
for (int i = 0; i < 17; i++) {
周星驰.add(list.get(3*i));
周润发.add(list.get(3*i+1));
刘德华.add(list.get(3*i+2));
}
for (int i = 51; i < 54; i++) {
底牌.add(list.get(i));
}
TreeMap<Integer, String> 周 = new TreeMap<>();
for (int i = 0; i < 17; i++) {
Integer integer = 周星驰.get(i);
int i1 = integer.intValue();
周.put(integer,map.get(i1));
}
System.out.println("周星驰:");
Set<Integer> set = 周.keySet();
for (Integer st : set) {
System.out.print("\t"+周.get(st));
}
TreeMap<Integer, String> 周1 = new TreeMap<>();
for (int i = 0; i < 17; i++) {
Integer integer = 周润发.get(i);
int i1 = integer.intValue();
周1.put(integer, map.get(i1));
}
System.out.println("\n周润发:");
Set<Integer> set1 = 周1.keySet();
for (Integer st : set1) {
System.out.print("\t" + 周1.get(st));
}
TreeMap<Integer, String> 刘 = new TreeMap<>();
for (int i = 0; i < 17; i++) {
Integer integer = 刘德华.get(i);
int i1 = integer.intValue();
刘.put(integer, map.get(i1));
}
System.out.println("\n刘德华:");
Set<Integer> set2 = 刘.keySet();
for (Integer st : set2) {
System.out.print("\t" + 刘.get(st));
}
System.out.println("\n底牌:");
for (Integer integer : 底牌) {
System.out.print("\t"+map.get(integer.intValue()));
}
}
}