------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
Map<K, V>
特点:
1. 以Key和Value,成对的形式存储数据。
2. Key不能重复
3. Set是基于Map而生成的。
常用方法:
1. 增:
V put(K key, V value)
如果添加相同Key,不同的Value,则保存最后添加进来的Value。
boolean putAll(Map<? extends k, ? extends v>)
2. 删:
clear()
V remove(K key) ,底层判断key.equals(k)
3. 改:
4. 查:
V get(Object key)
5. 判断:
boolean containsKey(Obejct key)
boolean containsValue(Obejct value)
boolean isEmpty()
package com.lxh.collection;
import java.util.*;
public class MapDemo {
/**
* 使用keySet()方法,获取Map中的全部Key
*/
public static void main(String[] args) {
Map
map = new HashMap/*TreeMap有序的*/
();
map.put("01", "a1");
map.put("04", "a4");
map.put("02", "a2");
map.put("03", "a3");
map.put("null", "a5");
map.put("06", "null");
// 当对同一个键,赋值两次时,以最后一次的赋值为最终值。
map.put("06", "06");
// values()返回values的collection视图
Collection coll = map.values();
System.out.println(coll);
// HashMap允许键值为null
System.out.println("null:" + map.get("null"));
// keySet()获取Key的Set视图
Set
set = map.keySet();
Iterator it = set.iterator();
while(it.hasNext()) {
String str = (String)it.next();
System.out.println(map.get(str));
}
}
}
Map.entrySet()方法。返回Map中的映射项(键 - 值对)
内部接口:
Map.Entry<K , V>
1. 用存放映射项(键 - 值对)
2. 定义:public static interface Map.Entry<K , V> 注意:接口声明为static,只可能做为内部接口,才可能被调用。
3. 因为价值对值存在于Map接口中,其他类没有此功能,故此将Map.Entry作为内部接口定义在Map接口中。
Map.Entry<K , V>常用方法:
1. K getKey()
2. V getValue()
3. V setValue()
public class MapEntrySetDemo {
public static void main(String[] args) {
Map
map = new HashMap/*TreeMap有序的*/
();
map.put("01", "a1");
map.put("04", "a4");
map.put("02", "a2");
map.put("03", "a3");
map.put("null", "a5");
map.put("06", "null");
Set
> set = map.entrySet();
Iterator
> it = set.iterator(); while(it.hasNext()) { Map.Entry
me = it.next(); System.out.println(me.getKey() + ":" + me.getValue()); } } } /** * Map.Entry原理 * */ interface Map { public static interface Entry { public abstract Object getKey(); public abstract Object getValue(); } } class HashMap implements Map{ class Aaa implements Map.Entry { public Object getKey() { return new Object(); } public Object getValue() { return new Object(); } } }
常用子类:
1. TreeMap
2. HashMap
3. Hashtable
Hashtable
特点:
1. 底层实现是Hashtable。无序
2. Key和Value都不允许为null。
3. 用作Key的对象,必须实现equals()和hashCode()方法。
4. JDK1.0时出现的,是线程同步的。效率相对低
HashMap
特点:
1. Key和Value都允许为null
2. JDK1.2版本出线,非同步,效率相对高。
3. 其他特点和Hashtable几乎相同。
演示例子:
需求:
Student对象继承Comparable接口,重写HashCode和equals方法;
该对象有name和age字段。
存入到HashMap<Student,String>中。
分别使用keySet方法和EntrySet方法遍历HashMap集合。
package com.lxh.collection;
import java.util.HashMap;
import java.util.*;
public class MapDemo2 {
/**
* @param args
*/
public static void main(String[] args) {
Coach c1 = new Coach("aa1",11);
Coach c2 = new Coach("aa2",12);
Coach c3 = new Coach("aa3",13);
Coach c4 = new Coach("aa4",14);
HashMap
hm = new HashMap();
hm.put(c1, "北京");
hm.put(c2, "上海");
hm.put(c3, "南京");
hm.put(c4, "武汉");
Set
s = hm.keySet();
Iterator
it = s.iterator();
while(it.hasNext()) {
Coach c = it.next();
System.out.println(c + "..." + hm.get(c));
}
Set
> set = hm.entrySet(); Iterator
> iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry
me = iterator.next(); System.out.println(me.getKey() + ":" + me.getValue()); } } } class Coach implements Comparable
{ private String name; private int age; public Coach(String name , int age) { this.name = name; this.age = age; } public String toString() { return name + ":" + age; } public int HashCode() { return this.name.hashCode() + this.age*39; } public boolean equals(Coach c) { return this.name.equals(c.name) && this.age == c.age; } public int compareTo(Coach c) { if(!(c instanceof Coach)) throw new ClassCastException("请输入Coach类型的对象!"); int num = name.compareTo(c.name); if(num == 0) return new Integer(age).compareTo(new Integer(c.age)); return num; } }
需求:
输入一个字符串”abcdabcaba”,返回各个字符出线的次数。格式如下:a(4),b(3)...
思路:
接收的格式为映射关系,可以使用Map数据结构。
将字符串转换成字符数组,遍历该数组,使用Map<String,Integer>集合接收(打印时需要注意顺序,选择TreeMap),字符作为Key,数字作为Value,每一次都使用Map的contains()方法判断键是否存在,存在其值加1,否则创建一个新建,将值赋值为1。
package com.lxh.collection;
import java.util.*;
public class MapStringCount {
/**
* @param args
*/
public static void main(String[] args) {
// 1. 初始化字符串”abcdabcaba”
String s = "abcdabcaba";
// 2. 将字符串转换成字符数组
char[] array = s.toCharArray();
// 打印时需要注意顺序,初始化TreeMap
的map对象
TreeMap
tm = new TreeMap
();
// 3. 遍历字符数组的每一个字符
for(int i = 0; i < array.length ;i++) {
Character key = array[i];
if(tm.containsKey(key)){
// Map中包含该键,则加1
Integer value = tm.get(key);
tm.put(key, ++value);
} else {
// 否则,创建该键,赋值为1
tm.put(key, 1);
}
}
// 将Map中的键值对打印出来。
Set
> set = tm.entrySet(); Iterator
> it = set.iterator(); while(it.hasNext()) { Map.Entry
me = it.next(); System.out.print(me.getKey() + "(" + me.getValue() + ") "); } } }
Map一对多
School
|-- Class
|-- Student
ID
Name
HashMap<ClassNo , className> School
HashMap<ID , Name> className
遍历所有的学生信息。
package com.lxh.collection;
import java.util.*;
public class MapOneToMore {
public static void main(String[] args) {
HashMap
> school = new HashMap
>();
HashMap
classA = new HashMap
(); HashMap
classB = new HashMap
(); school.put("一年级", classA); school.put("二年级", classB); classA.put("001", "Daizy1"); classA.put("002", "Daizy2"); classB.put("001", "Kouxue1"); classB.put("002", "Kouxue2"); Set
>> set = school.entrySet(); Iterator
>> it = set.iterator(); while(it.hasNext()) { Map.Entry
> me = it.next(); stuInfo(me.getValue()); } } public static void stuInfo(HashMap
cla) { Set
> set = cla.entrySet(); Iterator
> it = set.iterator(); while(it.hasNext()) { Map.Entry
me = it.next(); System.out.println(me.getKey() + ":" + me.getValue()); } } }