Map集合
Map接口和Collection接口的不同
Map是双列的,Collection是单列的
Map的键唯一,Collection的子体系Set是唯一的
Map集合的数据结构针对键有效,跟值无关;Collection集合的数据结构是针对元素有效
A:HashMap集合底层是哈希表/散列表的数据结构
B:哈希表是一个数组和单链表的结合体(数组查询效率高,增删效率低,链表增删效率高,查询效率低)哈希表充分发挥他们各自的优点
Map集合的方法
V put(K key,V value):添加元素
如果键是第一次存储,就直接存储元素,返回null
如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值
void clear():移除所有的键值对元素
V remove(Object key):根据键删除键值对元素,并把值返回
boolean containsKey(Object key):判断集合是否包含指定的键
boolean containsValue(Object value):判断集合是否包含指定的值
boolean isEmpty():判断集合是否为空
Set<Map.Entry<K,V>> entrySet(): 返回一个键值对的Set集合
V get(Object key):根据键获取值
Set<K> keySet():获取集合中所有键的集合
Collection<V> values():获取集合中所有值的集合
int size():返回集合中的键值对的对数
import java. util. *;
public class MapTest1 {
public static void main ( String[ ] args) {
Map< Integer, String> map = new HashMap < > ( ) ;
map. put ( 1 , "aaa" ) ;
map. put ( 2 , "bbb" ) ;
map. put ( 3 , "ccc" ) ;
map. put ( 4 , "ddd" ) ;
map. put ( 5 , "eee" ) ;
map. put ( 6 , "eee" ) ;
map. remove ( 6 ) ;
boolean b = map. containsKey ( 4 ) ;
System. out. println ( b) ;
boolean bbb = map. containsValue ( "bbb" ) ;
System. out. println ( bbb) ;
boolean empty = map. isEmpty ( ) ;
System. out. println ( empty) ;
System. out. println ( map. size ( ) ) ;
Set< Integer> keySet = map. keySet ( ) ;
Iterator< Integer> it = keySet. iterator ( ) ;
while ( it. hasNext ( ) ) {
Integer key = it. next ( ) ;
String value = map. get ( key) ;
System. out. println ( key + "==" + value) ;
}
System. out. println ( "==========" ) ;
for ( Integer keys : keySet) {
String value = map. get ( keys) ;
System. out. println ( keys + "==" + value) ;
}
System. out. println ( "==========" ) ;
Set< Map. Entry< Integer, String> > mapSet = map. entrySet ( ) ;
Iterator< Map. Entry< Integer, String> > it1 = mapSet. iterator ( ) ;
while ( it1. hasNext ( ) ) {
Map. Entry< Integer, String> node = it1. next ( ) ;
Integer key = node. getKey ( ) ;
String value = node. getValue ( ) ;
System. out. println ( key + "===" + value) ;
}
for ( Map. Entry< Integer, String> node1 : mapSet) {
System. out. println ( node1) ;
}
}
}
public class MyTest5 {
public static void main ( String[ ] args) {
HashMap< Student, String> hm = new HashMap < > ( ) ;
hm. put ( new Student ( "张三" , 23 ) , "s001" ) ;
hm. put ( new Student ( "张三" , 23 ) , "s002" ) ;
hm. put ( new Student ( "李四" , 24 ) , "s003" ) ;
hm. put ( new Student ( "王五" , 25 ) , "s004" ) ;
Set< Student> students = hm. keySet ( ) ;
for ( Student key : students) {
String value = hm. get ( key) ;
System. out. println ( key. getName ( ) + "==" + key. getAge ( ) + "===" + value) ;
}
System. out. println ( "============================" ) ;
Set< Map. Entry< Student, String> > entries = hm. entrySet ( ) ;
for ( Map. Entry< Student, String> en : entries) {
Student key = en. getKey ( ) ;
String value = en. getValue ( ) ;
System. out. println ( key. getName ( ) + "==" + key. getAge ( ) + "===" + value) ;
}
System. out. println ( "===============================" ) ;
hm. forEach ( new BiConsumer < Student, String> ( ) {
@Override
public void accept ( Student key, String value) {
System. out. println ( key. getName ( ) + "==" + key. getAge ( ) + "===" + value) ;
}
} ) ;
}
}
class Student implements Comparable < Student> {
private String name;
private int age;
public Student ( ) {
}
public Student ( String name, int age) {
this . name = name;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
@Override
public String toString ( ) {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}' ;
}
@Override
public boolean equals ( Object o) {
if ( this == o) return true ;
if ( o == null || getClass ( ) != o. getClass ( ) ) return false ;
Student student = ( Student) o;
return age == student. age &&
Objects. equals ( name, student. name) ;
}
@Override
public int hashCode ( ) {
return Objects. hash ( name, age) ;
}
@Override
public int compareTo ( Student s) {
int num = this . age - s. age;
int num2= num== 0 ? this . name. compareTo ( s. name) : num;
return num2;
}
}
HashMap和HashTable
HashMap 允许 null值和 null键。线程不安全效率高
Hashtable 不允许存储null值和null键 线程安全效率低
HashMap< Object, Object> hm = new HashMap < > ( ) ;
hm. put ( null, "abc" ) ;
hm. put ( "aaa" , null) ;
hm. put ( null, null) ;
System. out. println ( hm) ;
Hashtable< Object, Object> hashtable = new Hashtable < > ( ) ;
LinkedHashMap
Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序
LinkedHashMap的特点:
底层的数据结构是链表和哈希表 元素有序 并且唯一
元素的有序性由链表数据结构保证 唯一性由 哈希表数据结构保证
Map集合的数据结构只和键有关
TreeMap
键的数据结构是红黑树,可保证键的排序和唯一性
排序分为自然排序和比较器排序
线程是不安全的效率比较高
package org. westos. 作业;
import java. util. Comparator;
import java. util. TreeMap;
public class TreeMapTest1 {
public static void main ( String[ ] args) {
TreeMap< Student, String > treeMap = new TreeMap < > ( new Comparator < Student> ( ) {
@Override
public int compare ( Student o1, Student o2) {
int i = o1. getAge ( ) - o2. getAge ( ) ;
int j = i == 0 ? o1. getName ( ) . compareTo ( o2. getName ( ) ) : i;
return j;
}
} ) ;
treeMap. put ( new Student ( "马云" , 55 ) , "aaa" ) ;
treeMap. put ( new Student ( "马超" , 985 ) , "bbb" ) ;
treeMap. put ( new Student ( "马画藤" , 45 ) , "ccc" ) ;
treeMap. put ( new Student ( "高司令" , 25 ) , "ddd" ) ;
System. out. println ( treeMap) ;
}
}
class Student {
String name;
int age;
public Student ( ) {
}
public Student ( String name, int age) {
this . name = name;
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
@Override
public String toString ( ) {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}' ;
}
}
统计字符串中每个字符出现的次数
package org. westos. 作业;
import java. util. HashMap;
import java. util. Scanner;
public class Test1 {
public static void main ( String[ ] args) {
Scanner sc = new Scanner ( System. in) ;
System. out. println ( "请随便输入一段字符串" ) ;
String s = sc. nextLine ( ) ;
char [ ] ch = s. toCharArray ( ) ;
HashMap< Character, Integer> hashMap = new HashMap < > ( ) ;
for ( char c : ch) {
if ( ! hashMap. containsKey ( c) ) {
hashMap. put ( c, 1 ) ;
} else {
hashMap. put ( c, hashMap. get ( c) + 1 ) ;
}
}
StringBuilder sb = new StringBuilder ( ) ;
for ( Character key : hashMap. keySet ( ) ) {
sb. append ( key) . append ( "(" ) . append ( hashMap. get ( key) ) . append ( ")" ) ;
}
System. out. println ( sb) ;
}
}
HashMap嵌套HashMap
基础班
张三 20
李四 22
就业班
王五 21
赵六 23
package org. westos. 作业;
import java. util. HashMap;
import java. util. Set;
public class Test2 {
public static void main ( String[ ] args) {
HashMap< String, Integer> jcMap = new HashMap < > ( ) ;
jcMap. put ( "张三" , 20 ) ;
jcMap. put ( "李四" , 22 ) ;
HashMap< String, Integer> jyMap = new HashMap < > ( ) ;
jyMap. put ( "王五" , 21 ) ;
jyMap. put ( "赵六" , 23 ) ;
HashMap< String, HashMap< String, Integer> > allMap = new HashMap < > ( ) ;
allMap. put ( "基础班" , jcMap) ;
allMap. put ( "就业班" , jyMap) ;
Set< String> keySet = allMap. keySet ( ) ;
for ( String s : keySet) {
System. out. println ( s) ;
HashMap< String, Integer> stu = allMap. get ( s) ;
Set< String> keySet1 = stu. keySet ( ) ;
for ( String s1 : keySet1) {
System. out. println ( "\t" + s1 + "\t" + stu. get ( s1) ) ;
}
System. out. println ( ) ;
}
}
}
HashMap嵌套ArrayList
集合嵌套之HashMap嵌套ArrayList
假设HashMap集合的元素是ArrayList。有3 个。
每一个ArrayList集合的值是字符串。
三国演义
吕布
周瑜
笑傲江湖
令狐冲
林平之
神雕侠侣
郭靖
杨过
package org. westos. 作业;
import java. util. ArrayList;
import java. util. HashMap;
import java. util. Set;
public class Test3 {
public static void main ( String[ ] args) {
ArrayList< String> aList = new ArrayList < > ( ) ;
aList. add ( "吕布" ) ;
aList. add ( "周瑜" ) ;
ArrayList< String> aList1 = new ArrayList < > ( ) ;
aList1. add ( "令狐冲" ) ;
aList1. add ( "林平之" ) ;
ArrayList< String> aList2 = new ArrayList < > ( ) ;
aList2. add ( "郭靖" ) ;
aList2. add ( "黄蓉" ) ;
HashMap< String, ArrayList< String> > hashMap = new HashMap < > ( ) ;
hashMap. put ( "三国演义" , aList) ;
hashMap. put ( "笑傲江湖" , aList1) ;
hashMap. put ( "神雕侠侣" , aList2) ;
Set< String> keySets = hashMap. keySet ( ) ;
for ( String s : keySets) {
System. out. println ( s) ;
ArrayList< String> value = hashMap. get ( s) ;
for ( int i = 0 ; i < value. size ( ) ; i++ ) {
System. out. println ( "\t" + value. get ( i) ) ;
}
}
}
}
ArrayList嵌套HashMap
集合嵌套之ArrayList嵌套HashMap
假设ArrayList集合的元素是HashMap。有3 个。
每一个HashMap集合的键和值都是字符串。
周瑜-- - 小乔
吕布-- - 貂蝉
郭靖-- - 黄蓉
杨过-- - 小龙女
令狐冲-- - 任盈盈
林平之-- - 岳灵珊
import java. util. ArrayList;
import java. util. HashMap;
import java. util. Map;
import java. util. Set;
public class MyTest3 {
public static void main ( String[ ] args) {
HashMap< String, String> sgMap = new HashMap < > ( ) ;
sgMap. put ( "周瑜" , "小乔" ) ;
sgMap. put ( "吕布" , "貂蝉" ) ;
HashMap< String, String> sdMap = new HashMap < > ( ) ;
sdMap. put ( "郭靖" , "黄蓉" ) ;
sdMap. put ( "杨过" , "小龙女" ) ;
HashMap< String, String> xaMap = new HashMap < > ( ) ;
xaMap. put ( "令狐冲" , "任盈盈" ) ;
xaMap. put ( "林平之" , "岳灵珊" ) ;
ArrayList< HashMap< String, String> > bigList = new ArrayList < > ( ) ;
bigList. add ( sgMap) ;
bigList. add ( sdMap) ;
bigList. add ( xaMap) ;
for ( HashMap< String, String> hashMap : bigList) {
Set< Map. Entry< String, String> > entries = hashMap. entrySet ( ) ;
for ( Map. Entry< String, String> en : entries) {
String key = en. getKey ( ) ;
String value = en. getValue ( ) ;
System. out. println ( key+ "------" + value) ;
}
System. out. println ( ) ;
}
}
}
Collections
public static <T> void sort(List<T> list):排序,默认按照自然顺序
public static <T> int binarySearch(List<?> list,T key):二分查找
public static <T> T max(Collection<?> coll):获取最大值
public static void reverse(List<?> list):反转
public static void shuffle(List<?> list):随机置换
斗地主思路
模拟斗地主洗牌和发牌
模拟斗地主洗牌和发牌并对牌进行排序的代码实现
package org. westos. demo92;
import java. util. ArrayList;
import java. util. Collections;
public class 斗地主 {
public static void main ( String[ ] args) {
ArrayList< String> pokerBox = new ArrayList < > ( ) ;
String[ ] colors= { "♠" , "♥" , "♦" , "♣" } ;
String[ ] nums= { "A" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" } ;
for ( String color : colors) {
for ( String num : nums) {
String poker = color. concat ( num) ;
pokerBox. add ( poker) ;
}
}
pokerBox. add ( "☀" ) ;
pokerBox. add ( "☼" ) ;
Collections. shuffle ( pokerBox) ;
Collections. shuffle ( pokerBox) ;
Collections. shuffle ( pokerBox) ;
ArrayList< String> 星仔 = new ArrayList < > ( ) ;
ArrayList< String> 刀仔 = new ArrayList < > ( ) ;
ArrayList< String> 高进 = new ArrayList < > ( ) ;
ArrayList< String> 底牌 = new ArrayList < > ( ) ;
Collections. sort ( 星仔) ;
for ( int i = 0 ; i < pokerBox. size ( ) ; i++ ) {
if ( i>= pokerBox. size ( ) - 3 ) {
底牌. add ( pokerBox. get ( i) ) ;
} else if ( i% 3 == 0 ) {
星仔. add ( pokerBox. get ( i) ) ;
} else if ( i % 3 == 1 ) {
刀仔. add ( pokerBox. get ( i) ) ;
} else {
高进. add ( pokerBox. get ( i) ) ;
}
}
lookPoker ( "星仔" , 星仔) ;
lookPoker ( "刀仔" , 刀仔) ;
lookPoker ( "高进" , 高进) ;
lookPoker ( "底牌" , 底牌) ;
}
private static void lookPoker ( String name, ArrayList< String> list) {
System. out. println ( name) ;
for ( String poker : list) {
System. out. print ( poker+ "\t" ) ;
}
System. out. println ( ) ;
}
}
按照大小顺序
package org. westos. 作业;
import java. util. ArrayList;
import java. util. Collections;
import java. util. TreeMap;
import java. util. TreeSet;
public class Test8 {
}
class Demo3 {
public static void main ( String[ ] args) {
TreeMap< Integer, String> pokers = new TreeMap < > ( ) ;
String[ ] color = { "♥" , "♠" , "♣" , "♦" } ;
String[ ] number = { "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" , "2" } ;
ArrayList< Integer> indexs = new ArrayList < > ( ) ;
int index= 0 ;
for ( String numbers : number) {
for ( String colors : color) {
pokers. put ( index, colors. concat ( numbers) ) ;
indexs. add ( index) ;
index++ ;
}
}
System. out. println ( index) ;
pokers. put ( index, "大王" ) ;
indexs. add ( index) ;
index++ ;
pokers. put ( index, "小王" ) ;
indexs. add ( index) ;
Collections. shuffle ( indexs) ;
TreeSet< Integer> 星仔 = new TreeSet < > ( ) ;
TreeSet< Integer> 刀仔 = new TreeSet < > ( ) ;
TreeSet< Integer> 高进 = new TreeSet < > ( ) ;
TreeSet< Integer> 底牌 = new TreeSet < > ( ) ;
for ( int i = 0 ; i < indexs. size ( ) ; i++ ) {
if ( i>= indexs. size ( ) - 3 ) {
底牌. add ( indexs. get ( i) ) ;
} else if ( i% 3 == 0 ) {
星仔. add ( indexs. get ( i) ) ;
} else if ( i% 3 == 1 ) {
刀仔. add ( indexs. get ( i) ) ;
} else if ( i% 3 == 2 ) {
高进. add ( indexs. get ( i) ) ;
}
}
lookPoker ( "星仔" , 星仔, pokers) ;
lookPoker ( "刀仔" , 刀仔, pokers) ;
lookPoker ( "高进" , 高进, pokers) ;
lookPoker ( "底牌" , 底牌, pokers) ;
}
private static void lookPoker ( String name, TreeSet< Integer> indexs, TreeMap< Integer, String> pokers) {
System. out. print ( name+ "的牌是:" ) ;
for ( Integer index : indexs) {
System. out. print ( pokers. get ( index) + "\t" ) ;
}
System. out. println ( ) ;
}
}