Map集合
在之前的Collection接口下有List接口和Set接口,但是咱们没有说有Map接口,那是因为Map集合其实和Collection集合没有直接关系,只不过Map集合也可以存放对象,所以如果要学习Map集合则肯定不和Collection集合一样,需要咱们学习更多的知识点。
Map接口的常用子类
1、HashMap:无序存放的,是新的操作类,key不允许重复。
2、Hashtable:无序存放的,是旧的操作类,key不允许重复。
3、TreeMap:可以排序的Map集合,按集合中的key排序,key不允许重复。
HashMap
HashMap本身是Map的子类,直接使用此类为Map接口实例化即可;
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable |
创建一个HashMap类的对象:
public static void main(String[] args) { //创建HashMap类的对象 HashMap<Integer,String> hashMap=new HashMap<Integer,String>(); System.out.println(hashMap); } |
{} |
此时输出HashMap类的对象的时候输出了一个”{}”,”{}”代表的是能够存储更加复杂内容。
向集合中添加对象:
public V put(K key, V value){} |
public static void main(String[] args) { //创建HashMap类的对象 HashMap<Integer,String> hashMap=new HashMap<Integer,String>(); //添加内容 hashMap.put(1,"sram"); hashMap.put(2,"edu"); hashMap.put(3,"com"); System.out.println(hashMap); } |
{1=sram, 2=edu, 3=com} |
在Collection集合中添加内容是使用addXXX()方法,但是在Map集合中添加内容是使用putXXX(K,V)方法,从Map集合中获取值是通过集合中的get(K),同键来获取值。
在Map集合中是通过key值获取对应的内容的。
public V get(Object key){} |
案例:获取Map集合中的key为2的对应的值
public static void main(String[] args) { //创建HashMap类的对象 HashMap<Integer,String> hashMap=new HashMap<Integer,String>(); //添加内容 hashMap.put(1,"sram"); hashMap.put(2,"edu"); hashMap.put(3,"com"); System.out.println(hashMap); //获取key为2的对应的值 System.out.println(hashMap.get(2)); } |
{1=sram, 2=edu, 3=com} edu |
获取Map集合中全部的键(key):
public Set<K> keySet(){} |
此方法是获取Map集合中全部的key的,返回的一个结果是Set集合。
public static void main(String[] args) { //创建HashMap类的对象 HashMap<Integer,String> hashMap=new HashMap<Integer,String>(); //添加内容 hashMap.put(11,"sram"); hashMap.put(22,"edu"); hashMap.put(33,"com"); System.out.println(hashMap); //获取集合中全部key Set<Integer> keys=hashMap.keySet(); for(Integer key:keys){ System.out.println(key); System.out.println(hashMap.get(key));//通过key值获取集合中的value值 } } |
{33=com, 22=edu, 11=sram} 33 com 22 edu 11 sram |
获取Map集合中的所有value值
public Collection<V> values(){} |
案例:
public static void main(String[] args) { //创建HashMap类的对象 HashMap<Integer,String> hashMap=new HashMap<Integer,String>(); //添加内容 hashMap.put(11,"sram"); hashMap.put(22,"edu"); hashMap.put(33,"com"); System.out.println(hashMap); //获取全部的value值 Collection<String> list=hashMap.values();//注意不能向下转型 for (String value : list) { System.out.println(value); } } |
{33=com, 22=edu, 11=sram} com edu sram |
判断指定的key是否存在:
public boolean containsKey(Object key){} |
案例:判断num2这个key是哦福在Map集合中存在
public static void main(String[] args) { //创建HashMap类的对象 HashMap<Integer,String> hashMap=new HashMap<Integer,String>(); //添加内容 hashMap.put(11,"sram"); hashMap.put(22,"edu"); hashMap.put(33,"com"); System.out.println("集合中包含44这个key(键)?"+hashMap.containsKey(44)); } |
集合中包含44这个key(键)?false |
判断指定的value是否存在:
public boolean containsValue(Object value){} |
案例:
public static void main(String[] args) { //创建HashMap类的对象 HashMap<Integer,String> hashMap=new HashMap<Integer,String>(); //添加内容 hashMap.put(11,"sram"); hashMap.put(22,"edu"); hashMap.put(33,"com"); System.out.println("集合中包含com这个value(值)?"+hashMap.containsValue("com")); } |
集合中包含com这个value(值)?true |
TreeMap
TreeSet在存放对象的时候需要给对象所属的类实现Comparable接口,那么咱们TreeMap是否也需要呢?
添加内容到TreeMap中:
public static void main(String[] args) { //创建TreeMap类的对象 TreeMap<String,String> treeMap=new TreeMap<String,String>(); //向集合中添加内容 treeMap.put("num2", "edu"); treeMap.put("num3", "com"); treeMap.put("num4", "sram"); treeMap.put("num1", "sram"); System.out.println(treeMap); } |
{num1=sram, num2=edu, num3=com, num4=sram} |
发现TreeMap是可以进行排序的,但是它的排序是给Map集合中的key(键)进行排序,和value(值)没关系。
所以在以后使用自定义的类当做Map集合的key的时候需要让此类实现Comparable接口并覆写comprareTo()方法。
package com.sram.entity; public class Student implements Comparable<Student>{ private String name; private Integer age; private Integer code; public Student(String name, Integer age, Integer code) { this.name = name; this.age = age; this.code = code; } public Student() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", code=" + code + "]"; } @Override public int compareTo(Student stu) { //通过学号排序code if(this.code>stu.getCode()){ return -1; }else if(this.code<stu.getCode()){ return 1; }else{ return 0; } } } |
public static void main(String[] args) { //创建TreeMap类的对象 TreeMap<Student,String> treeMap=new TreeMap<Student,String>(); //添加内容 treeMap.put(new Student("李四",31,1002),"回族"); treeMap.put(new Student("张三",23,1001),"汉族"); treeMap.put(new Student("王五",22,1003),"维吾尔族");
System.out.println(treeMap); } |
{Student [name=王五, age=22, code=1003]=维吾尔族, Student [name=李四, age=31, code=1002]=回族, Student [name=张三, age=23, code=1001]=汉族} |
Map集合的内容输出
- 输出Map集合中key
要想输出key,首先应该从集合中获取key
public static void main(String[] args) { //创建TreeMap类的对象 TreeMap<Student,String> treeMap=new TreeMap<Student,String>(); //添加内容 treeMap.put(new Student("李四",31,1002),"回族"); treeMap.put(new Student("张三",23,1001),"汉族"); treeMap.put(new Student("王五",22,1003),"维吾尔族"); //方式一使用for...each循环 Set<Student> keys=treeMap.keySet();//获取所有的key for (Student student : keys) { System.out.println(student); } } //方式二使用迭代器 Iterator<Student> iterator=keys.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } |
Student [name=王五, age=22, code=1003] Student [name=李四, age=31, code=1002] Student [name=张三, age=23, code=1001] |
上面使用两种方式输出Set集合中的key,一种是使用fort...each循环输出,迭代器输出。
2.输出Map集合中的value
通过key获取value
public static void main(String[] args) { //创建TreeMap类的对象 TreeMap<Student,String> treeMap=new TreeMap<Student,String>(); //添加内容 treeMap.put(new Student("李四",31,1002),"回族"); treeMap.put(new Student("张三",23,1001),"汉族"); treeMap.put(new Student("王五",22,1003),"维吾尔族"); //方式一使用for...each循环 Set<Student> keys=treeMap.keySet();//获取所有的key for (Student student : keys) { //通过key获取value值 System.out.println(treeMap.get(student)); } //迭代器 /*Iterator<Student> iterator=keys.iterator(); while(iterator.hasNext()){ //通过key获取value值 System.out.println(treeMap.get(iterator.next())); }*/ } |
维吾尔族 回族 汉族 |
直接获取value值
public static void main(String[] args) { //创建TreeMap类的对象 TreeMap<Student,String> treeMap=new TreeMap<Student,String>(); //添加内容 treeMap.put(new Student("李四",31,1002),"回族"); treeMap.put(new Student("张三",23,1001),"汉族"); treeMap.put(new Student("王五",22,1003),"维吾尔族"); //直接获取集合中value Collection<String> collection=treeMap.values(); //方式一使用for...each /*for (String string : collection) { System.out.println(string);//输出value值 }*/ //方式二迭代器 Iterator<String> iterator=collection.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next());//输出value值 } } |
维吾尔族 回族 汉族 |
上面就是Java中集合也称为类集的全部内容了,集合一共分为两种:Collection/Map。