Map接口常用的子类:
HashMap: 无序存放的,是新的操作类,key值不允许重复
Hashtable: 无序存放的,是旧的操作类,key值不允许重复
TreeMap: 可以排序的Map集合,按集合中的key排序,key不允许重复
IdentityHashMap: key可以重复的Map集合
HashMap:
package com.map; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class HashMapDemo01 { public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("ZY", "www.baidu.com"); map.put("YQ", "www.google.com"); map.put("LS", "www.360.com"); /*String str = map.get("YQ"); System.out.println(str);*/ System.out.println(map.get("YQ")); if(map.containsKey("YQ")){ System.out.println("搜索的key存在!"); }else{ System.out.println("搜索的key不存在!"); } if(map.containsValue("www.google.com")){ System.out.println("搜索的Value存在!"); }else{ System.out.println("搜索的Value不存在!"); } Set<String> keys = map.keySet(); Iterator<String> iter1 = keys.iterator(); System.out.printf("输出所有的key:"); while(iter1.hasNext()){ System.out.printf(iter1.next()+"、"); } Collection<String> values = map.values(); Iterator<String> iter2 = values.iterator(); System.out.print("\n输出所有的values:"); while(iter2.hasNext()){ System.out.printf(iter2.next()+"、"); } } }
从运行结果可以看出存放数据的时候没有进行排序
www.google.com 搜索的key存在! 搜索的Value存在! 输出所有的key:ZY、LS、YQ、 输出所有的values:www.baidu.com、www.360.com、www.google.com、
Hashtable:
package com.map; import java.util.Collection; import java.util.Hashtable; import java.util.Iterator; import java.util.Map; import java.util.Set; public class HashTableDemo01 { public static void main(String[] args) { Map<String,String> map = new Hashtable<String,String>(); map.put("ZY", "www.baidu.com"); map.put("YQ", "www.google.com"); map.put("LS", "www.360.com"); System.out.println(map.get("YQ")); if(map.containsKey("YQ")){ System.out.println("搜索的key存在!"); }else{ System.out.println("搜索的key不存在!"); } if(map.containsValue("www.google.com")){ System.out.println("搜索的value存在!"); }else{ System.out.println("搜索的value不存在!"); } Set<String> keys = map.keySet(); Iterator<String> iter = keys.iterator(); System.out.printf("输出所有的keys:"); while(iter.hasNext()){ System.out.printf(iter.next()+"、"); } Collection<String> values = map.values(); Iterator<String> iter2 = values.iterator(); System.out.printf("\n输出所有的values:"); while(iter2.hasNext()){ System.out.printf(iter2.next()+"、"); } } }
从运行结果可以看出存放数据的时候没有进行排序
www.google.com 搜索的key存在! 搜索的value存在! 输出所有的keys:LS、ZY、YQ、 输出所有的values:www.360.com、www.baidu.com、www.google.com、
HashMap与Hashtable的区别:
序号 | 比较点 | ArrayList | Vector |
1 | 推出时间 | JDk1.2之后推出,属于新的操作类 | JDK1.时推出的,属于旧的操作类 |
2 | 性能 | 采用异步处理方式,性能更高 | 采用同步处理方式,性能更低 |
3 | 线程安全 | 属于非线程安全的操作类 | 属于线程安全的操作类 |
4 | 输出 | key或value允许保存null | key或value不允许保存null |
TreeMap:最终保存在Map中的数据是经过排序的数据,按其key排序
package com.map; import java.util.TreeMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class TreeMap { public static void main(String[] args) { Map<String,String> map = new TreeMap<String,String>(); map.put("ZY", "www.baidu.com"); map.put("YQ", "www.google.com"); map.put("LS", "www.360.com"); Set<String> keys = map.keySet(); Iterator<String> iter = keys.iterator(); while(iter.hasNext()){ String str = iter.next(); System.out.println(str+"-->"+map.get(str)); } } }
Iterator输出Map:
package com.map; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class IteratorMapDemo { public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); //声明Map对象,并实例化Map对象 map.put("ZY", "www.baidu.com"); //添加内容 map.put("YQ", "www.google.com"); map.put("LS", "www.360.com"); Set<Map.Entry<String, String>> allSet = map.entrySet(); //声明一个Set集合指定泛型,将Map接口实例变为Set接口实例 Iterator<Map.Entry<String, String>> iter = allSet.iterator(); //声明Iterator对象,并实例化Iterator while(iter.hasNext()){ Map.Entry<String, String> mp = iter.next(); //找到Map.Entry实例 System.out.println(mp.getKey()+"-->"+mp.getValue()); //输出key、value、 } } }
ZY-->www.baidu.com LS-->www.360.com YQ-->www.google.com
Foreach输出Map:将集合中的每个元素通过Map.Entry类型的对象进行所接收,然后进行key与value的分离
package com.map; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class ForeachMapDemo { public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("ZY", "www.baidu.com"); map.put("YQ", "www.google.com"); map.put("LS", "www.360.com"); for(Entry<String, String> mp:map.entrySet()){ System.out.println(mp.getKey()+"-->"+mp.getValue()); } } }
ZY-->www.baidu.com LS-->www.360.com YQ-->www.google.com
String-->Person的映射:
package com.map; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; class Person{ private String name; private int age; public Person(String name,int age){ this.name=name; this.age=age; } public String toString(){ return "name:"+this.name+","+"age:"+this.age; } } public class String_PersonDemo { public static void main(String[] args) { Map<String,Person> map = new HashMap<String,Person>(); map.put("YQ", new Person("QQ",20)); map.put("ZY", new Person("PP",30)); map.put("LS", new Person("HH",25)); System.out.println(map.get("YQ")); Set<String> keys = map.keySet(); Iterator<String> iter1 = keys.iterator(); System.out.println("输出所有的person:"); while(iter1.hasNext()){ String str = iter1.next(); System.out.println(map.get(str)+"、"); } } }
name:QQ,age:20 输出所有的person: name:PP,age:30、 name:HH,age:25、 name:QQ,age:20、
Person-->String的映射:
package com.map; import java.util.HashMap; import java.util.Map; class Person2{ private String name; private int age; public Person2(String name,int age){ this.name=name; this.age=age; } public String toString(){ return "name:"+this.name+","+"age"+this.age; } } public class Person_StringDemo { public static void main(String[] args) { Map<Person2,String> map =new HashMap<Person2,String>(); Person2 per = new Person2("YQ",20); map.put(per,"YQ"); System.out.println(map.get(per)); } }
YQ
package com.map; import java.util.HashMap; import java.util.Map; class Person3{ private String name; private int age; public Person3(String name,int age){ this.name=name; this.age=age; } public boolean equals(Object obj){ //覆写equals()方法 if(this==obj){ //判断地址是否相同 return true; //返回true表示同一对象 } if(!(obj instanceof Person3)){ //传递进来的不是本类的对象 return false; //返回false表示不是同一类对象 } Person3 p = (Person3) obj; //向下转型 if(this.name.equals(p.name) && this.age==p.age){//属性依次比较,相等返回true return true; }else{ return false; //属性内容不相等返回false } } public int hashCode(){ //覆写hashCode()方法 return this.name.hashCode()*this.age; //计算公式 } public String toString(){ //覆写toString()方法 return "name:"+this.name+","+"age"+this.age;//返回信息 } } public class Person_StringDemo02 { public static void main(String[] args) { Map<Person3,String> map =new HashMap<Person3,String>(); Person3 per = new Person3("YQ",20); map.put(per,"YQ"); System.out.println(map.get(per)); } }
YQ
IdentityHashMap:key值可以重复
package com.maps; //key值可以重复的Map集合IdentityHashMap import java.util.IdentityHashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; class Person{ private String name; private int age; public Person(String name,int age){ this.name=name; this.age=age; } public boolean equals(Object obj){ if(this==obj){ return true; } if(!(obj instanceof Person)){ return false; } Person p = (Person) obj; if(this.name.equals(p.name)&&this.age==p.age){ return true; }else{ return false; } } public int hashCode(){ return this.name.hashCode()*this.age; } public String toString(){ return "name:"+this.name+","+"age"+this.age; } } public class IdentityHashMapdemo01 { public static void main(String[] args) { Map<Person,String> map =new IdentityHashMap<Person,String>();//只要地址不相同,Key1!=key2,就表示不是相同的key /*Map<Person,String> map =new HashMap<Person,String>(); HashMap(),key1=key2则会被覆盖*/ map.put(new Person("YQ",20),"1"); map.put(new Person("YQ",20),"2"); map.put(new Person("QQ",22),"3"); map.put(new Person("PP",20),"4"); Set<Map.Entry<Person, String>> allSet = map.entrySet(); Iterator<Map.Entry<Person, String>> iter = allSet.iterator(); while(iter.hasNext()){ Map.Entry<Person, String> p = iter.next(); System.out.println(p.getKey()+","+p.getValue()); } } }
name:QQ,age22,3 name:YQ,age20,2 name:PP,age20,4 name:YQ,age20,1
认真看过此篇的小伙伴,如果对你有收获,请点击旁边的小手推荐一下,如果有误,欢迎指正,谢谢!
版权声明:此篇为本人原创,转载请标明出处https://www.cnblogs.com/YQian/p/10880654.html
我的博客园地址:https://www.cnblogs.com/YQian/
原文地址:https://www.cnblogs.com/YQian/p/10880654.html
时间: 2024-10-22 07:37:00