蓝桥杯复习,Collections的使用

Collections类是什么

在这里插入图片描述

Collections类的使用


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

public class Test04 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/**
		 * Collections类就是对集合的操作类
		 * 	具体可以去看API的简介
		 * 	这里只介绍三个方法
		 */
		ArrayList<Integer> arrayList=new ArrayList<>();
		arrayList.add(5);
		arrayList.add(3);
		arrayList.add(4);
		arrayList.add(2);
		arrayList.add(1);
		Collections.sort(arrayList);
		System.out.println(arrayList.get(0));
		System.out.println(arrayList.get(arrayList.size()-1));
		Integer max = Collections.max(arrayList);
		Integer min = Collections.min(arrayList);
		System.out.println("max="+max);
		System.out.println("min"+min);
		
		HashSet<Character> hash=new HashSet<>();
		hash.add('c');
		hash.add('b');
		hash.add('a');
		hash.add('a');
		hash.add('f');
		hash.add('k');
		System.out.println(Collections.max(hash));
		System.out.println(Collections.min(hash));
		
		//获取集合中的最大值
		Collections.max(hash);
		//获取集合中的最小值
		Collections.min(arrayList);
		//对链表进行排序,(只可以对链表进行排序,因为其他数据结构存储元素的位置都是和元素的值有关系的)
		Collections.sort(arrayList);
		
		
	}

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值