城管希课堂之Map集合

Map集合 双列集合

1.是以 键值对 形式来保存数据 key ---value

2.键的值 唯一(不重复)

HashMap 和 HashSet 有什么关系

HashSet是依赖Map来存储数据的

Set在保存数据的时候 实际上就是向 Map中的 key这一列 存数据

为什么要这么设计? ? ?

内部是哈希算法 ---- 面向对象的设计


HashMap:去重 指的是 key这列

TreeMap:排序 指的是 key这列

HashMap map = new HashMap<>();map.put("彭前", 13);map.put("彭后", 12);map.put("彭左", 11);map.put("彭右", 15);//判断包含keyboolean b1 = map.containsKey("彭中");System.out.println(b1);//判断包含valueboolean b2 = map.containsValue(15);System.out.println(b2);//获取所有key的所有Set集合Set keySet = map.keySet();System.out.println(keySet);//获取Map中所有value值的collection集合		Collection values = map.values();System.out.println(values);//根据key删除整个键值对Integer remove = map.remove("彭前");System.out.println(remove);System.out.println(map.toString());//清空mapmap.clear();System.out.println(map);


//Entry接口 是Map接口中的内部对象//Entry中保存的是 键值对对象//相当于把 map中的 key和value 封装成一个对象HashMap map = new HashMap<>();map.put("彭前", 13);map.put("彭后", 12);map.put("彭左", 11);map.put("彭右", 15);//利用entrySet遍历集合(迭代器)//增强for循环//获取entrySet集合Set> entrySet = map.entrySet();//获取迭代器Iterator> iterator = entrySet.iterator();while (iterator.hasNext()) {Entry entry = iterator.next();Integer value = entry.getValue();String key = entry.getKey();System.out.println(key+"="+value);}System.out.println("------华丽的分割线------");for (Entry entry : entrySet) {System.out.println(entry);}

斗地主例子:

public class Demo04 {public static void main(String[] args) {TreeMap map = new TreeMap<>();ArrayList list = new ArrayList<>();int num = 0;String[] strings = { "❤️", "♠️", "♦️", "♣️" };String[] strings2 = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" ,"A","2"};for (int i = 0; i < strings2.length; i++) {for (int j = 0; j < strings.length; j++) {String s=strings[j]+strings2[i];//放到map中map.put(num,s);//放到洗牌的 list中list.add(num);num++;}}map.put(num, "小


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部