Collection--HashSet--TreeSet基础内容与实践应用 模拟斗地主洗牌发牌

Collection–Set集合–HashSet–TreeSet基础内容与实践应用

Set
 Set集合的特点:
  		元素是不重复的,无序的!(存储和取出不一致,取决于底层HashMap实例)
  	子实现类
  		HashSet
  		TreeSet
HashSet
/*
	使用HashSet集合存储自定义对象并遍历
  	存储自定义对象,必须重写hashcode()和equals()
 */

import java.util.HashSet;
import java.util.Set;
public class HashSestDemo {	
	public static void main(String[] args) {
		//创建Set集合对象
		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集合(本身保证元素唯一,不重复的元素!),还可以将元素按照自然顺序排序
/*
	TreeSet集合无参构造方法
 		public TreeSet()
	使用TreeSet集合存储学生类型
*/
public class TreeSetDemo {	
	public static void main(String[] args) {
		//使用TreeSet无参构造方法创建对象
		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 + "]";
	}
	
	/**
	 * 重写Comparable接口中的compareTo方法
	 */
	@Override
	public int compareTo(Student s) { //s:后面需要进来的学生对象
		//return 0;
		//自己的比较业务!
		//提供一个条件:主要条件:按照学生的年龄从到大排序
		//年龄:int类型       
		int num = this.age - s.age ;  //41 - 37 > 0 ? 
										//37-37 		
		//自己分析次要条件:
		//如果年龄相同的话:姓名不一定一样!  姓名:String 
		//如果年龄相同,按照姓名的字典顺序比较,如果不相同
		int num2 = (num==0)? (this.name.compareTo(s.name)): num ;		
		return num2 ;
	}	
}
/*
	TreeSet集合有参构造方法
 		public TreeSet(Comparator<? super E> comparator)
*/
public class TreeSetDemo {	
	public static void main(String[] args) {
		//方式2:形参参数如果接口:传入接口的匿名内部类(本质:接口的子实现类)
		//匿名内部类(推荐的方式)
		/*
		 * 匿名内部类的格式:
		 * 		new 类名/接口名(){
		 * 				重写抽象类中或者接口中的抽象方法...
		 * 		};
		 * */		
		TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
			@Override
			public int compare(Student s1, Student s2) {
				//主要条件: 学生年龄的从大到小排序
				//s1---->this
				//s2---->s
				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
/*
高级功能:
  		方式1:(推荐的方式:Map常用的方式)
  			Set<K> keySet():获取所有的键的集合
  		   V get(Object key):通过键获取对应的值
*/
public class MapDemo {	
	public static void main(String[] args) {		
		//创建Map集合对象
		Map<String,String> map = new HashMap<String,String>() ;		
		//添加键值对元素
		map.put("shy", "上单") ;
		map.put("rookie", "中单") ;
		map.put("ning", "打野") ;
		map.put("ming", "辅助") ;		
		//遍历Map集合
		//方式1:Set<K> keySet():获取所有的键的集合
		Set<String> set = map.keySet()  ;
		//通过键在值
		for(String key : set) {
			//通过键获取值
			//V get(Object key):通过键获取对应的值
			String value = map.get(key) ;
			System.out.println(key+"---"+value);
		}
	}
}	
Map集合的遍历方式2
/*
  方式2遍历
  		Set<Map.Entry<K,V>> entrySet():获取当前Map集合中所有的键值对对象
  				
  			K getKey() :通过键值对对象获取键
  			V getValue():通过键值对对象获取值
  
*/
public class MapDemo {	
	public static void main(String[] args) {
		//创建Map集合
		Map<String,String> map = new HashMap<String,String>() ;		
		//添加元素
		map.put("zoom", "上单") ;
		map.put("yagao", "中单") ;
		map.put("sofm", "打野") ;
		map.put("baolan", "辅助") ;		
		//方式2:
		//获取所有的键值对对象
		Set<Map.Entry<String, String>> entrySet = map.entrySet() ;
		//遍历
		for(Map.Entry<String, String> entry :entrySet) {
			//获取到了所有的键值对对象:一一获取
			//K getKey():通过键值对对象获取键
			String key = entry.getKey() ;
			//V Value():通过键值对对象获取值
			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集合对象
		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) {		
		//创建Map集合对象
		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());
		}
	}
}
/*
运行结果
7420---bin---38
7430---karsa---39
7411---369---41
7410---369---41
7421---bin---27
*/
//Student类
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 ---- 遍历
/* 
 对于Map存储的键如果是自定义对象:该自定义对象的所在类必须重写Object:equals()和hashCode()
 		会依赖于HashMap的put方法
 			hash()---->依赖于hashCode():算出每一个对象哈希码值一样
 			putValue()---->equals()方法:哈希码值一样,还有比较每一个成员信息是否相同!
*/
public class HashMapDemo3 {	
	public static void main(String[] args) {		
		//创建HashMap集合对象
		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);
		}
	}
}
//Student类  -- 重写hashCode()和equals()方法
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);
			
		}
	}
}
//Student类  -- 重写hashCode()和equals()方法
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) {
		/**
		 * 1)创建牌盒
		 * 			HashMap<Integer,String> 
		 * 				键存储的是牌的编号
		 * 				值存储的是就是每一个牌(花色和点数的拼接)
		 * 			ArrayList<Integer>
		 * 				单独存储编号
		 */
		HashMap<Integer, String> hm = new HashMap<Integer,String>() ;
		ArrayList<Integer> array = new ArrayList<Integer>() ;
		
		//2)装牌
	String[] colors = {"♥","♠","♣","♦"} ;
	String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"} ;
		
		
		//编号从0开始
		int index = 0 ;
		//2.1)遍历点数数组和花色数组,拼接起来
		for(String number:numbers) {
			for(String color:colors) {
				//拼接
				String poker = color.concat(number) ;
				//将编号以及牌添加HashMap集合中
				hm.put(index, poker) ;
				//将编号存储在ArrayList集合中
				array.add(index) ;
				index ++ ;
			}
		}
		
		//2.2)存储小王
		hm.put(index, "小王") ;
		array.add(index) ;
		index ++ ;
		//存储大王
		hm.put(index, "大王") ;
		array.add(index) ;
		
		//测试
		//System.out.println(array);
		
		//3)洗牌:洗的是编号
		Collections.shuffle(array);
		//System.out.println(array);
		
		//4)发牌:发的编号
		//发的也是编号,为了保证每个人手上的有序,所以三个玩家都需要TreeSet<Integer>
		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) {
				//玩家1
				player1.add(array.get(x)) ;
			}else if(x %3  ==1) {
				//玩家2
				player2.add(array.get(x)) ;
				
			}else if(x % 3 == 2) {
				//玩家3
				player3.add(array.get(x)) ;
			}
		}
		
		//5看牌:
		//将看牌封装成功能
		//单独调用
		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+"的牌是:");
		//需要遍历TreeSet集合获取每一个人手上的编号
		for(Integer key: ts) {
			//获取到了每一个编号
			//在HashMap集合中:通过键获取值:获取的牌
			String value = hm.get(key) ;
			System.out.print(value+"  ");
		}
		System.out.println();
	}
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值