JavaSE——day15Collections

   Collection和Collections的区别:

            Collection 是顶层次单列集合的根接口,是一个集合,是一个接口。

            Collections 是针对集合操作的工具类,此类有一些功能:随机置换,集合中的二分查找法,reverse()方法。。。。


Collections集合    

        此集合没有构造方法,所有的方法都是静态方法,也就意味着Collections中的 所有方法都是破坏性的方法。

方法摘要:

static
<T> int
binarySearch(List<? extends Comparable<? super T>> list, T key)
          使用二分搜索法搜索指定列表,以获得指定对象。
static
<T> int
binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
          使用二分搜索法搜索指定列表,以获得指定对象。
static void reverse(List<?> list)
          反转指定列表中元素的顺序。
static void shuffle(List<?> list, Random rnd)
          使用指定的随机源对指定列表进行置换。
例子

    reverse()例子(部分代码):

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

public class Test1base {

	public static void main(String[] args) {
		
		ArrayList<Student> arrayList = new ArrayList<Student>();
		
		arrayList.add(new Student("aloha", 22));
		arrayList.add(new Student("alohaa", 222));
		arrayList.add(new Student("alohaaa", 2222));
		arrayList.add(new Student("alohaaaa", 22222));
		
		for(Student s : arrayList) {
			System.out.println(s.getName() + "---"+s.getAge());
		}
		System.out.println("----------------------");

		Collections.reverse(arrayList);
		
		for(Student s : arrayList) {
			System.out.println(s.getName() + "---"+s.getAge());
		}
	}
}

结果:


二分查找法(二分查找法要求   只能操作有序的集合   并且是   List接口的子实现类):

import java.util.Collections;
import java.util.List;
import java.util.Vector;

public class Test2base {

	public static void main(String[] args) {
		
		List<Integer> list = new Vector<Integer>() ;
		
		list.add(45);
		list.add(20);
		list.add(70);
		list.add(10);
		list.add(2);
		
		//有Vector集合的特性知道此集合自动排序
		System.out.println(Collections.binarySearch(list, 10)) ;
		
		Collections.sort(list);
		System.out.println(Collections.binarySearch(list, 10)) ;
	}
}
结果:


可以看到,没有排序的二分查找法是无法找到元素的位置的。

随机置换shuffle

例子:

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

public class Test3base {

	public static void main(String[] args) {
		
		ArrayList<Integer> arrayList = new ArrayList<Integer>();
		
		arrayList.add(10);
		arrayList.add(101);
		arrayList.add(24);
		arrayList.add(20);
		arrayList.add(77);
		
		System.out.println("随机置换前: "+arrayList);
		Collections.shuffle(arrayList);
		System.out.println("随机置换后: "+arrayList);
	}
}

结果:


这个方法可以用在洗牌、抽奖等项目中。

    Collections类中的两种排序方法

    1、使用比较器进行排序

	public void sort() {
		
		//创建对象
		ArrayList<Student> al = new ArrayList<Student>() ;
		//加入Student对象
		al.add(new Student("alohaaa", 2222)) ;
		al.add(new Student("alohaa", 2222)) ;
		al.add(new Student("alohaaaaa", 222222)) ;
		al.add(new Student("aloha", 22)) ;
		al.add(new Student("alohaaa", 22222)) ;
		
		Collections.sort(al, new Comparator<Student>() {


			@Override
			public int compare(Student o1, Student o2) {
				int num = o1.getAge() - o2.getAge() ;
				int num2 = num == 0 ? o1.getName().length()-o2.getName().length() : num ;
				int num3 = num2 == 0 ? o1.getName().compareTo(o2.getName()) : num2;
				
				return num3;
			}
		});
		for(Student s : al) {
			System.out.println(s.getName()+"\t\t"+ s.getAge());
		}
	}
 
 
    2、自然排序
        重写子类中的compareTo()方法,在调用者中直接调用Collections.sort()即可。
例子:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Sort {

	public static void main(String[] args) {
		
		//创建集合对象
		List<Student> list = new ArrayList<Student>() ;
		
		list.add(new Student("aloha", 22));
		list.add(new Student("nihao", 15));	
		list.add(new Student("你好", 10));
		list.add(new Student("hello", 25));	
		
		//排序
		Collections.sort(list);
		//遍历
		for(Student s : list) {
			System.out.println(s.getName()+ "\t"+ s.getAge());
		}
	}
}

class Student implements Comparable<Student>{
	private String name ;
	private int age ;
	public Student() {
		super();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public int compareTo(Student o) {
		int num = this.age - o.age ;
		return num ;
	}

}


实时

猜你喜欢

转载自blog.csdn.net/weixin_38930706/article/details/80312694