Collections的简单学习

package com.hanchao.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

/**
 * java.util.Collections的简单学习
 * @author liweihan ([email protected])
 * @version 1.0 (2016年1月13日 上午10:20:51)
 */
public class TestCollections {

	public static void main(String[] args) {
		
		/**
		 * java.util.Collections 是一个包装类。它包含有各种有关集合的操作的静态多态方法。
		 * 此类不能实例化,就像一个工具类,服务于java的Collection框架。
		 * 
		 * 参考:
		 * 	  http://www.apihome.cn/api/java/Collections.html
		 * 
		 */
		
		/**
		 * 1.排序sort()
		 * 根据元素的自然顺序 对指定列表按升序进行排序。列表中的所有元素都必须实现 Comparable 接口。
		 * 此外,列表中的所有元素都必须是可相互比较的(也就是说,
		 * 对于列表中的任何 e1 和 e2 元素,e1.compareTo(e2) 
		 * 不得抛出 ClassCastException
		 */
		List<Double> list = new ArrayList<Double>();
		list.add(112D);
		list.add(113D);
		list.add(23D);
		list.add(456D);
		list.add(112D);
		list.add(231D);
		
		Collections.sort(list);
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
		//结果:23.0 112.0 113.0 231.0 456.0
		
		
		/**
		 * 2.混排
		 * 混排算法所做的正好与 sort 相反: 它打乱在一个 List 中可能有的任何排列的踪迹。
		 * 也就是说,基于随机源的输入重排该 List,这样的排列具有相同的可能性(假设随机源是公正的)。
		 * 这个算法在实现一个碰运气的游戏中是非常有用的。
		 * 例如,它可被用来混排代表一副牌的Card 对象的一个 List 。另外,在生成测试案例时,它也是十分有用的。
		 */
		Collections.shuffle(list);
		System.out.println("混排:");
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
		//结果:113.0 456.0 112.0 23.0 112.0 231.0
		
		
		/**
		 * 3.反转reverse()
		 * 使用Reverse方法可以根据元素的自然顺序 对指定列表按降序进行排序。
		 */
		Collections.reverse(list);
		System.out.println("反转:");
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
		//结果:231.0 112.0 23.0 112.0 456.0 113.0
		
		/**
		 * 4.fill()
		 * 使用指定元素替换指定列表中的所有元素。
		 */
		List<String> list2 = new ArrayList<String>();
		list2.add("aa");
		list2.add("bb");
		list2.add("cc");
		list2.add("dd");
		
		Collections.fill(list2,"hanchao");
		System.out.println("替换:");
		for(String string : list2) {
			System.out.println(string);
		}
		//结果:hanchao hanchao hanchao hanchao
		
		/**
		 * 5.copy()
		 * 将所有元素从一个列表复制到另一个列表。
		 * 执行此操作后,目标列表中每个已复制元素的索引将等同于源列表中该元素的索引。
		 * 目标列表的长度至少必须等于源列表。
		 * 如果目标列表更长一些,也不会影响目标列表中的其余元素。
		 * 
		 */
		List<String> list3 = new ArrayList<String>();
		list3.add("list31");
		list3.add("list32");
		list3.add("list33");
		
		List<String> list4 = new ArrayList<String>();
		list4.add("copy1");
		list4.add("copy2");
		list4.add("copy3");
//		list4.add("copy4");
		
		Collections.copy(list3, list4); //list4是源 ,list4的长度必须<=list3.size()
		System.out.println("复制:");
		for (String str : list3) {
			System.out.println(str);
		}
		
		/**
		 * 6.min()
		 * 根据元素的自然顺序 返回给定 collection 的最小元素。
		 * collection 中的所有元素都必须实现 Comparable 接口。
		 * 此外,collection 中的所有元素都必须是可相互比较的(也就是说,
		 * 对于 collection 中的任意 e1 和 e2 元素,e1.compareTo(e2) 
		 * 不得抛出 ClassCastException)。
		 * 
		 * max()
		 * 根据元素的自然顺序,返回给定 collection 的最大元素。
		 * collection 中的所有元素都必须实现 Comparable 接口。
		 * 此外,collection 中的所有元素都必须是可相互比较的(也就是说,
		 * 对于 collection 中的任意 e1 和 e2 元素,
		 * e1.compareTo(e2) 不得抛出 ClassCastException)。
		 */
		List<Double> list5 = new ArrayList<Double>();
		list5.add(112D);
		list5.add(113D);
		list5.add(23D);
		list5.add(456D);
		list5.add(112D);
		list5.add(231D);
		
		System.out.println("最小元素:" + Collections.min(list5));
		System.out.println("最大元素:" + Collections.max(list5));
		
		
		/**
		 * 7.indexOfSubList(List,list) 
		 *   返回指定源列表中第一次出现指定目标列表的起始位置;
		 *   如果没有出现这样的列表,则返回 -1。
		 *   
		 *   lastIndexOfSubList(List,list)
		 *   返回指定源列表中最后一次出现指定目标列表的起始位置;
		 *   如果没有出现这样的列表,则返回 -1。
		 *   
		 *   binarySearch(list,key)
		 *   查找指定集合中的元素,返回所查找元素的索引。
		 *   
		 */
		double array[] = {112, 111, 23, 456,111, 231 };
		List<Double> list6 = new ArrayList<Double>();
		List<Double> li = new ArrayList<Double>();
		for (int i = 0; i < array.length; i++) {
			list6.add(new Double(array[i]));
		}
		double arr[] = {111};
		for(int j=0;j<arr.length;j++){
			li.add(new Double(arr[j]));
		}
		int locations = Collections.lastIndexOfSubList (list6,li);
		int locations2 = Collections.indexOfSubList(list6, li);
		int locations3 = Collections.binarySearch(list6, 23d);
		System.out.println("=== lastIndexOfSubList:" + locations);
		System.out.println("=== indexOfSubList:" + locations2);
		System.out.println("=== binarySearch:" + locations3);
		//结果:=== lastIndexOfSubList:4
		//结果:=== indexOfSubList:1
		//结果:=== binarySearch:2
		
		/**
		 * 8.rotate()
		 * 根据指定的距离轮换指定列表中的元素。调用此方法后,
		 * 对于 0 和 list.size()-1(包括)之间的所有 i 值,
		 * 索引 i 处的元素将是以前位于索引 (i - distance) mod list.size() 处的元素。
		 * (此方法对列表的大小没有任何影响。) 
		 * 
		 */
		String[] str = {"t","a","n","k","s"};
		List<String> list7 = new ArrayList<String>();
		for (int i = 0; i < str.length; i++) {
			list7.add(str[i]);
		}
		
//		Collections.rotate(list7, -4);//如果是负数,则左移
		Collections.rotate(list7, 2);//如果是正数,则右移
		System.out.println("位移:");
		for(String s : list7) {
			System.out.println(s);
		}
		
		
		/**
		 * 9.replaceAll(list,old,new)
		 * 替换指定的元素,若要替换的值存在则返回true,反之则返回false
		 * 
		 */
		List<String> list8 = Arrays.asList("one two three four five six siven".split(" "));
		System.out.println(list8);
		//[one, two, three, four, five, six, siven]
		Collections.replaceAll(list8, "siven", "siven eight nine");
		System.out.println("---:" + list8);
		//---:[one, two, three, four, five, six, siven eight nine]
		
		/**
		 * 10.swap(List list,int i,int j)
		 * 在指定列表的指定位置处交换元素。
		 * (如果指定位置相同,则调用此方法不会更改列表。)
		 * 
		 */
		List<String> list10 = Arrays.asList("one two three four five six siven".split(" "));
		System.out.println("***:" + list10);
		//***:[one, two, three, four, five, six, siven]
		Collections.swap(list10, 3, 5);
		System.out.println("---:" + list10);
		//---:[one, two, three, six, five, four, siven]
		
		/**
		 * 11.nCopies(int n,Object 0)
		 * 返回大小为n的List ,List不可改变,其中的所有引用都指向o
		 */
		List<String> list11 = new ArrayList<String>();
		list11 = Collections.nCopies(5, "hello");
		System.out.println("$$$:" + list11);
		//$$$:[hello, hello, hello, hello, hello]
		
		/**
		 * 12.enumeration(Collection)
		 *    返回一个指定 collection 上的枚举。
		 *    此方法提供与遗留 API 的互操作性,遗留 API 需要一个枚举作为输入。 
		 */
		List<String> list12 = Arrays.asList("I love you xiao jie !".split(" "));
		System.out.println("---:" + list12);
		//---:[I, love, you, xiao, jie, !]
		Enumeration<String> e = Collections.enumeration(list12);
		Vector<String> v = new Vector<String>();
		while (e.hasMoreElements()) {
			v.addElement(e.nextElement());
		}
		System.out.println(" v :" + v);
		//v :[I, love, you, xiao, jie, !]
		
	}
	
}


你可能感兴趣的:(java,Collections)