java基础 之 Collections类

Collections类

  • 集合工具类,用来对集合进行操作。
  • public static <T> boolean addAll(Collection<T> c, T... elements):往集合中添加一些元素。
  • public static void shuffle(List<?> list) 打乱顺序:打乱集合顺序。
public static void main(String[] args) {
        ArrayList<Integer> coll=new ArrayList<>();
        //addAll()往集合中添加一些元素,是静态方法,所以直接用类名调用。
        Collections.addAll(coll,1,2,3,4,5);
        System.out.println(coll);
        //打乱集合顺序
        Collections.shuffle(coll);
        System.out.println(coll);
    }

运行结果:

[1, 2, 3, 4, 5]
[2, 1, 4, 3, 5]
  • public static <T> void sort(List<T> list):将集合中元素按照默认规则排序。
public class CollectionsDemo {
    public static void main(String[] args) {
        ArrayList<Integer> coll=new ArrayList<>();
        Collections.addAll(coll,1,4,2,3,6,5);
        System.out.println(coll);
        //将集合中元素按照默认规则排序
        Collections.sort(coll);
        System.out.println(coll);
        //将集合中元素按照指定规则排序
        ArrayList coll1=new ArrayList();
        HashSetDemo p1=new HashSetDemo("fjc",15);
        HashSetDemo p2=new HashSetDemo("lwh",22);
        HashSetDemo p3=new HashSetDemo("ltf",20);
        Collections.addAll(coll1, p1, p2, p3);
        System.out.println(coll1);
        Collections.sort(coll1);
        System.out.println(coll1);
    }
}
  • 自定义排序规则:
  • 自己(this)- 参数 就是升序排序。
public class HashSetDemo implements Comparable<HashSetDemo> {
		//重写compareTo方法就可以实现自定义排序。
		@Override
    	public int compareTo(HashSetDemo o) {
        	//return 0;//认为元素都是相同的
        	return this.getAge()-o.getAge();//按年龄升序排序
    }
}
  • public static <T> void sort(List<T> list,Comparator<? super T> ):将集合中元素按照指定规则排序。
 public static void main(String[] args) {
        ArrayList<Integer> coll=new ArrayList<>();
        Collections.addAll(coll,1,4,2,3,6,5);
        System.out.println(coll);
        //将集合中元素按照默认规则排序
        Collections.sort(coll, new Comparator<Integer>() {
        	//重写比较的方法。
            @Override
            public int compare(Integer o1, Integer o2) {
                //升序
                //return o1-o2;
                //降序
                return o2-o1;
            }
        });
        System.out.println(coll);
    }
}

运行结果:

[1, 4, 2, 3, 6, 5]
[6, 5, 4, 3, 2, 1]
public static void main(String[] args) {
        ArrayList coll1=new ArrayList();
        HashSetDemo p1=new HashSetDemo("fjc",15);
        HashSetDemo p2=new HashSetDemo("lwh",22);
        HashSetDemo p3=new HashSetDemo("ltf",20);
        Collections.addAll(coll1, p1, p2, p3);
        System.out.println(coll1);
        Collections.sort(coll1, new Comparator<HashSetDemo>() {
            @Override
            public int compare(HashSetDemo o1, HashSetDemo o2) {
                //升序
                //return o1.getAge()-o2.getAge();
                //降序
                return o2.getAge()-o1.getAge();
            }
        });
        System.out.println(coll1);
    }

运行结果:

[HashSet{name='fjc', age=15}, HashSet{name='lwh', age=22}, HashSet{name='ltf', age=20}]
[HashSet{name='lwh', age=22}, HashSet{name='ltf', age=20}, HashSet{name='fjc', age=15}]
  • 如果年龄相同,按姓名首字母排序。
Collections.sort(coll1, new Comparator<HashSetDemo>() {
            @Override
            public int compare(HashSetDemo o1, HashSetDemo o2) {
                int result=o1.getAge()-o2.getAge();
                if(result==0){
                    result=o1.getName().charAt(0)-o2.getName().charAt(0);
                }
                return result;
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值