如上图,java中的Map与collection接口不同,map中的元素由键值对(key,value)组成。
Map接口中键和值一一映射. 可以通过键来获取值。
- 给定一个键和一个值,你可以将该值存储在一个Map对象. 之后,你可以通过键来访问对应的值。
- 当访问的值不存在的时候,方法就会抛出一个NoSuchElementException异常.
- 当对象的类型和Map里元素类型不兼容的时候,就会抛出一个 ClassCastException异常。
- 当在不允许使用Null对象的Map中使用Null对象,会抛出一个NullPointerException 异常。
- 当尝试修改一个只读的Map时,会抛出一个UnsupportedOperationException异常。
Map
以按键
/
数值对的形式存储数据
,这里要特别说明(
Map.Entry
,是
Map
的内部类,它用来描述
Map
中的键
/
值对)。
Map
是一个接口,我们平时多用它的实现类
HashMap
。
1.key是用Set来存放的,不可重复,value是用Collection来存放的,可重复
一个key-value对,是一个Entry,所有的Entry是用Set存放的,也是不可重复的。
2.向HashMap中添加元素时,会调用key所在类的equals()方法,判断两个key是否相同,若相同则只能够添加进一个,则只能添加进后加的那个元素
一个key-value对,是一个Entry,所有的Entry是用Set存放的,也是不可重复的。
2.向HashMap中添加元素时,会调用key所在类的equals()方法,判断两个key是否相同,若相同则只能够添加进一个,则只能添加进后加的那个元素
import java.util.*; public class TestMap{ public static void main(String[] args) { TestMap tm = new TestMap(); tm.test1(); tm.test2(); tm.testLinkedHashMap(); tm.testTreeMap(); } public void test1(){ System.out.println("***************Test1***************"); Map map = new HashMap(); map.put("AA",213); map.put("BB",456); //添加相同的key会覆盖原来的键值对 map.put("BB",45); map.put(123,"CC"); map.put(null,null); map.put(new Person("xx",21),89); map.put(new Person("xx",21),87);//与上边的key相同所以map中是这个 System.out.println(map.size()); map.remove("BB"); System.out.println(map); Object value = map.get(123); System.out.println(value); System.out.println("***************end***************"); } /* Map 遍历 1.Set keySet() 2.Collection values() 3.Set entrySet() */ public void test2(){ System.out.println("***************Test2***************"); Map map = new HashMap(); map.put("AA",213); map.put("BB",45); map.put(123,"CC"); map.put(null,null); map.put(new Person("xx",21),89); //1.使用keySet遍历 Set set = map.keySet(); for(Object obj : set){ System.out.println(obj); } //2.使用values遍历 Collection values = map.values(); Iterator i = values.iterator(); while(i.hasNext()){ System.out.println(i.next()); } //3.使用entrySet遍历 Set set2 = map.entrySet(); for(Object obj : set2 ){ Map.Entry entry = (Map.Entry)obj; //Map.Entry为Map的内部类 System.out.println(entry.getKey() + "---->" + entry.getValue()); } System.out.println("***************end***************"); } public void testLinkedHashMap(){ System.out.println("************TestLinkedHashMap*************"); Map map = new LinkedHashMap(); map.put("AA",213); map.put("BB",45); map.put(123,"CC"); map.put(null,null); map.put(new Person("xx",21),89); Set set1 = map.keySet(); for(Object obj1 : set1){ System.out.println(obj1 + "----->" + map.get(obj1)); } System.out.println("***************end***************"); } public void testTreeMap(){ System.out.println("************TestTreeMap*************"); //自然排序 Map map = new TreeMap(); map.put(new Person("AA",23),89); map.put(new Person("MM",22),79); map.put(new Person("BB",23),99); map.put(new Person("CC",13),69); Set set1 = map.keySet(); for(Object obj1 : set1){ System.out.println(obj1 + "----->" + map.get(obj1)); } System.out.println("***************end***************"); } class Person implements Comparable<Person>{ private String name; private int age; public Person(String name,int age){ this.name = name; this.age = age; } public String getName(){return this.name;} public int getAge(){return this.age;} @Override public boolean equals(Object o){ if(o instanceof Person){ Person p = (Person)o; return this.name.equals(p.name) && this.age==p.age; } else return false; } @Override public int hashCode() { return Objects.hash(name, age); } @Override public int compareTo(Person o) { // TODO Auto-generated method stub if(!this.name.equals(o.getName())) return this.name.compareTo(o.getName()); else return this.age - o.getAge(); } @Override public String toString() { // TODO Auto-generated method stub return name + "(" + age + ")"; } } }