Map集合--映射
|-- 一个键只能映射一个值,保证键的唯一性
|-- 常用方法
|-- put(Key,Value),将键值存储到集合,put方法的返回值,一般情况下是null,但是如果存储了
重复的键,替换原来老键的值,put方法的返回值就是被替换后的值
|-- boolean containsKey(键) 判断集合中有没有这个键
|-- boolean containsValue(值) 判断集合有有没有这个值
|-- V get(键),通过键,获取值,获取不到就是null,获取不到值,说明根本就没这个键
|-- 迭代Set集合,就可以获取到键
|-- 根据键,获取值,get()是Map集合的方法
|-- 通过Map集合中键值的映射关系来获取
|-- Set<Map.Entry<K,V>> entrySet(),这个方法返回的是键值映射关系对
|-- entrySet()方法返回的夫妻关系,表示这个夫妻关系的对象是
|-- 常用方法
|-- put(Key,Value),将键值存储到集合,put方法的返回值,一般情况下是null,但是如果存储了
重复的键,替换原来老键的值,put方法的返回值就是被替换后的值
|-- boolean containsKey(键) 判断集合中有没有这个键
|-- boolean containsValue(值) 判断集合有有没有这个值
|-- V get(键),通过键,获取值,获取不到就是null,获取不到值,说明根本就没这个键
Map集合的取出方法
|-- Map集合中有一个方法,keySet(),将Map中的键,存储到Set集合|-- 迭代Set集合,就可以获取到键
|-- 根据键,获取值,get()是Map集合的方法
|-- 通过Map集合中键值的映射关系来获取
|-- Set<Map.Entry<K,V>> entrySet(),这个方法返回的是键值映射关系对
|-- entrySet()方法返回的夫妻关系,表示这个夫妻关系的对象是
Map子类的特点
|-- HashMap
|-- 底层结构是哈希表|-- 线程不安全
|-- 不允许存储重复键
|-- 允许存储null值和null键
package cn.river.map;
import java.util.*;
public class HashMapDemo {
public static void main(String[] args) {
//method();
HashMap<String,Integer> hm = new HashMap<String,Integer>();
hm.put("a", 1);
hm.put("b", 2);
hm.put("c", 3);
hm.put("d", 4);
hm.put("e", 5);
System.out.println(hm);
boolean b = hm.containsKey("d");
System.out.println(b);
b = hm.containsValue(3);
System.out.println(b);
Integer i = hm.get("e");
System.out.println(i);
}
public static void method(){
HashMap<String,Integer> hm = new HashMap<String,Integer>();
hm.put("sa", 1);
hm.put("sb", 2);
hm.put("sc", 3);
hm.put("sd", 4);
hm.put("sd", 5);
System.out.println(hm);
System.out.println(hm.put("ss",2));
System.out.println(hm.put("ss",2));
}
}
package cn.river.map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class HashMapDemo2 {
public static void main(String[] args) {
method_2();
}
//Map集合存储自定义对象并取出
public static void method_3(){
HashMap<Person,String> hm = new HashMap<Person,String>();
hm.put(new Person("river01",21),"北京市" );
hm.put(new Person("river01",21),"天津市" );
hm.put(new Person("river01",21),"河北" );
hm.put(new Person("river01",21),"太原" );
//使用Map集合的keySet()方法,将键存储到Set集合
Set<Map.Entry<Person, String>> set = hm.entrySet();
Iterator<Map.Entry<Person, String>> it = set.iterator();
while(it.hasNext()){
Map.Entry<Person, String> me = it.next();
Person p = me.getKey();
String s = me.getValue();
System.out.println(p+"..."+s);
}
}
//Map集合存储自定义对象并取出
public static void method_2(){
HashMap<Person,String> hm = new HashMap<Person,String>();
hm.put(new Person("river01",21),"北京市" );
hm.put(new Person("river01",21),"天津市" );
hm.put(new Person("river01",21),"河北" );
hm.put(new Person("river01",21),"太原" );
//使用Map集合的keySet()方法,将键存储到Set集合
Set<Person> set = hm.keySet();
Iterator<Person> it = set.iterator();
while(it.hasNext()){
Person s = it.next();
String i = hm.get(s);
System.out.println(s+"..."+i);
}
}
//Map集合存储对象
public static void method(){
HashMap<String,Integer> hm = new HashMap<String,Integer>();
hm.put("a", 1);
hm.put("b", 2);
hm.put("c", 3);
hm.put("d", 4);
//使用Map集合的keySet()方法,将键存储到Set集合
Set<String> set = hm.keySet();
Iterator<String> it = set.iterator();
while(it.hasNext()){
String s = it.next();
Integer i = hm.get(s);
System.out.println(s+"..."+i);
}
}
}
|-- TreeMap
|-- 底层数据结构是红黑树|-- 线程不安全
|-- 不允许存储重复键
|-- 作为键的存储对象,对象必须具备比较性,对象实现Compareble接口
|-- 或者让TreeMap自身具备比较性
package cn.river.map;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
method();
}
//Map集合存储对象
public static void method(){
TreeMap<Person,String> tm = new TreeMap<Person,String>();
tm.put(new Person("river01",21),"北京市" );
tm.put(new Person("river01",21),"天津市" );
tm.put(new Person("river01",21),"河北" );
tm.put(new Person("river01",21),"太原" );
//使用Map集合的keySet()方法,将键存储到Set集合
/*Set<Person> set = tm.keySet();
Iterator<Person> it = set.iterator();
while(it.hasNext()){
Person p = it.next();
String a = tm.get(p);
System.out.println(p+"..."+a);
}*/
//通过entrySet()方法,获取到键值关系,并存储到Set集合
Set<Map.Entry<Person,String>> set1 = tm.entrySet();
//迭代Set集合
Iterator<Map.Entry<Person,String>> it1 = set1.iterator();
while(it1.hasNext()){
Map.Entry<Person, String> me1 =it1.next();
Person p = me1.getKey();
String s = me1.getValue();
System.out.println(p+"...."+s);
}
}
}
package cn.river.map;
public class Person implements Comparable<Person>{
String name;
int age;
public Person(String name, int age){
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
public boolean equals(Object obj){
if(obj==this)
return true;
if(obj instanceof Person){
Person p = (Person) obj;
return p.name.equals(this.name) && p.age==this.age;
}
return false;
}
public int hashCode(){
return this.name.hashCode()+this.age*31;
}
@Override
public int compareTo(Person o) {
int num = o.age - this.age;
return num==0 ? o.name.compareTo(this.name):num;
}
}
|-- Hashtable
|-- 底层数据结构也是哈希表|-- 线程安全
|-- 不允许存储重复键
|-- 不允许null值和null键
|-- 郁郁而终了
|-- Hashtable的子类,是我们以后很常用的一个对象,Properties
|-- Properties最大特点:可IO流管理使用
练习
package cn.river.map;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/*
* 实现统计字母出现次数
*/
public class TreeMapTest {
public static void main(String[] args) {
String s = "abcabcdsffdssseeehg";
method(s);
}
public static void method(String str){
if(str==null||str=="")
throw new RuntimeException("数据异常");
//将字符串转换成字符数组
char[] ch = str.toCharArray();
//建立一个TreeMap字符做键,出现次数做值
TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
for(int i=0; i<ch.length;i++){ //循环数组
//如果map里面没有对应的字符,则添加一个
if(!tm.containsKey(ch[i]))
tm.put(ch[i], 1);
else{
//如果对应的字符存在,则将其值加+1
Integer x = (Integer)tm.get(ch[i]);
x++;
tm.put(ch[i], x);
}
}
//遍历Map
Set<Map.Entry<Character, Integer>> set = tm.entrySet();
Iterator<Map.Entry<Character, Integer>> it = set.iterator();
while(it.hasNext()){
Map.Entry<Character, Integer> me = it.next();
Character c = me.getKey();
Integer i = me.getValue();
System.out.println("字母"+c+"出现了"+i+"次");
}
}
}