8 Map集合
HashMap
: 元素按照键是无序,不重复,无索引,值不做要求 LinkedHashMap
: 元素按照键是有序,不重复,无索引,值不做要求
8.1 Map集合概述和特点
Map集合是一种双列集合,每个元素包含两个值 Interface Map<K,V>; K:键的类型,V:值的类型 Map集合的每个元素的格式 :key = value(键值对元素)Map集合也被称为“键值对集合” Map集合特点 :
Map 集合的键是无序,不重复,无索引的 Map 集合后面重复的键对应的元素会覆盖 前面的整个元素 创建Map集合对象 :
package ceshi ;
import java. util. HashMap ;
import java. util. Map ;
public class MapDemo {
public static void main ( String [ ] args) {
Map < String , String > map = new HashMap < > ( ) ;
map. put ( "y1" , "10" ) ;
map. put ( "y2" , "20" ) ;
map. put ( "y2" , "30" ) ;
map. put ( "y3" , "30" ) ;
System . out. println ( map) ;
}
}
8.2 Map集合的基本方法
方法名 说明 public V put(K key, V value) 添加元素 public V remove(Object key) 根据键删除键值对元素 public void clear() 移除所有键值对元素 public boolean containKey(Object key) [kənˈteɪn] 判断集合是否包含指定的键 public boolean containValue(Object value) 判断集合是否包含指定的值 public boolean isEmpty() 判断集合是否为空 public int size() 集合的长度,也就是集合中键值对个数
package ceshi ;
import java. util. HashMap ;
import java. util. Map ;
public class MapDemo {
public static void main ( String [ ] args) {
Map < String , String > map = new HashMap < > ( ) ;
map. put ( "y1" , "10" ) ;
map. put ( "y2" , "20" ) ;
map. put ( "y3" , "30" ) ;
System . out. println ( map) ;
System . out. println ( map. size ( ) ) ;
}
}
8.3 Map集合的获取方法(重点)
方法名 说明 public V get(Object key) 根据键获取值 public Set<K> keySet()
获取所有键的集合,存储到Set集合中 public Collection<V> values()
获取所有值的集合,存储到Collection集合中 public Set<Map.Entry<K,V>> entrySet()
获取所有键值对对象的集合(Set集合)
package ceshi ;
import java. util. Collection ;
import java. util. HashMap ;
import java. util. Map ;
import java. util. Set ;
public class MapDemo {
public static void main ( String [ ] args) {
Map < String , String > map = new HashMap < > ( ) ;
map. put ( "y1" , "10" ) ;
map. put ( "y2" , "20" ) ;
map. put ( "y3" , "30" ) ;
System . out. println ( map) ;
System . out. println ( map. get ( "y1" ) ) ;
System . out. println ( map. get ( "y4" ) ) ;
Set < String > key = map. keySet ( ) ;
for ( String s: key) {
System . out. println ( s) ;
}
Collection < String > vlaues = map. values ( ) ;
for ( String s: vlaues) {
System . out. println ( s) ;
}
Set < Map. Entry < String , String > > entries = map. entrySet ( ) ;
for ( Map. Entry < String , String > s: entries) {
System . out. println ( s) ;
}
}
}
8.4 Map集合的遍历
Map 集合遍历有三种方式 :
"键找值"的方式遍历 "键值对"的方式遍历 Lambda 表达式(JDK1.8开始之后的新技术)
8.4.1 "键找值"的方式遍历(常用)
1、获取所有键的集合:用keySet()
方法实现 2、遍历键的集合,获取到每一个键:用增强for 遍历实现 3、根据键去找值:在增强for中,用get(Object key)
方法实现
package ceshi ;
import java. util. Collection ;
import java. util. HashMap ;
import java. util. Map ;
import java. util. Set ;
public class MapDemo {
public static void main ( String [ ] args) {
Map < String , String > map = new HashMap < > ( ) ;
map. put ( "y1" , "10" ) ;
map. put ( "y2" , "20" ) ;
map. put ( "y3" , "30" ) ;
System . out. println ( map) ;
Set < String > keySet = map. keySet ( ) ;
for ( String key : keySet) {
String value = map. get ( key) ;
System . out. println ( key + "=" + value) ;
}
}
}
8.4.2 "键值对"的方式遍历
1、获取所有键值对对象的集合:Set<Map.Entry<K,V>> entrySet()
2、遍历键值对对象的集合,得到每一个键值对对象:用增强for实现,得到每一个Map.Entry
3、根据键值对对象获取键和值:在增强for中,用geKey()
得到键;用getValue()
得到值
package ceshi ;
import java. util. Collection ;
import java. util. HashMap ;
import java. util. Map ;
import java. util. Set ;
public class MapDemo {
public static void main ( String [ ] args) {
Map < String , String > map = new HashMap < > ( ) ;
map. put ( "y1" , "10" ) ;
map. put ( "y2" , "20" ) ;
map. put ( "y3" , "30" ) ;
System . out. println ( map) ;
Set < Map. Entry < String , String > > entrySet = map. entrySet ( ) ;
for ( Map. Entry < String , String > me: entrySet) {
String key = me. getKey ( ) ;
String value = me. getValue ( ) ;
System . out. println ( key+ "=" + value) ;
}
}
}
8.4.3 Lambda [ˈlæmdə] 表达式方式遍历
package ceshi ;
import java. util. Collection ;
import java. util. HashMap ;
import java. util. Map ;
import java. util. Set ;
public class MapDemo {
public static void main ( String [ ] args) {
Map < String , String > map = new HashMap < > ( ) ;
map. put ( "y1" , "10" ) ;
map. put ( "y2" , "20" ) ;
map. put ( "y3" , "30" ) ;
System . out. println ( map) ;
map. forEach ( ( k , v) -> {
System . out. println ( k+ "=" + v) ;
} ) ;
}
}
8.5 Map集合存储自定义类型
Map集合的键和值都可以存储自定义类型 如果希望Map集合认为自定义类型的键对象重复了,必须重写对象的hashCode()
和equals()
方法
8.6 LinkedHashMap(HashMap子类)
LinkedHashMap集合是有序不重复 的键值对集合
public class LinkedHashMapDemo {
public static void main ( String [ ] args) {
Map < String , Integer > map = new LinkedHashMap < > ( ) ;
map. put ( "y1" , 10 ) ;
map. put ( "y2" , 20 ) ;
map. put ( "y3" , 30 ) ;
map. put ( "y3" , 100 ) ;
System . out. println ( map) ;
}
}
8.7 TreeMap
TreeMap 集合按照键是可排序不重复 的键值对集合(默认升序)
8.8 案例
8.8.1 集合遍历
package ceshi ;
import java. util. Collection ;
import java. util. HashMap ;
import java. util. Map ;
import java. util. Set ;
public class MapDemo {
public static void main ( String [ ] args) {
HashMap < String , Student > hm = new HashMap < > ( ) ;
Student s1 = new Student ( "y1" , 10 ) ;
Student s2 = new Student ( "y2" , 20 ) ;
Student s3 = new Student ( "y3" , 30 ) ;
hm. put ( "itheima1" , s1) ;
hm. put ( "itheima2" , s2) ;
hm. put ( "itheima3" , s3) ;
Set < String > keySet = hm. keySet ( ) ;
for ( String key: keySet) {
Student value = hm. get ( key) ;
System . out. println ( key+ "," + value. getName ( ) + "," + value. getAge ( ) ) ;
}
System . out. println ( "---------" ) ;
Set < Map. Entry < String , Student > > entrySet= hm. entrySet ( ) ;
for ( Map. Entry < String , Student > me: entrySet) {
String key = me. getKey ( ) ;
Student value = me. getValue ( ) ;
System . out. println ( key+ "," + value. getName ( ) + "," + value. getAge ( ) ) ;
}
}
}
8.8.2 集合嵌套之 ArrayList嵌套HashMap
package ceshi ;
import java. util. * ;
public class MapDemo {
public static void main ( String [ ] args) {
ArrayList < HashMap < String , String > > array = new ArrayList < > ( ) ;
HashMap < String , String > hm1 = new HashMap < > ( ) ;
hm1. put ( "y1" , "10" ) ;
HashMap < String , String > hm2 = new HashMap < > ( ) ;
hm1. put ( "y2" , "20" ) ;
HashMap < String , String > hm3 = new HashMap < > ( ) ;
hm1. put ( "y3" , "30" ) ;
array. add ( hm1) ;
array. add ( hm2) ;
array. add ( hm3) ;
for ( HashMap < String , String > hm: array) {
Set < String > keySet = hm. keySet ( ) ;
for ( String key: keySet) {
String value = hm. get ( key) ;
System . out. println ( key+ "," + value) ;
}
}
}
}
8.8.3 统计字符串中每个字符出现的次数