类图1:
类图2:
C++中的map方法
1。关键字有序保存元素
map 关联数组,保存key-value
set 只保存关键字容器
multimap 关键字可重复出现的map
multiset 关键字可重复出现的set
2。关键字无序集合
unordered_map 用哈希函数组织的map
unordered_set 用哈希函数组织的set
unordered_multimap 用哈希组织的map,关键字可以重复出现
unordered_multiset 用哈希组织的set, 关键字可以重复出现
Map接口常用的操作:
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* 实现Comparable()接口
* @author Administrator
*
*/
class Student implements Comparable<Student>{
private int score;
private int age;
public Student(int score, int age) {
super();
this.score = score;
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Student o) {
if(score != o.score) {
return score - o.score;
}
return 0;
}
@Override
public String toString() {
return "Student [score=" + score + ", age=" + age + "]";
}
}
public class MapDemo {
/**
* 取元素
*/
public static void getTest() {
Map<String, String> map = new HashMap<String, String>();
map.put("wwh", "wangwenhao");
String val = map.get("wwh");
System.out.println("val = " + val);
System.out.println();
}
/**
* 判断指定的key或value是否存在.
* Map接口提供的containsKey(Object key)
* containsValue(Object value)
*/
public static void containsTest() {
Map<String, String> map = new HashMap<String, String>();
map.put("wwh", "wangwenhao");
if(map.containsKey("wwh")) {
System.out.println("搜索key存在!");
}
if(map.containsValue("wangwenhao")){
System.out.println("搜索value存在!");
}
System.out.println();
}
/*
* 取key和value集合元素
*/
public static void keySetTest() {
Map<String, String> map = new HashMap<>();
map.put("qqh", "qiuqihuang");
map.put("wwh", "wangwenhao");
Set<String> keys = map.keySet();
Iterator<String> iterator = keys.iterator();
System.out.println("全部的key: ");
while(iterator.hasNext()) {
String str = iterator.next();
System.out.println(str);
}
System.out.println("全部的value: ");
Collection<String> valuesCollection = map.values();
Iterator<String> iter = valuesCollection.iterator();
while(iter.hasNext()) {
String str = iter.next();
System.out.println(str);
}
System.out.println();
}
/**
* For TreeMap 需要实现Comparable接口
* @param args
*/
public static void treeMapTest() {
Map<Student, String> map = new TreeMap<>();
Student oneStudent = new Student(99, 23);
Student twoStudent = new Student(94, 21);
Student threeStudent = new Student(89, 22);
map.put(oneStudent, "First");
map.put(twoStudent, "second");
map.put(threeStudent, "third");
Set<Student> keys = map.keySet();
Iterator<Student> iter = keys.iterator();
while(iter.hasNext()) {
Student temp = iter.next();
System.out.println(temp + " " + map.get(temp));
}
}
/*
* 1.将Map接口的实例通过entrySet()方法变为Set接口
* 2.通过Set接口实例为Iterator实例化
* 3.通过Iterator迭代输出, 每个内容都是Map.Entry的对象
* 4.通过Map.Entry进行key->value的分离
* 注意:不能直接用迭代器输出Map中的内容
* Map一般不经常直接输出, 只是作为查询使用,以上流程是Map输出最标准的流程.
*/
public static void outputMapTest() {
Map<String, String> map = new HashMap<>();
map.put("China", "Beijing");
map.put("America", "Washington");
map.put("wwh", "wangwenhao");
Set<Map.Entry<String, String>> allSet = map.entrySet();
Iterator<Map.Entry<String, String>> iterator = allSet.iterator();
while(iterator.hasNext()) {
Map.Entry<String, String> me = iterator.next();
System.out.println(me.getKey() + " " + me.getValue());
}
System.out.println();
}
/**
* foreach 输出Map
* 1.输出是还将Map集合转变为Set集合, Set集合中的每一个元素都是Map.Entry对象
*/
public static void outputMapTest2() {
Map<String, String> map = new HashMap<>();
map.put("China", "Beijing");
map.put("America", "Washington");
map.put("wwh", "wangwenhao");
for(Map.Entry<String, String> me:map.entrySet()) {
System.out.println(me.getKey() + " " + me.getValue());
}
System.out.println();
}
public static void main(String[] args) {
getTest();
containsTest();
keySetTest();
treeMapTest();
outputMapTest();
outputMapTest2();
}
}
SortedMap接口特有的方法:
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
public class SortedMapDemo {
/**
* SortedMap接口是排序接口,TreeMap实现了此接口,所以TreeMap有排序功能。
* 在此接口中定义了一些Map中没有的方法:
* 1.返回第一个元素
* 2.返回最后一个元素
* 3.返回小于指定元素的集合[begin, end)
* 4.返回大于等于指定元素的集合[begin,)
* 5.返回指定key返回的集合[begin, end)
* 6.注意左右开闭区间
* 如果是是自己定义的类,需要实现Comparable接口
*/
public static void main(String[] args) {
SortedMap<String, String> map = new TreeMap<>();
map.put("A", "AAA");
map.put("B", "BBB");
map.put("C", "CCC");
map.put("D", "DDD");
map.put("E", "EEE");
System.out.println("第一个元素:" + map.firstKey() + "->" + map.get(map.firstKey()));
System.out.println("最后一个元素: " + map.lastKey() + "->" + map.get(map.lastKey()));
System.out.println("返回小于指定范围的集合: ");
for(Map.Entry<String, String> me : map.headMap("C").entrySet()) {
System.out.println(me.getKey() + "->" + me.getValue());
}
System.out.println("返回大于等于指定范围的集合: ");
for(Map.Entry<String, String> me : map.tailMap("C").entrySet()) {
System.out.println(me.getKey() + "->" + me.getValue());
}
System.out.println("返回部分集合:");
for(Map.Entry<String, String> me: map.subMap("B", "D").entrySet()) {
System.out.println(me.getKey() + "->" + me.getValue());
}
}
}
IdentityHashMap运行有重复的元素
import java.util.HashMap;
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) {
super();
this.name = name;
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
public class IdentityHashMapDemo {
/**
* 从结果看,第二个内容覆盖了第一个的内容。
*/
public static void hashMapTest() {
Map<Person, String> map = new HashMap<Person, String>();
map.put(new Person("张三", 30), "zhangsanFirst");
map.put(new Person("张三", 30), "zhangsanSecond");
map.put(new Person("李四", 31), "lisi");
Set<Map.Entry<Person, String>> allSet = map.entrySet();
Iterator<Map.Entry<Person, String>> iter = allSet.iterator();
while(iter.hasNext()) {
Map.Entry<Person, String> me = iter.next();
System.out.println(me.getKey() + "-->" + me.getValue());
}
System.out.println();
}
/**
* 可以保存重复的key
*/
public static void identityHashMapTest() {
Map<Person, String> map = new IdentityHashMap<Person, String>();
map.put(new Person("张三", 30), "zhangsanFirst");
map.put(new Person("张三", 30), "zhangsanSecond");
map.put(new Person("李四", 31), "lisi");
Set<Map.Entry<Person, String>> allSet = map.entrySet();
Iterator<Map.Entry<Person, String>> iter = allSet.iterator();
while(iter.hasNext()) {
Map.Entry<Person, String> me = iter.next();
System.out.println(me.getKey() + "-->" + me.getValue());
}
System.out.println();
}
public static void main(String[] args) {
hashMapTest();
identityHashMapTest();
}
}