Collection–Set集合–HashSet–TreeSet基础内容与实践应用
Set
Set集合的特点:
元素是不重复的,无序的!(存储和取出不一致,取决于底层HashMap实例)
子实现类
HashSet
TreeSet
HashSet
import java. util. HashSet;
import java. util. Set;
public class HashSestDemo {
public static void main ( String[ ] args) {
Set< Student> set = new HashSet < Student> ( ) ;
Student s1 = new Student ( "zoom" , 41 ) ;
Student s2 = new Student ( "zoom" , 41 ) ;
Student s3 = new Student ( "bin" , 35 ) ;
Student s4 = new Student ( "bin" , 35 ) ;
Student s5 = new Student ( "bin" , 38 ) ;
set. add ( s1) ;
set. add ( s2) ;
set. add ( s3) ;
set. add ( s4) ;
set. add ( s5) ;
for ( Student s : set) {
System. out. println ( s. getName ( ) + "---" + s. getAge ( ) ) ;
}
}
}
public class Student {
private String name ;
private int age ;
public Student ( ) {
super ( ) ;
}
public Student ( String name, int age) {
super ( ) ;
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 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 ;
Student other = ( Student) 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 ;
}
}
TreeSet
TreeSet集合:
本质基于TreeMap的底层实现(红黑树结构---->自平衡"的二叉树结构")
有两种排序方式:
自然排序
选择器排序
取决于创建当前Set集合对象的时候的构造方法
public TreeSet():默认的自然顺序排序
由于TreeSet属于Set集合(本身保证元素唯一,不重复的元素!),还可以将元素按照自然顺序排序
public class TreeSetDemo {
public static void main ( String[ ] args) {
TreeSet< Student> ts = new TreeSet < Student> ( ) ;
Student s1 = new Student ( "gaoyuanyuan" , 41 ) ;
Student s2 = new Student ( "gaoyuanyuan" , 41 ) ;
Student s3 = new Student ( "dengchao" , 37 ) ;
Student s4 = new Student ( "zhangyio" , 37 ) ;
Student s5 = new Student ( "wuqilong" , 50 ) ;
Student s6 = new Student ( "zhangguorong" , 20 ) ;
Student s7 = new Student ( "bybyzsd" , 29 ) ;
ts. add ( s1) ;
ts. add ( s2) ;
ts. add ( s3) ;
ts. add ( s4) ;
ts. add ( s5) ;
ts. add ( s6) ;
ts. add ( s7) ;
for ( Student s : ts) {
System. out. println ( s. getName ( ) + "---" + s. getAge ( ) ) ;
}
}
}
public class Student implements Comparable < Student> {
private String name ;
private int age ;
public Student ( ) {
super ( ) ;
}
public Student ( String name, int age) {
super ( ) ;
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 int compareTo ( Student s) {
int num = this . age - s. age ;
int num2 = ( num== 0 ) ? ( this . name. compareTo ( s. name) ) : num ;
return num2 ;
}
}
public class TreeSetDemo {
public static void main ( String[ ] args) {
TreeSet< Student> ts = new TreeSet < Student> ( new Comparator < Student> ( ) {
@Override
public int compare ( Student s1, Student s2) {
int num = s2. getAge ( ) - s1. getAge ( ) ;
int num2 = ( num== 0 ) ? ( s1. getName ( ) . compareTo ( s2. getName ( ) ) ) : num ;
return num2;
}
} ) ;
Student s1 = new Student ( "gaoyuanyuan" , 41 ) ;
Student s2 = new Student ( "gaoyuanyuan" , 41 ) ;
Student s3 = new Student ( "dengchao" , 37 ) ;
Student s4 = new Student ( "zhangyio" , 37 ) ;
Student s5 = new Student ( "wuqilong" , 50 ) ;
Student s6 = new Student ( "zhangguorong" , 20 ) ;
Student s7 = new Student ( "bybyzsd" , 29 ) ;
ts. add ( s1) ;
ts. add ( s2) ;
ts. add ( s3) ;
ts. add ( s4) ;
ts. add ( s5) ;
ts. add ( s6) ;
ts. add ( s7) ;
for ( Student s: ts) {
System. out. println ( s. getName ( ) + "---" + s. getAge ( ) ) ;
}
}
}
public class Student {
private String name ;
private int age ;
public Student ( ) {
super ( ) ;
}
public Student ( String name, int age) {
super ( ) ;
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 + "]" ;
}
}
Map集合
Map集合:
java.util.Map<K,V>:接口
HashMap
TreeMap
Java提供了集合:双列集合
键:K 学号:String "班级+学号id" 键的特点:必须唯一的!
值:V 学生Student
Map集合的特点: 一个键值对元素(一个键对应一个值,值是可以重复,但是键必须唯一)
只针对键有效,跟值无关!
Map和Collection集合的区别?
Map<K key,V value>集合:双列集合,键映射到值的对象 ,键必须保证唯一,
遍历方式和Collection集合遍历方式不同
Collection<E>集合:
单列集合:只能存储一种引用类型,里面的set集合依赖于Map集合的实现 (理解为:光棍)
HashSet---->HashMap里面put方法
TreeSet---->TreeMap里面的put方法
Map集合的功能
添加功能
V put(K key, V value) :添加一个键值对元素
问题:返回值是什么意思
如果当前键是一次存储的时候,返回的结果null
如果键是不是第一次存储,后面重复,将当前对应的值把以前的值覆盖掉并保存下来,返回以前的值!
刪除功能:
V remove(Object key):刪除指定的键,返回的是跟键关联的值,如果没有映射关系,则返回null
void clear():删除全部的键以及值
判断功能:
boolean containsKey(Object key):是否包含指定的键
boolean containsValue(Object value):是否包含指定的值
boolean isEmpty():判断Map集合是否为空
Map集合的遍历方式1
public class MapDemo {
public static void main ( String[ ] args) {
Map< String, String> map = new HashMap < String, String> ( ) ;
map. put ( "shy" , "上单" ) ;
map. put ( "rookie" , "中单" ) ;
map. put ( "ning" , "打野" ) ;
map. put ( "ming" , "辅助" ) ;
Set< String> set = map. keySet ( ) ;
for ( String key : set) {
String value = map. get ( key) ;
System. out. println ( key+ "---" + value) ;
}
}
}
Map集合的遍历方式2
public class MapDemo {
public static void main ( String[ ] args) {
Map< String, String> map = new HashMap < String, String> ( ) ;
map. put ( "zoom" , "上单" ) ;
map. put ( "yagao" , "中单" ) ;
map. put ( "sofm" , "打野" ) ;
map. put ( "baolan" , "辅助" ) ;
Set< Map. Entry< String, String> > entrySet = map. entrySet ( ) ;
for ( Map. Entry< String, String> entry : entrySet) {
String key = entry. getKey ( ) ;
String value = entry. getValue ( ) ;
System. out. println ( key+ "=" + value) ;
}
}
}
HashMap
HashMap<K,V>是Map集合的子实现类,里面哈希表结构,保证(键唯一)
Map集合只只针对键有效
HashMap<Integer,String>键:Integer
HashMap<String,Student> :键:String
HashMap<Student,String>:键是自定义对象
HashMap<Integer,String> 键:Integer值: String ---- 遍历
public class HashMapDemo {
public static void main ( String[ ] args) {
HashMap< Integer, String> hm = new HashMap < Integer, String> ( ) ;
hm. put ( 1 , "knight" ) ;
hm. put ( 2 , "angle" ) ;
hm. put ( 3 , "yagao" ) ;
Set< Integer> set = hm. keySet ( ) ;
for ( Integer key : set) {
String value = hm. get ( key) ;
System. out. println ( key+ "----" + value) ;
}
}
}
HashMap<String,Student> :键:String,值:Student ---- 遍历
public class HashMapDemo {
public static void main ( String[ ] args) {
HashMap< String, Student> hm = new HashMap < String, Student> ( ) ;
Student s1 = new Student ( "369" , 41 ) ;
Student s2 = new Student ( "369" , 41 ) ;
Student s3 = new Student ( "bin" , 38 ) ;
Student s4 = new Student ( "bin" , 27 ) ;
Student s5 = new Student ( "gimgoon" , 35 ) ;
Student s6 = new Student ( "karsa" , 39 ) ;
hm. put ( "7410" , s1 ) ;
hm. put ( "7411" , s2 ) ;
hm. put ( "7420" , s3 ) ;
hm. put ( "7421" , s4 ) ;
hm. put ( "7430" , s5 ) ;
hm. put ( "7430" , s6 ) ;
Set< String> set = hm. keySet ( ) ;
for ( String key: set) {
Student s = hm. get ( key) ;
System. out. println ( key+ "---" + s. getName ( ) + "---" + s. getAge ( ) ) ;
}
}
}
public class Student {
private String name ;
private int age ;
public Student ( ) {
super ( ) ;
}
public Student ( String name, int age) {
super ( ) ;
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 + "]" ;
}
}
HashMap<Student,String>:键是自定义对象 值为String ---- 遍历
public class HashMapDemo3 {
public static void main ( String[ ] args) {
HashMap< Student, String> hm = new HashMap < Student, String> ( ) ;
Student s1 = new Student ( "369" , 41 ) ;
Student s2 = new Student ( "369" , 41 ) ;
Student s3 = new Student ( "bin" , 38 ) ;
Student s4 = new Student ( "bin" , 27 ) ;
Student s5 = new Student ( "gimgoon" , 35 ) ;
Student s6 = new Student ( "karsa" , 39 ) ;
hm. put ( s1, "tes" ) ;
hm. put ( s2, "tes" ) ;
hm. put ( s3, "sn" ) ;
hm. put ( s4, "sn" ) ;
hm. put ( s5, "fpx" ) ;
hm. put ( s6, "tes" ) ;
Set< Student> set = hm. keySet ( ) ;
for ( Student key : set) {
String value = hm. get ( key) ;
System. out. println ( key. getName ( ) + "---" + key. getAge ( ) + "---" + value) ;
}
}
}
public class Student {
private String name ;
private int age ;
public Student ( ) {
super ( ) ;
}
public Student ( String name, int age) {
super ( ) ;
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 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 ;
Student other = ( Student) 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 ;
}
}
TreeMap
TreeMap:红黑树结构
构造方法:
public TreeMap():默认的自然顺序排序
public TreeMap(Comparator<? super K> comparator):是一种比较器排序(推荐)
TreeMap<Student,String> / TreeSet<Student>: 一定要有条件进行排序!
按照主要条件:学生的年龄从小到进行排序!
TreeSet集合存储自定义类型,什么情况下自然排序(Comparable),什么情况下是选择器排序(Comparator)?
执行无参构造方法:TreeSet<Student>():无参构造----是自然排序:
要求当前自定义类型需要实现Comparable接口,重写comparesTo方法
执行有参构造方法:TreeSet<Student>(Comparator<Student> com):
方式1:自定义一个类实现Comparator接口中的compare(T t1,T t2)方法
方式2:通过接口匿名内部类实现
TreeMap<Student,String> – 有参构造–匿名内部类方式 遍历
public class TreeMapDemo {
public static void main ( String[ ] args) {
TreeMap< Student, String> tm = new TreeMap < Student, String> ( new Comparator < Student> ( ) {
@Override
public int compare ( Student s1, Student s2) {
int num = s1. getAge ( ) - s2. getAge ( ) ;
int num2 = ( num == 0 ) ? ( s1. getName ( ) . compareTo ( s2. getName ( ) ) ) : num;
return num2;
}
} ) ;
Student s1 = new Student ( "369" , 19 ) ;
Student s2 = new Student ( "369" , 19 ) ;
Student s3 = new Student ( "knight" , 19 ) ;
Student s4 = new Student ( "karsa" , 23 ) ;
Student s5 = new Student ( "yuyanjia" , 21 ) ;
Student s6 = new Student ( "huanfeng" , 18 ) ;
Student s7 = new Student ( "loken" , 20 ) ;
Student s8 = new Student ( "zoom" , 22 ) ;
tm. put ( s1, "ddxw001" ) ;
tm. put ( s2, "ddxw002" ) ;
tm. put ( s3, "ddxw003" ) ;
tm. put ( s4, "ddxw004" ) ;
tm. put ( s5, "ddxw005" ) ;
tm. put ( s6, "ddxw006" ) ;
tm. put ( s7, "ddxw007" ) ;
tm. put ( s8, "ddxw008" ) ;
Set< Student> set = tm. keySet ( ) ;
for ( Student key : set) {
String value = tm. get ( key) ;
System. out. println ( key. getName ( ) + "-" + key. getAge ( ) + "--" + value) ;
}
}
}
public class Student {
private String name;
private int age;
public Student ( ) {
super ( ) ;
}
public Student ( String name, int age) {
super ( ) ;
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 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 ;
Student other = ( Student) 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 ;
}
}
Collection集合进行操作的工具类-----Collections
Collections: java. util. Collections 类
可以针对Collection集合进行操作的工具类!
public static < T> int binarySearch ( List< ? > list, T key) :
针对集合的二分查询方法: 查询key在集合中出现的索引值
public static < T> T max ( Collection< ? extends T > list) :
获取集合中最大值
public static < T> T min ( Collection< ? extends T > list) :
获取集合中最小值
public static < T> void sort ( List< T> list) :
针对List集合进行排序: 升序排序
public static < T> void sort ( List< T> list, Comparator< T> com) :
比较器排序
public static void shuffle ( List< ? > list) :
针对List集合的元素进行随机置换
模拟斗地主洗牌发牌
模拟斗地主洗牌和发牌
牌: 54张牌
3个人玩
没人17张
分析:
1)创建一个牌盒
集合: ArrayList
2)装牌
两个数组:
点数数组
A 2 3 4 5 6 7 8 9 10 J Q K
花色数组
♠ ♥ ♣ ♦
2.1)将点数数组遍历,并且将花色数组遍历
将点数和花色拼接起来 concat
2.2)需要将牌添加到ArrayList集合
3)洗牌
Collections 工具类
public static void shuffle(List<?> list)随机置换
4)发牌
三个玩家-->每个玩家看成集合 ArrayLIst
创建三个集合,存储对应自己的牌
创建一个集合,存储底牌
遍历牌盒
for(int x=0;x<array.size();x++){
//规律
//取底牌
如果
x>array.size()-3
获取底牌get()
如果
x%3 == 0
获取第一个玩家的牌
x%3 == 1
获取第一个玩家的牌
x%3 == 2
获取第一个玩家的牌
}
5)看牌
玩家1,玩家2,玩家3, 同时还看底牌
看牌功能 封装成 lookPoker(String name,ArrayList<String> array)
将array集合遍历: 获取每一张牌
import java. util. ArrayList;
import java. util. Collections;
import java. util. HashMap;
import java. util. TreeSet;
public class PokerDemo2 {
public static void main ( String[ ] args) {
HashMap< Integer, String> hm = new HashMap < Integer, String> ( ) ;
ArrayList< Integer> array = new ArrayList < Integer> ( ) ;
String[ ] colors = { "♥" , "♠" , "♣" , "♦" } ;
String[ ] numbers = { "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" , "2" } ;
int index = 0 ;
for ( String number: numbers) {
for ( String color: colors) {
String poker = color. concat ( number) ;
hm. put ( index, poker) ;
array. add ( index) ;
index ++ ;
}
}
hm. put ( index, "小王" ) ;
array. add ( index) ;
index ++ ;
hm. put ( index, "大王" ) ;
array. add ( index) ;
Collections. shuffle ( array) ;
TreeSet< Integer> player1 = new TreeSet < Integer> ( ) ;
TreeSet< Integer> player2 = new TreeSet < Integer> ( ) ;
TreeSet< Integer> player3 = new TreeSet < Integer> ( ) ;
TreeSet< Integer> diPai = new TreeSet < Integer> ( ) ;
for ( int x = 0 ; x < array. size ( ) ; x ++ ) {
if ( x>= array. size ( ) - 3 ) {
diPai. add ( array. get ( x) ) ;
} else if ( x % 3 == 0 ) {
player1. add ( array. get ( x) ) ;
} else if ( x % 3 == 1 ) {
player2. add ( array. get ( x) ) ;
} else if ( x % 3 == 2 ) {
player3. add ( array. get ( x) ) ;
}
}
lookPoker ( "the shy" , player1, hm) ;
lookPoker ( "bin" , player2, hm) ;
lookPoker ( "let me" , player3, hm) ;
lookPoker ( "底牌" , diPai, hm) ;
}
public static void lookPoker ( String name, TreeSet< Integer> ts, HashMap< Integer, String> hm) {
System. out. print ( name+ "的牌是:" ) ;
for ( Integer key: ts) {
String value = hm. get ( key) ;
System. out. print ( value+ " " ) ;
}
System. out. println ( ) ;
}
}