重庆做网站最好的,免费.net网站空间,学做粤菜的网站有哪些,怎样维护网站建设双列集合
特点#xff1a;
双列集合一次需要存一对数据#xff0c;分别为键和值键不能重复#xff0c;值可以重复键和值是一一对应的#xff0c;每个键只能找到自己对应的值键值这个整体 称为#xff1a;键值对 键值对对象 Entry对象
Map集合的常用方法
public class …双列集合
特点
双列集合一次需要存一对数据分别为键和值键不能重复值可以重复键和值是一一对应的每个键只能找到自己对应的值键值这个整体 称为键值对 键值对对象 Entry对象
Map集合的常用方法
public class MapDemo1 {public static void main(String[] args) {MapString,String map new HashMap();//调用put添加元素 put添加|覆盖map.put(王老吉,凉茶);map.put(加多宝,凉茶);map.put(脉动,功能型饮料);map.put(可口可乐,碳酸饮料);map.put(特仑苏,纯牛奶);String ValueRet map.put(特仑苏,纯牛马); //输出System.out.println(ValueRet); //纯牛奶//删除String Ret map.remove(加多宝);System.out.println(Ret); //凉茶System.out.println(map);//{脉动功能型饮料, 特仑苏纯牛马, 王老吉凉茶, 可口可乐碳酸饮料}//获取集合长度int length map.size();System.out.println(length); //4//查找指定的键是否存在boolean a map.containsKey(可口可乐);System.out.println(a); //trueboolean b map.containsKey(雪碧);System.out.println(b); //false//查找指定的值是否存在boolean c map.containsValue(纯牛马);System.out.println(c); //trueboolean d map.containsValue(QQ糖);System.out.println(d); //false//判断集合是否为空boolean e map.isEmpty();System.out.println(e); //false}
}Map集合的遍历方式
1.键找值
public class MapDemo2 {public static void main(String[] args) {//创建HashMap集合MapString,String map new HashMap();//添加数据map.put(tom,汤姆);map.put(peter,彼得);map.put(jack,杰克);map.put(smith,史密斯);//Map集合的第一种遍历方式 键找值// 调用 map.keySet() 获取所有的键 SetString keys map.keySet();//遍历单列集合 得到每一个键System.out.println(迭代器);IteratorString it keys.iterator(); //获取单列集合迭代器while (it.hasNext()){String s it.next();System.out.println(keys\tvaluemap.get(s)); //map.get(s):通过传入s(键)返回对应的值}System.out.println(增强for);for (String key : keys) {String value map.get(key);System.out.println(键key\t值value);}System.out.println(lambda);keys.forEach( s-System.out.println(smap.get(s)));}
}
2.键值对
public class MapDemo3 {public static void main(String[] args) {//Map集合的第二种遍历方式 键值对MapString,String map new HashMap();map.put(tom,汤姆);map.put(peter,彼得);map.put(jack,杰克);map.put(smith,史密斯);//通过一个方法 map.entrySet() 获取所有的键值对对象返回一个set集合SetMap.EntryString, String entries map.entrySet();//遍历entries这个集合去得到里面每个键值对对象for (Map.EntryString, String entry : entries) {//利用entry调用get方法获取键和值String key entry.getKey();String value entry.getValue();System.out.println(键key 值value);}IteratorMap.EntryString, String it entries.iterator();while (it.hasNext()){Map.EntryString, String s it.next();System.out.println(s.getKey()s.getValue());}System.out.println(lambda);entries.forEach(s - System.out.println(s.getKey()s.getValue()));}
}
3.lambda表达式
public class MapDemo4 {public static void main(String[] args) {MapString,String map new HashMap();map.put(tom,汤姆);map.put(peter,彼得);map.put(jack,杰克);map.put(smith,史密斯);//利用lambda表达式进行遍历 map.forEachmap.forEach((String key, String value) - System.out.println(keyvalue));}
} HashMap
是Map里面的一个实现类没有额外需要学习的特有方法直接使用Map里面的方法就可以了特点都是由键决定的无序 不重复 无索引HashMap跟HashSet底层原理是一模一样的都是哈希表结构
总结
HashMap底层是哈希表结构的依赖hashCode方法和equals方法保证键的唯一如果键存储的是自定义对象需要重写hashCode和equals方法如果值存储自定义对象不需要重写hashCode和equals方法
HashMap测试题 1. 创建一个HashMap集合键是学生对象(Student)值是籍贯(String) 存储三个键值对元素并遍历要求:同姓名同年龄认为是同一个学生
public class HashMapDemo1 {public static void main(String[] args) {MapStudent3, String map new HashMap();Student3 s1 new Student3(zhangsan, 18);Student3 s2 new Student3(lisi, 19);Student3 s3 new Student3(wangwu, 17);Student3 s4 new Student3(zhangsan, 18);map.put(s1,娄底);map.put(s2,上海);map.put(s3,长沙);map.put(s4,昆明);SetStudent3 s map.keySet(); //获取键的单列集合IteratorStudent3 it s.iterator(); //获取单列集合的迭代器while(it.hasNext()){Student3 st it.next();System.out.println(stmap.get(st)); // map.get(st)通过键找值}}
}class Student3{private String name;private int age;public Student3(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;}Overridepublic String toString() {return Student3{ name name \ , age age };}Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;Student3 student3 (Student3) o;return age student3.age Objects.equals(name, student3.name);}Overridepublic int hashCode() {return Objects.hash(name, age);}
}
2. 某个班级80名学生现在需要组成秋游活动班长提供了四个景点依次是 (A、B、C、D), 每个学生只能选择一个景点请统计出最终哪个景点想去的人数最多。
public class HashMapDemo2 {public static void main(String[] args) {//定义一个数组 存储四个景点String arr[] {A,B,C,D};//利用随机数模拟80个学生的投票 并把投票的结果存储起来ArrayListString list new ArrayList();Random r new Random();for (int i 0; i 80; i) {int index r.nextInt(arr.length); //随机范围为0-3list.add(arr[index]); //将投票景点存入list集合中}HashMapString, Integer hm new HashMap();for (String name : list) {//判断当前的景点在map当中是否存在if (hm.containsKey(name)){//存在//先获取当前景点已经被投票的次数int count hm.get(name);//表示当前景点又被添加了一次count;//把新的次数再次添加到集合中hm.put(name,count);}else {//不存在hm.put(name,1);}}System.out.println(hm);//求最大值int max 0;SetMap.EntryString, Integer entries hm.entrySet();for (Map.EntryString, Integer entry : entries) {int count entry.getValue(); //如果if (count max){max count;}}System.out.println(最大值是max);//判断哪个景点和最大值的次数一样如果一样则打印出来for (Map.EntryString, Integer entry : entries) {if (entry.getValue() max){System.out.println(被选择最多次数的景点是entry.getKey());}}}
}LinkedHashMap
由键决定有序 不重复 无索引
有序指的是保证存储和取出的元素顺序一致
底层底层数据结构依然是哈希表只是每个键值对元素又额外的多了一个双向链表的机制记录存储的顺序
public class LinkedHashMap1 {public static void main(String[] args) {/*由键决定有序 不重复 无索引有序指的是保证存储和取出的元素顺序一致底层底层数据结构依然是哈希表只是每个键值对元素又额外的多了一个双向链表的机制记录存储的顺序*///创建集合MapString,Integer lhm new LinkedHashMap();//添加元素lhm.put(tom,11);lhm.put(tom,111);lhm.put(jack,12);lhm.put(peter,13);System.out.println(lhm); //打印集合有序不重复//{tom111, jack12, peter13} tom111: put方法同样有两个功能 添加/覆盖}
} TreeMap
TreeMap跟TreeSet底层原理一样都是红黑树结构的
由键决定特性 不重复 无索引 可排序
可排序对键进行排序
注意默认按照键的从小到大进行排序也可以自己规定键的排序规则
两种排序规则
实现Comparable接口指定比较规则
public class TreeMapDemo1 {public static void main(String[] args) {/*需求1:整数表示id键:值:字符串表示商品名称要求:按照id的升序排列、按照id的降序排列*/MapInteger,String tm new TreeMap(new ComparatorInteger() {Overridepublic int compare(Integer o1, Integer o2) {//o1:表示当前要添加的元素//o2:表示已经在红黑树中存在的元素//return o1 - o2; 升序return o2 - o1;}});tm.put(1,QQ糖);tm.put(3,AD钙);tm.put(2,旺仔牛奶);tm.put(2,忘崽牛奶);System.out.println(tm); //按照键的大小排序//输出{3AD钙, 2忘崽牛奶, 1QQ糖}}
}
创建集合时传递Comparator比较器对象指定比较规则
import java.util.*;/*** author hyk~*/
public class TreeMapDemo2 {public static void main(String[] args) {/*需求2:键:学生对象值:籍贯要求:按照学生年龄的升序排列年龄一样按照姓名的字母排列同姓名年龄视为同一个人。*/Student5 s1 new Student5(jack,10);Student5 s2 new Student5(tom,15);Student5 s3 new Student5(peter,14);Student5 s4 new Student5(jack,10);TreeMapStudent5, String tm new TreeMap();tm.put(s1,长沙);tm.put(s2,怀化);tm.put(s3,娄底);tm.put(s4,上海);for (Map.EntryStudent5, String entry : tm.entrySet()) {Student5 key entry.getKey();String value entry.getValue();System.out.println(key value);}}
}class Student5 implements ComparableStudent5{private String name;private int age;public Student5(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;}Overridepublic String toString() {return Student5{ name name \ , age age };}Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;Student5 student5 (Student5) o;return age student5.age Objects.equals(name, student5.name);}Overridepublic int hashCode() {return Objects.hash(name, age);}Overridepublic int compareTo(Student5 o) {/*this: 表示当前要添加的元素o: 表示已经在红黑树中存在的元素如果返回是负数要添加的元素是小的 存左边如果返回是正数要添加的元素是大的 存右边如果返回是 0 要添加的元素已经存在不存入*/int num this.getAge() - o.getAge();//如果年龄相差为0 则说明年龄一样 按名字排序num num 0 ? this.getName().compareTo(o.getName()) : num;return num;}
}
测试题
public class TreeMapDemo3 {/*需求:字符串 aababcabcdabcde 请统计字符串中每一个字符出现的次数并按照以下格式输出输出结果:a(5) b(4)c(3) d(2) e(1)统计思想利用map集合进行统计如果没有要求对结果进行排序默认使用HashMap(效率高)如果要求对结果进行排序使用TreeMap(可排序)*/public static void main(String[] args) {//定义字符串String str aababcabcdabcde;//创建集合MapCharacter,Integer tm new TreeMap();for (int i 0; i str.length(); i) {char c str.charAt(i);//charAt() 方法是 String 类的一个成员方法// 用于获取字符串中指定位置的字符。// 它的作用是返回字符串中指定索引位置处的字符。if (tm.containsKey(c)){//存在 表示当前字符又出现了一次int count tm.get(c); //tm.get(c):返回的是值count;tm.put(c,count);}else{//不存在 表示当前字符是第一次出现 直接添加tm.put(c,1);}}tm.forEach((Character character, Integer integer)- System.out.print(character (integer) ));//a(5) b(4) c(3) d(2) e(1) }
}