Java中集合操作工具类Collections

 示例代码:

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);

    }
}

运行截图:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值