人生苦短我学Java-14-HashSet/Map等实现类
一、HashSet
1.特点:
- 1.无序
- 2.元素唯一
- 3.无索引
2.常用方法:和set方法一样
3.遍历:
- 1.转数组
- 2.迭代器
- 3.增强for
重写 hashCode、equals是的去重,否则不会对自定义类去重
public class MyHashSet { public static void main(String[] args) { // 创建集合 HashSeth = new HashSet (); // 添加元素 h.add(3); h.add(1); h.add(2); // 遍历 for (Integer i : h) { System.out.println(i); } HashSet p = new HashSet (); p.add(new Pig("ppl", 18)); p.add(new Pig("gsxl", 19)); p.add(new Pig("test", 18)); p.add(new Pig("ppl", 18)); for (Pig hp : p) { System.out.println(hp.getName() +":"+ hp.getAge()); } } } class Pig { private String name; private int age; public Pig() { } public Pig(String name, int age) { super(); 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; } // 重写 hashCode、equals是的去重,否则不会对自定义类去重 @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pig pig = (Pig) o; return age == pig.age && Objects.equals(name, pig.name); } @Override public int hashCode() { return Objects.hash(name, age); } }
运行结果:
4.底层结构如下:
二、Map
1.Map 接口
- -- HashMap 实现类
- -- TreeMap 实现类
- -- LinkedHashMap 实现类
- -- Hashtable 实现类
- -- Properties 实现类
特点
- 1.无序的
- 2.无索引
- 3.元素唯一的,键唯一,值可以重复
- 4.双列的,key:value
2.Map
主要方法:
- put:新增/修改元素
- get:获取key的value
- remove:移除
- size:map长度
- entrySet:key,value集合
class CustomMap { public static void main(String[] args) { // 创建集合 Mapmap = new HashMap<>(); // 添加元素,put:既是添加,相同key添加也可以是修改 map.put("ppl", "18"); map.put("gsxl", "19"); System.out.println(map); map.put("ppl", "22"); map.put("ppl2", "222"); System.out.println(map); // get值 System.out.println(map.get("ppl")); System.out.println(map.get("不存在呢")); // 不存在呢 返回null // remove:返回value String re = map.remove("gsxl"); System.out.println(re); System.out.println(map); // map长度 Integer size = map.size(); System.out.println(size); // 判断map中是否有指定的key boolean b = map.containsKey("ppl"); boolean b1 = map.containsKey("ppl1"); System.out.println(b); System.out.println(b1); // 判断map中是否有指定的value boolean b2 = map.containsValue("22"); boolean b3 = map.containsValue("23"); System.out.println(b2); System.out.println(b3); // 获取map中所有的key Set set = map.keySet(); for (String key : set) { // 获取所有value String value = map.get(key); System.out.println("key:" + key + " value:" + value); } //获取map中所有的value Collection c = map.values(); for (String s : c) { System.out.println(s); } // Entry 直接获取key与value Set > set1 = map.entrySet(); for (Map.Entry entry : set1) { String key = entry.getKey(); String value = entry.getValue(); System.out.println("key:" + key + " value:" + value); } } }
3.HashMap
特点和方法,与Map集合中的特点、方法一样。
class CustomHashMap { public static void main(String[] args) { HashMapmap = new HashMap<>(); // 添加元素 map.put("ppl", "广深小龙"); map.put("gsxl", "小龙龙"); // get String ppl = map.get("ppl"); // 遍历 Set > set = map.entrySet(); for (Map.Entry entry : set) { String key = entry.getKey(); String value = entry.getValue(); System.out.println("key:" + key + " value:" + value); } // remove map.remove("ppl"); System.out.println(map.get("ppl")); } }
4.TreeMap
特点在map上多了:
- 可以排序
- 数据结构是:红黑树,TreeSet底层使用的就是TreeMap
方法和Map集合中的方法一样。
自定义类排序报错Comparable处理
1.key类需重写 compareTo 方法
2.new TreeMap 时重写 Comparator 方法
class CustomTreeMap { public static void main(String[] args) { TreeMapmap = new TreeMap<>(); // 添加元素 map.put(new MyMap("tom", 18), "5566"); map.put(new MyMap("jerry", 19), "5565"); map.put(new MyMap("tom", 18), "5568"); map.put(new MyMap("rose", 17), "5567"); map.put(new MyMap("tony", 19), "5569"); // 遍历1 Set set = map.keySet(); for (MyMap key : set) { String value = map.get(key); System.out.println(key.getName() + " " + key.getAge() + " " + value); } /* --报错Comparable处理 1.key类需重写 compareTo 方法 2.new TreeMap 时重写 Comparator 方法,如下 */ // new TreeMap 时重写 Comparator 方法,如下 TreeMap treeMap = new TreeMap<>( new Comparator () { @Override public int compare(MyMap o1, MyMap o2) { // 比较规则,年龄从大到小 int i = o2.getAge() - o1.getAge(); // 比较规则,如果年龄相同,姓名从大到小 int ii = i == 0 ? o2.getName().compareTo(o1.getName()) : i; return ii; } } ); } }
自定义类重写 compareTo
@Override public int compareTo(MyMap o) { // 比较规则,年龄从大到小 int i = o.age - this.age; // 比较规则,如果年龄相同,姓名从大到小 int ii = i == 0 ? o.name.compareTo(this.name) : i; return ii; }
5.LinkedHashMap
和TreeMap特点/方法一致
class CustomLinkedHashMap { public static void main(String[] args) { // 创建集合 LinkedHashMapmap = new LinkedHashMap<>(); map.put("a", "1"); map.put("c", "2"); map.put("b", "3"); Set set = map.keySet(); for (String key : set) { String value = map.get(key); System.out.println("key:" + key + " value:" + value); } } }
6.Hashtable
key和value不可以是null,只有hashMap可以。
class CustomHashTable { public static void main(String[] args) { // 只有HashMap key和value可以是null HashMaphashMap=new HashMap<>(); hashMap.put("ppl",5566); hashMap.put("gsxl",null); // Hashtable、TerryMap key和value不可以是null Hashtable hashtable = new Hashtable<>(); hashtable.put("ppl", 5566); System.out.println(hashtable.get("ppl")); hashtable.put("gsxl", null); // 空指针异常 } }