示例代码:
package collection.map.d3_collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsDemo01 {
public static void main(String[] args) {
//1.定义一个集合
List<String> names=new ArrayList<>();
//批量添加数据
Collections.addAll(names,"楚留香","张三","李四","马五");
System.out.println(names);
//2.打乱List集合元素顺序
Collections.shuffle(names);
System.out.println(names);
//3.将集合中的元素按照默认规则排序(排值特性元素,默认升序)
List<Integer> list=new ArrayList<>();
Collections.addAll(list,21,3,44,17,8);
Collections.sort(list);
System.out.println(list);
}
}
运行截图:
示例代码:
Apple类(苹果类)
package collection.map.d3_collections;
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;//保留重量相同的元素
}
}
CollectionDemo02类 (实现类)
package collection.map.d3_collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
利用Collections方法类自定义排序
*/
public class CollectionDemo02 {
public static void main(String[] args) {
List<Apple> apples=new ArrayList<>();
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);
//方法一
// Collections.sort(apples);//可以实现排序,Apple类已经重写比较规则
// System.out.println(apples);
//方法二
//sort自带比较器对象public static <T> void sort(List<T> list,Comparator<? super T>c)
//按照价格排序
// Collections.sort(apples, new Comparator<Apple>() {
// @Override
// public int compare(Apple o1, Apple o2) {
// return Double.compare(o1.getPrice(), o2.getPrice());
// }
// });
//简化
Collections.sort(apples, ( o1, o2) ->{return Double.compare(o1.getPrice(), o2.getPrice());});
System.out.println(apples);
}
}
运行截图: