响应式单页网站模板网络网站
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:获取功能
Set<Map.Entry<K,V>> entrySet(): 返回一个键值对的Set集合
V get(Object key):根据键获取值
Set keySet():获取集合中所有键的集合
Collection values():获取集合中所有值的集合
e:长度功能
int size():返回集合中的键值对的对数
Map集合的遍历之键找值
获取所有键的集合
遍历键的集合,获取到每一个键
根据键找值
public class Test4 {public static void main(String[] args) {HashMap<Phone,String> map = new HashMap<>();map.put(new Phone("Apple",7000),"美国");map.put(new Phone("Sony",5000),"日本");map.put(new Phone("Huawei",6000),"中国");Set<Phone> phones = map.keySet();Iterator<Phone> 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) {HashMap<Phone,String> map = new HashMap<>();map.put(new Phone("Apple",7000),"美国");map.put(new Phone("Sony",5000),"日本");map.put(new Phone("Huawei",6000),"中国");Set<Map.Entry<Phone, String>> entries = map.entrySet();for (Map.Entry<Phone, 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) {TreeMap<Phone,String> map = new TreeMap<>();map.put(new Phone("Apple",7000),"美国");map.put(new Phone("Sony",5000),"日本");map.put(new Phone("Huawei",6000),"中国");Set<Phone> phones = map.keySet();Iterator<Phone> iterator = phones.iterator();while(iterator.hasNext()){Phone next = iterator.next();System.out.println(next.getBrand()+"==="+next.getPrice()+"==="+map.get(next));}}
}
class Phone implements Comparable<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;}@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) {HashMap<String, Integer> map = new HashMap<>();map.put("张三",20);map.put("李四",22);HashMap<String, Integer> map1 = new HashMap<>();map1.put("王五",21);map1.put("赵六",23);HashMap<String, HashMap<String, Integer>> mapmax = new HashMap<>();mapmax.put("基础班",map);mapmax.put("就业班",map1);Set<Map.Entry<String, HashMap<String, Integer>>> entries = mapmax.entrySet();for (Map.Entry<String, HashMap<String, Integer>> entry : entries) {System.out.println(entry.getKey());Set<Map.Entry<String, Integer>> entries1 = entry.getValue().entrySet();for (Map.Entry<String, Integer> stringIntegerEntry : entries1) {System.out.println(" "+stringIntegerEntry.getKey()+" "+stringIntegerEntry.getValue());}}}
}
Arraylist嵌套HashMap
public class Test3 {public static void main(String[] args) {HashMap<String, String> map = new HashMap<>();map.put("周瑜","小乔");map.put("吕布","貂蝉");HashMap<String, String> map1 = new HashMap<>();map1.put("郭靖","黄蓉");map1.put("杨过","小龙女");HashMap<String, String> map2 = new HashMap<>();map2.put("令狐冲","任盈盈");map2.put("林平之","岳灵珊");ArrayList<HashMap<String, String>> list = new ArrayList<>();list.add(map);list.add(map1);list.add(map2);for (HashMap<String, String> s : list) {Set<String> 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"};HashMap<Integer, String> map = new HashMap<Integer, String>();ArrayList<Integer> 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);//发牌TreeSet<Integer> 高进 = new TreeSet<>();TreeSet<Integer> 刀仔 = new TreeSet<>();TreeSet<Integer> 星仔 = new TreeSet<>();TreeSet<Integer> 底牌 = new TreeSet<>();// 高进 = (ArrayList<String>) 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(HashMap<Integer, String> map, TreeSet<Integer> list, String name) {System.out.println(name);for (Integer s : list) {System.out.print(map.get(s));}System.out.println();}
}
Map中的键唯一,但是当存储自定义对象时,需要重写Hashcode和equals方法