Collection类相关(方法、Iterator)

本文详细介绍了Java中的Collections类,包括如何对List进行排序(如reverse、shuffle、sort)、查找与替换元素(如binarySearch、max、min、fill、replaceAll)以及集合元素的复制。通过示例代码展示了这些方法的使用,帮助开发者更好地理解和应用Collections类的功能。

Collections 类是 Java 提供的一个操作 Set、List 和 Map 等集合的工具类。Collections 类提供了许多操作集合的静态方法,借助这些静态方法可以实现集合元素的排序、查找替换和复制等操作。下面介绍 Collections 类中操作集合的常用方法。

排序

Collections 提供了如下方法用于对 List 集合元素进行排序。

  • void reverse(List list):对指定 List 集合元素进行逆向排序。
  • void shuffle(List list):对 List 集合元素进行随机排序(shuffle 方法模拟了“洗牌”动作)。
  • void sort(List list):根据元素的自然顺序对指定 List 集合的元素按升序进行排序。
  • void sort(List list, Comparator c):根据指定 Comparator 产生的顺序对 List 集合元素进行排序。
  • void swap(List list, int i, int j):将指定 List 集合中的 i 处元素和 j 处元素进行交换。
  • void rotate(List list, int distance):当 distance 为正数时,将 list 集合的后 distance 个元素“整体”移到前面;当 distance 为负数时,将 list 集合的前 distance 个元素“整体”移到后面。该方法不会改变集合的长度。
public static void main(String[] args) {
		List<Integer> num = new ArrayList<Integer>();
		num.add(1);
		num.add(2);
		num.add(5);
		num.add(3);
		num.add(-1);
		System.out.println(num);
		
		System.out.println("排序之后的:");
		Collections.sort(num);
		System.out.println(num);
		
		System.out.println("逆向排序之后的:");
		Collections.reverse(num);
		System.out.println(num);
		
		System.out.println("rotate之后的:");
		Collections.rotate(num, 1);
		System.out.println(num);
	}
//			[1, 2, 5, 3, -1]
//			排序之后的:
//			[-1, 1, 2, 3, 5]
//			逆向排序之后的:
//			[5, 3, 2, 1, -1]
//			rotate之后的:
//			[-1, 5, 3, 2, 1]

查找、替换操作

Collections 还提供了如下常用的用于查找、替换集合元素的方法。

  • int binarySearch(List list, Object key):使用二分搜索法搜索指定的 List 集合,以获得指定对象在 List 集合中的索引。如果要使该方法可以正常工作,则必须保证 List 中的元素已经处于有序状态。
  • Object max(Collection coll):根据元素的自然顺序,返回给定集合中的最大元素。
  • Object max(Collection coll, Comparator comp):根据 Comparator 指定的顺序,返回给定集合中的最大元素。
  • Object min(Collection coll):根据元素的自然顺序,返回给定集合中的最小元素。
  • Object min(Collection coll, Comparator comp):根据 Comparator 指定的顺序,返回给定集合中的最小元素。
  • void fill(List list, Object obj):使用指定元素 obj 替换指定 List 集合中的所有元素。
  • int frequency(Collection c, Object o):返回指定集合中指定元素的出现次数。
  • int indexOfSubList(List source, List target):返回子 List 对象在父 List 对象中第一次出现的位置索引;如果父 List 中没有出现这样的子 List,则返回 -1。
  • int lastIndexOfSubList(List source, List target):返回子 List 对象在父 List 对象中最后一次出现的位置索引;如果父 List 中没有岀现这样的子 List,则返回 -1。
  • boolean replaceAll(List list, Object oldVal, Object newVal):使用一个新值 newVal 替换 List 对象的所有旧值 oldVal。

复制

Collections 类的 copy() 静态方法用于将指定集合中的所有元素复制到另一个集合中。执行 copy() 方法后,目标集合中每个已复制元素的索引将等同于源集合中该元素的索引。

public static void copyTest() {
		List srcList = new ArrayList();
        List destList = new ArrayList();
        destList.add("苏打水");
        destList.add("木糖醇");
        destList.add("方便面");
        destList.add("火腿肠");
        destList.add("冰红茶");
        srcList.add("燕麦片");
        System.out.println(destList);
        Collections.copy(destList, srcList);
        System.out.println(destList);
	}
//	 [苏打水, 木糖醇, 方便面, 火腿肠, 冰红茶]
//	 [燕麦片, 木糖醇, 方便面, 火腿肠, 冰红茶]

Java Iterator(迭代器)

Java Iterator(迭代器)不是一个集合,它是一种用于访问集合的方法,可用于迭代ArrayList和HashSet等集合。

public static void main(String[] args) {
		List<Integer> num = new ArrayList<Integer>();
		num.add(1);
		num.add(2);
		num.add(5);
		num.add(3);
		num.add(-1);
		Iterator<Integer> iterator = num.iterator();
		while(iterator.hasNext()) {
			Integer integer = iterator.next();
			if (integer < 3) {
				iterator.remove();
			}
		}
		System.out.println(num);
}

// 输出
//[5, 3]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值