网站如何被搜索到,广告模板免费,如需手机网站建设,便宜的网站制作From: https://www.cnblogs.com/douyu2580860/p/8358768.html --Map接口简介 今天来看一看map集合#xff0c;map映射接口#xff0c;用于存放键值对#xff0c;key,value#xff0c;通过key来查找value,顾名思义key不能为空#xff0c;唯一且不重复#xff0c;不…From: https://www.cnblogs.com/douyu2580860/p/8358768.html --Map接口简介 今天来看一看map集合map映射接口用于存放键值对key,value通过key来查找value,顾名思义key不能为空唯一且不重复不然底层怎么查呢 可以从图中看出Map为单独的接口他和Collection有什么区别呢
Map和Collection在集合中并列存在。 Map集合是双列的键值对而Collection是单列集合Map存储元素使用put方法Collection使用Put方法。Map遍历没有直接取出元素的方法而是先转成Set集合再通过迭代获取元素。--Map常用方法 --Map应用
添加使用HashMap。立了学生姓名和年龄之间的映射关系。并试图添加重复的键 public static void main(String[] args) {// 定义一个Map的容器对象 MapString, Integer map1 new HashMapString, Integer (); map1.put(jack, 20); map1.put(rose, 18); map1.put(lucy, 17); map1.put(java, 25);// map1.put(jack, 30) 在没有hashCode和equals方式 添加重复的键值值不同,会覆盖掉前面key值相同的值System.out.println(map1); MapString, Integer map2 new HashMapString, Integer(); map2.put(张三丰, 100); map2.put(虚竹, 20); System.out.println(map2: map2); // 从指定映射中将所有映射关系复制到此映射中。 map1.putAll(map2); System.out.println(map1: map1); } 删除: public static void main(String[] args) { // 删除 // remove() 删除关联对象指定key对象 // clear() 清空集合对象 MapString, Integer map1 new HashMapString, Integer(); map1.put(jack, 20); map1.put(rose, 18); map1.put(lucy, 17); map1.put(java, 25); System.out.println(map1); // 指定key返回删除的键值对映射的值。 map1.remove(java);System.out.println(map1); map1.clear(); System.out.println(map1: map1); } 获取: public static void main(String[] args) {// 获取 // V get(Object key) 通过指定的key对象获取value对象 // int size() 获取容器的大小 MapString, Integer map1 new HashMapString, Integer(); map1.put(jack, 20); map1.put(rose, 18); map1.put(lucy, 17); map1.put(java, 25); System.out.println(map1); // V get(Object key) 通过指定的key对象获取value对象 System.out.println(value: map1.get(jack)); // int size() 获取容器的大小System.out.println(map.size: map1.size()); } 判断: public static void main(String[] args) {// 判断 // boolean isEmpty() 判断集合是否为空 长度为0返回true否则false // boolean containsKey(Object key) 判断集合中是否包含指定的key // boolean containsValue(Object value) MapString, Integer map1 new HashMapString, Integer(); map1.put(jack, 20); map1.put(rose, 18); map1.put(lucy, 17); map1.put(java, 25); System.out.println(map1); System.out.println(isEmpty: map1.isEmpty()); System.out.println(containskey: map1.containsKey(jack)); System.out.println(containsvalues: map1.containsValue(100)); } 遍历Map的4中方式 第一种 public static void main(String[] args) {//遍历Map 第一种方式MapString, Integer map1 new HashMapString, Integer(); map1.put(jack, 20); map1.put(rose, 18); map1.put(lucy, 17); map1.put(java, 25); //通过 map1.keySet() 获取key 通过key 找到valuefor (String key : map1.keySet()) {Integer value map1.get(key);System.out.println(key : key value : value);}} 第二种 public static void main(String[] args) {//遍历Map 第二种方式MapString, Integer map1 new HashMapString, Integer(); map1.put(jack, 20); map1.put(rose, 18); map1.put(lucy, 17); map1.put(java, 25); //通过Map.Entry(String,Integer) 获取然后使用entry.getKey()获取到键通过entry.getValue()获取到值for(Map.EntryString, Integer entry : map1.entrySet()){System.out.println(键 key entry.getKey() 值value entry.getValue());}} 第三种 //遍历Map 第三种方式MapString, Integer map1 new HashMapString, Integer(); map1.put(jack, 20); map1.put(rose, 18); map1.put(lucy, 17); map1.put(java, 25); //第三种只遍历键或者值通过加强for循环for(String s1:map1.keySet()){//遍历map的键System.out.println(键key s1);}for(Integer s2:map1.values()){//遍历map的值System.out.println(值value s2);}System.out.println(); } 第四种 public static void main(String[] args) {//遍历Map 第一种方式MapString, Integer map1 new HashMapString, Integer(); map1.put(jack, 20); map1.put(rose, 18); map1.put(lucy, 17); map1.put(java, 25); //第四种Iterator遍历获取然后获取到Map.EntryString, String再得到getKey()和getValue()IteratorMap.EntryString, Integer itmap1.entrySet().iterator();while(it.hasNext()){Map.EntryString, Integer entryit.next(); System.out.println(键key entry.getKey() value entry.getValue());}} HashMap 底层是哈希表数据结构线程是不同步的可以存入null键null值。要保证键的唯一性需要覆盖hashCode方法和equals方法。案例自定义对象作为Map的键。 public class Demo3 { public static void main(String[] args) { HashMapPerson, String hm new HashMapPerson, String(); hm.put(new Person(jack, 20), 1001); hm.put(new Person(rose, 18), 1002); hm.put(new Person(lucy, 19), 1003); hm.put(new Person(hmm, 17), 1004); hm.put(new Person(ll, 25), 1005); System.out.println(hm); System.out.println(hm.put(new Person(rose, 18), 1006)); //重写hashCode和equalse后key相同不会覆盖SetEntryPerson, String entrySet hm.entrySet(); IteratorEntryPerson, String it entrySet.iterator(); while (it.hasNext()) { EntryPerson, String next it.next(); Person key next.getKey(); String value next.getValue(); System.out.println(key value); } } } class Person { private String name; private int age; Person() { } public Person(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; } Override public int hashCode() { return this.name.hashCode() age * 37; } Override public boolean equals(Object obj) { if (obj instanceof Person) { Person p (Person) obj; return this.name.equals(p.name) this.age p.age; } else { return false; } } Override public String toString() { return Personname: this.name age: this.age; } } } TreeMap TreeMap的排序TreeMap可以对集合中的键进行排序。如何实现键的排序 方式一元素自身具备比较性 和TreeSet一样原理需要让存储在键位置的对象实现Comparable接口重写compareTo方法也就是让元素自身具备比较性这种方式叫做 元素的自然排序也叫做默认排序。 方式二容器具备比较性 当元素自身不具备比较性或者自身具备的比较性不是所需要的。那么此时可以让容器自身具备。需要定义一个类实现接口Comparator重 写compare方法并将该接口的子类实例对象作为参数传递给TreeMap集合的构造方法。 注意当Comparable比较方式和Comparator比较方式同时存在时以Comparator的比较方式为主 注意在重写compareTo或者compare方法时必须要明确比较的主要条件相等时要比较次要条件。假设姓名和年龄一致的人为相同的人 如果想要对人按照年龄的大小来排序如果年龄相同的人需要如何处理不能直接return 0以为可能姓名不同年龄相同姓名不同的人 是不同的人。此时就需要进行次要条件判断需要判断姓名只有姓名和年龄同时相等的才可以返回0. 通过return 0来判断唯一性。 1 2 3 4 5 6 7 8 9 10 11 12 13 public class Demo4 { public static void main(String[] args) { TreeMapString, Integer tree new TreeMapString, Integer(); tree.put(张三, 19); tree.put(李四, 20); tree.put(王五, 21); tree.put(赵六, 22); tree.put(周七, 23); tree.put(张三, 24); System.out.println(tree); System.out.println(张三.compareTo(李四));//-2094 } }
自定义元素排序 public class Demo3 { public static void main(String[] args) { TreeMapPerson, String hm new TreeMapPerson, String( new MyComparator()); hm.put(new Person(jack, 20), 1001); hm.put(new Person(rose, 18), 1002); hm.put(new Person(lucy, 19), 1003); hm.put(new Person(hmm, 17), 1004); hm.put(new Person(ll, 25), 1005); System.out.println(hm); System.out.println(hm.put(new Person(rose, 18), 1006)); SetEntryPerson, String entrySet hm.entrySet(); IteratorEntryPerson, String it entrySet.iterator(); while (it.hasNext()) { EntryPerson, String next it.next(); Person key next.getKey(); String value next.getValue(); System.out.println(key value); } } } class MyComparator implements ComparatorPerson { Override public int compare(Person p1, Person p2) { if (p1.getAge() p2.getAge()) { return -1; } else if (p1.getAge() p2.getAge()) { return 1; } return p1.getName().compareTo(p2.getName()); } } class Person implements ComparablePerson { private String name; private int age; Person() { } public Person(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; } Override public int hashCode() { return this.name.hashCode() age * 37; } Override public boolean equals(Object obj) { if (obj instanceof Person) { Person p (Person) obj; return this.name.equals(p.name) this.age p.age; } else { return false; } } Override public String toString() { return Personname: this.name age: this.age; } Override public int compareTo(Person p) { if (this.age p.age) { return 1; } else if (this.age p.age) { return -1; } return this.name.compareTo(p.name); } } 注意Set的元素不可重复Map的键不可重复如果存入重复元素如何处理
Set元素重复元素不能存入add方法返回false
Map的重复健将覆盖旧键将旧值返回。 技术交流群海量学习资料免费获取备注来意就说博客上看到的 Q群289683917