Set系列集合特点
package com.itliuxue.d1_set;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
public class SetDemo01 {
public static void main(String[] args) {
//看看Set系列集合的特点:HashSet LinkedHashSet TreeSet
//无序不重复无索引
// Set<String> sets = new HashSet<>();//一行经典代码[Java, MySQL, HTML, SpringBoot]
//有序不重复无索引
Set<String> sets = new LinkedHashSet<>();//[MySQL, Java, HTML, SpringBoot]
sets.add("MySQL");
sets.add("MySQL");
sets.add("MySQL");
sets.add("Java");
sets.add("Java");
sets.add("HTML");
sets.add("HTML");
sets.add("SpringBoot");
sets.add("SpringBoot");
System.out.println(sets);
}
}
HashSet无序底层原理
HashSet集合底层采取哈希表存储的数据
哈希表是一种对于增删改查性能都较好的结构
哈希表的组成
JDK8之前的,底层使用数组+链表组成
JDK8开始后,底层采用数组+链表+红黑树组成
哈希值
是JDK根据对象的地址,按照某种规则算出来的int类型的数值。
Object类的API
public int hashCode():返回对象的哈希值
对象的哈希值特点
总结
HashSet元素去重复的底层原理
LinkedHashSet集合概述和特点
· 有序,不重复,无索引
有序的指的是保证存储和取出的元素顺序一致
TreeSet集合
package com.itliuxue.d1_set;
public class Apple5 implements Comparable<Apple5> {
private String name;
private String color;
private double price;
private int weight;
public Apple5() {
}
public Apple5(String name, String color, double price, int weight) {
this.name = name;
this.color = color;
this.price = price;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Apple5{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
", price=" + price +
", weight=" + weight +
'}';
}
//方式一,类自定义比较规则
@Override
public int compareTo(Apple5 o) {
return this.weight - o.weight;//将相等的值去掉,保留第一个
// return this.weight - o.weight >= 0 ? 1:-1;//将相等的值全部输出,保留重复元素
}
}
注意两个规则都有默认用集合自带规则
Collection集合总结
补充知识:可变参数 ![](https://i-blog.csdnimg.cn/blog_migrate/4d7d04bcc6a7c0ab3395a12fbf9a1fd8.png)
package d2_params;
import java.util.Arrays;
public class MethodDemo {
public static void main(String[] args) {
sum();//1.不传参数
sum(10,20);//2.传两个参数
sum(10,20,30);//3.传三个参数
sum(new int[]{10,20,30,40,50});
}
public static void sum(int...nums){
//可变参数在方法内部就是一个数组,nums
System.out.println("元素个数"+nums.length);
System.out.println("元素内容"+ Arrays.toString(nums));
}
}
补充知识:集合工具类Collections
package com.itliuxue.d1_set;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
//要观察TreeSet对于有值特性的数据如何排序
//学会对自定义类型的对象进行制定规则排序
public class SetDemo5 {
public static void main(String[] args) {
Set<Integer> sets = new TreeSet<>();//不重复,无索引,可排序
sets.add(12);
sets.add(13);
sets.add(16);
sets.add(15);
System.out.println(sets);
Set<String> sets1 = new TreeSet<>();
sets1.add("Java");
sets1.add("angela");
sets1.add("About");
sets1.add("base");
sets1.add("Python");
sets1.add("UI");
sets1.add("UI");
System.out.println(sets1);
System.out.println("-----------------");
//方式二集合自带比较器对象进行制定规则
Set<Apple5> apple = new TreeSet<>(new Comparator<Apple5>() {
@Override
public int compare(Apple5 o1, Apple5 o2) {
// return o1.getWeight() - o2.getWeight();//升序
// return -(o1.getWeight() - o2.getWeight());//降序
//注意:浮点型建议直接使用Double.price进行比较
return Double.compare(o1.getPrice(), o2.getPrice());
}
});
apple.add(new Apple5("富士","红色",9.9,500));
apple.add(new Apple5("嘎啦","条纹红色",10,400));
apple.add(new Apple5("黄嘎啦","黄偏红色",6.9,300));
apple.add(new Apple5("采青","青色",8.9,500));
System.out.println(apple+"\n");
}
}
package d3_collection;
public class Apple5 implements Comparable<Apple5> {
private String name;
private String color;
private double price;
private int weight;
public Apple5() {
}
public Apple5(String name, String color, double price, int weight) {
this.name = name;
this.color = color;
this.price = price;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Apple5{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
", price=" + price +
", weight=" + weight +
'}';
}
//方式一,类自定义比较规则
@Override
public int compareTo(Apple5 o) {
return this.weight - o.weight;//List集合存储相同大小的元素,他会保留
}
}
package d3_collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CollectionsDemo02 {
public static void main(String[] args) {
List<Apple5> apples = new ArrayList<>();//可以重复
apples.add(new Apple5("富士", "红色", 9.9, 500));
apples.add(new Apple5("嘎啦", "条纹红色", 10, 400));
apples.add(new Apple5("黄嘎啦", "黄偏红色", 6.9, 300));
apples.add(new Apple5("采青", "青色", 8.9, 500));
Collections.sort(apples);
System.out.println(apples);
System.out.println("--------------");
Collections.sort(apples, new Comparator<Apple5>() {
@Override
public int compare(Apple5 o1, Apple5 o2) {
return Double.compare(o1.getPrice(),o2.getPrice() );
}
});
System.out.println(apples);
}
}
斗地主案例
package d4_collection_test;
public class Card {
private String size;
private String color;
private int index;
public Card(String[] size, String[] color) {
}
public Card(String size, String color, int index) {
this.size = size;
this.color = color;
this.index = index;
}
public Card(String size, String color) {
this.size = size;
this.color = color;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String toString(){
return size + color;
}
}
package d4_collection_test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class GameDemo {
public static List<Card> allCards = new ArrayList<>();
//做牌
static {
String[] sizes = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
String[] colors = {"♠", "♦", "♥", "♣"};
int index = 0;
for (int i = 0; i < sizes.length; i++) {
index++;
for (int j = 0;j < colors.length;j++) {
Card c = new Card(sizes[i],colors[j],index);
allCards.add(c);
}
}
Card c1 = new Card("","大🃏",++index);
Card c2 = new Card("","小🃏",++index);
Collections.addAll(allCards,c1,c2);
System.out.println("新牌"+allCards);
}
public static void main(String[] args) {
//洗牌
Collections.shuffle(allCards);
System.out.println("洗牌后"+allCards);
//发牌(定义三个玩家,每个玩家的牌也是一个集合容器)
List<Card> g1 = new ArrayList<>();
List<Card> g2 = new ArrayList<>();
List<Card> g3 = new ArrayList<>();
//开始发牌(从集合中发出51张牌给三位玩家,剩余三张牌作为底牌)
for (int i = 0; i < allCards.size() - 3; i++) {
Card c = allCards.get(i);
if(i % 3 == 0){
//请g1接牌
g1.add(c);
}else if(i % 3==2){
g2.add(c);
}else{
g3.add(c);
}
}
List<Card> lastCard = allCards.subList(allCards.size()-3, allCards.size());
sortCards(g1);
sortCards(g2);
sortCards(g3);
//给玩家的牌排序(从大到小)
//输出玩家的牌
System.out.println("g1"+g1);
System.out.println("g2"+g2);
System.out.println("g3"+g3);
System.out.println("三张底牌"+lastCard);
}
//给玩家的牌排序
public static void sortCards(List<Card> cards){
Collections.sort(cards, new Comparator<Card>() {
@Override
public int compare(Card o1, Card o2) {
return o2.getIndex() - o1.getIndex();
}
});
}
}