HashSet LinkedHashSet示例代码
package collection.map.d1_set;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
public class SetDemo1 {
public static void main(String[] args) {
//set系类集合的特点:HashSet LinkedHashSet TreeSet
Set<String> sets=new HashSet<>();//无序,不重复,无索引
// Set<String> sets=new LinkedHashSet<>();//有序,不重复,无索引
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);
}
}
运行截图:
目标:
TreeSet对于有值对象排序 ,学会对自定义类型的对象进行指定规则排序。
示例代码:
SetDemo3(实现类):
package collection.map.d1_set;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
/**
目标:
TreeSet对于有值对象排序
学会对自定义类型的对象进行指定规则排序
*/
public class SetDemo3 {
public static void main(String[] args) {
Set<Integer> sets=new TreeSet<>();//不重复,无索引,可排序
sets.add(23);
sets.add(14);
sets.add(15);
sets.add(10);
sets.add(33);
System.out.println(sets);
Set<String> sets1=new TreeSet<>();//不重复,无索引,可排序
sets1.add("Java");
sets1.add("java");
sets1.add("About");
sets1.add("Mysql");
sets1.add("UI");
sets1.add("图图");
System.out.println(sets1);
System.out.println("-------------------------");
//方法二:集合自带的比较器进行规则制定
// Set<Apple> apples=new TreeSet<>(new Comparator<Apple>() {
// @Override
// public int compare(Apple o1, Apple o2) {
return o1.getWeight()-o2.getWeight();//重量比较升序
return o2.getWeight()-o1.getWeight();//重量比较降序
//
// //注意:浮点型建议直接Double.compare进行比较
return Double.compare(o1.getPrice(),o2.getPrice());//升序
// return Double.compare(o2.getPrice(),o1.getPrice());//降序
//
// }
// });
//可简化为一行代码
Set<Apple> apples=new TreeSet<>((Apple o1, Apple o2) -> Double.compare(o2.getPrice(),o1.getPrice()));//降序
Apple a1=new Apple("红富士","红",9.9,500);
Apple a2=new Apple("红富士2","红",19.9,600);
Apple a3=new Apple("红富士3","红",15.3,500);
Apple a4=new Apple("红富士3","红",20.1,100);
apples.add(a1);
apples.add(a2);
apples.add(a3);
apples.add(a4);
System.out.println(apples);
}
}
Apple类(苹果类):
package collection.map.d1_set;
import java.util.Objects;
public class Apple implements Comparable<Apple>{
private String name;
private String color;
private double price;
private int weight;
public Apple() {
}
public Apple(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;
}
/**
*
* @return
*/
@Override
public String toString() {
return "Apple{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
", price=" + price +
", weight=" + weight +
'}';
}
/**
* @param o
* @return
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Apple apple = (Apple) o;
return Double.compare(apple.price, price) == 0 && weight == apple.weight && Objects.equals(name, apple.name) && Objects.equals(color, apple.color);
}
@Override
public int hashCode() {
return Objects.hash(name, color, price, weight);
}
/**
* 方式一:o1.compareTo(o2)
* 类自定义比较规则
* @param o
* @return
*/
@Override
public int compareTo(Apple o) {
//按照重量比较
// return this.weight-o.weight;//去除重量重复的元素
return this.weight-o.weight>=0 ? 1 :-1;//保留重量相同的元素
}
}