摄影作品网站排行榜,企事业网站建设,网站与微信,淄博百度网页设计Map
Map集合概述和特点
概述#xff1a; 将键映射到值的对象 一个映射不能包含重复的键 每个键最多只能映射到一个值 Map接口和Collection接口的不同 Map是双列的,Collection是单列的 Map的键唯一,Collection的子体系Set是唯一的 Map集合的数据结构针对键有效#xff0c;跟…Map
Map集合概述和特点
概述 将键映射到值的对象 一个映射不能包含重复的键 每个键最多只能映射到一个值 Map接口和Collection接口的不同 Map是双列的,Collection是单列的 Map的键唯一,Collection的子体系Set是唯一的 Map集合的数据结构针对键有效跟值无关;Collection集合的数据结构是针对元素有效
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:获取功能 SetMap.EntryK,V entrySet(): 返回一个键值对的Set集合 V get(Object key):根据键获取值 Set keySet():获取集合中所有键的集合 Collection values():获取集合中所有值的集合 e:长度功能 int size()返回集合中的键值对的对数
Map集合的遍历之键找值
获取所有键的集合 遍历键的集合获取到每一个键 根据键找值
public class Test4 {public static void main(String[] args) {HashMapPhone,String map new HashMap();map.put(new Phone(Apple,7000),美国);map.put(new Phone(Sony,5000),日本);map.put(new Phone(Huawei,6000),中国);SetPhone phones map.keySet();IteratorPhone iterator phones.iterator();while (iterator.hasNext()){Phone next iterator.next();System.out.println(next.getBrand()next.getPrice()map.get(next));}}
}
class Phone{private String Brand;private int Price;public Phone(String brand, int price) {Brand brand;Price price;}public String getBrand() {return Brand;}public void setBrand(String brand) {Brand brand;}public int getPrice() {return Price;}public void setPrice(int price) {Price price;}
}获取所有键值对对象的集合 遍历键值对对象的集合获取到每一个键值对对象 根据键值对对象找键和值
public class Test4 {public static void main(String[] args) {HashMapPhone,String map new HashMap();map.put(new Phone(Apple,7000),美国);map.put(new Phone(Sony,5000),日本);map.put(new Phone(Huawei,6000),中国);SetMap.EntryPhone, String entries map.entrySet();for (Map.EntryPhone, String entry : entries) {System.out.println(entry.getKey().getBrand()entry.getKey().getPrice()entry.getValue());}}
}一般来说建议使用entrySet遍历方式其效率高
LinkedHashMap的概述和使用
LinkedHashMap的概述: Map 接口的哈希表和链接列表实现具有可预知的迭代顺序LinkedHashMap的特点 底层的数据结构是链表和哈希表 元素有序 并且唯一 元素的有序性由链表数据结构保证 唯一性由 哈希表数据结构保证 Map集合的数据结构只和键有关
TreeMap集合
TreeMap 键不允许插入null TreeMap: 键的数据结构是红黑树,可保证键的排序和唯一性 排序分为自然排序和比较器排序 线程是不安全的效率比较高 TreeMap集合排序 实现Comparable接口重写CompareTo方法 使用比较器
TreeMap集合的遍历
public class Test4 {public static void main(String[] args) {TreeMapPhone,String map new TreeMap();map.put(new Phone(Apple,7000),美国);map.put(new Phone(Sony,5000),日本);map.put(new Phone(Huawei,6000),中国);SetPhone phones map.keySet();IteratorPhone iterator phones.iterator();while(iterator.hasNext()){Phone next iterator.next();System.out.println(next.getBrand()next.getPrice()map.get(next));}}
}
class Phone implements ComparablePhone{private String Brand;private int Price;public Phone(String brand, int price) {Brand brand;Price price;}public String getBrand() {return Brand;}public void setBrand(String brand) {Brand brand;}public int getPrice() {return Price;}public void setPrice(int price) {Price price;}Overridepublic int compareTo(Phone o) {return this.getPrice() - o.getPrice();}
}集合嵌套之HashMap嵌套HashMap
基础班 张三 20 李四 22 就业班 王五 21 赵六 23
public class Test5 {public static void main(String[] args) {HashMapString, Integer map new HashMap();map.put(张三,20);map.put(李四,22);HashMapString, Integer map1 new HashMap();map1.put(王五,21);map1.put(赵六,23);HashMapString, HashMapString, Integer mapmax new HashMap();mapmax.put(基础班,map);mapmax.put(就业班,map1);SetMap.EntryString, HashMapString, Integer entries mapmax.entrySet();for (Map.EntryString, HashMapString, Integer entry : entries) {System.out.println(entry.getKey());SetMap.EntryString, Integer entries1 entry.getValue().entrySet();for (Map.EntryString, Integer stringIntegerEntry : entries1) {System.out.println( stringIntegerEntry.getKey() stringIntegerEntry.getValue());}}}
}Arraylist嵌套HashMap
public class Test3 {public static void main(String[] args) {HashMapString, String map new HashMap();map.put(周瑜,小乔);map.put(吕布,貂蝉);HashMapString, String map1 new HashMap();map1.put(郭靖,黄蓉);map1.put(杨过,小龙女);HashMapString, String map2 new HashMap();map2.put(令狐冲,任盈盈);map2.put(林平之,岳灵珊);ArrayListHashMapString, String list new ArrayList();list.add(map);list.add(map1);list.add(map2);for (HashMapString, String s : list) {SetString strings s.keySet();for (String s1 : strings) {String s2 s.get(s1);System.out.println( s1---s2);}System.out.println();}}
}Collections工具类的概述和常见方法
A:Collections类概述: 针对集合操作 的工具类 B: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 Test4 {public static void main(String[] args) {//A://案例演示://模拟斗地主洗牌和发牌牌没有排序//得有一副牌//生成54张牌放到牌盒里面String[] colors {?, ?, ?, ?};String[] nums {3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A, 2};HashMapInteger, String map new HashMapInteger, String();ArrayListInteger list new ArrayList();int index 0;for (String num : nums) {for (String color : colors) {map.put(index, color.concat(num));list.add(index);index;}}map.put(index, ☆);list.add(index);index;map.put(index, ★);list.add(index);//洗牌Collections.shuffle(list);//发牌TreeSetInteger 高进 new TreeSet();TreeSetInteger 刀仔 new TreeSet();TreeSetInteger 星仔 new TreeSet();TreeSetInteger 底牌 new TreeSet();// 高进 (ArrayListString) pokerBox.subList(0,17);// 高进 0 3 6 9//刀仔 1 4 7 10// 星仔 2 5 8 11for (int i 0; i list.size(); i) {if (i list.size() - 3) {底牌.add(list.get(i));} else if (i % 3 0) {高进.add(list.get(i));} else if (i % 3 1) {刀仔.add(list.get(i));} else {星仔.add(list.get(i));}}//看牌lookPoker(map,高进,高进);lookPoker(map,刀仔,刀仔);lookPoker(map,星仔,星仔);lookPoker(map,底牌,底牌);}public static void lookPoker(HashMapInteger, String map, TreeSetInteger list, String name) {System.out.println(name);for (Integer s : list) {System.out.print(map.get(s));}System.out.println();}
}Map中的键唯一但是当存储自定义对象时需要重写Hashcode和equals方法