Differences between Collections and Collections and Common Methods of Collections

Posted by andy_b42 on Mon, 05 Aug 2019 07:18:47 +0200

1. Collection:
Collection is the upper interface of collection class. Set and List inherit Collection interface.
2.Collections:
Collections is a wrapper class inherited from the Object class, which contains polymorphic methods (static) that operate on collections. If the collection or class object provided to it is null, a null pointer exception is thrown when calling the method in the wrapper class.
Collections class methods:
(1) copy(List<? super T> dest, List<? extends T> src);
Copy all the elements in one list to another list. After copying, the order of the elements in the list will not be changed. We must ensure that the length of the destination list is longer than or equal to the length of the original list, otherwise the index crossing exception will be thrown.
src: Original List dest: Destination List
int srcSize = src.size();
if (srcSize > dest.size())
throw new IndexOutOfBoundsException("Source does not fit in dest");

public static void main(String[] args) {
		List<Integer> list1 =new ArrayList<>();
		for(int i =1;i<=5;i++){
			list1.add(i);
		}
		//List < Integer > List2 = new ArrayList <> (10); //Here 10 specifies only the capacity of the list, not the length of the list.
		//Arrays.alist(T...a) // Returns a list of fixed lengths supported by a specified array
		List<Integer> list2 = new ArrayList<>(Arrays.asList(new Integer[list1.size()]));
		Collections.copy(list2, list1);
		for (Integer integer : list2) {
			System.out.println(integer);
		}
	}
1
2
3
4
5

(2)disjoint(Collection<?> c1, Collection<?> c2)
Returns true if two specified collections do not share the same element. A collection cannot be empty.

public static void main(String[] args) {
		List<Integer> list1 =new ArrayList<>();
		for(int i =1;i<=5;i++){
			list1.add(i);
		}
		List<Integer> list2 =new ArrayList<>();
		for(int i =6;i<=10;i++){
			list2.add(i);
		}
		boolean flag = Collections.disjoint(list1, list2);
		System.out.println(flag);
	}
true

(3)fill(List<? super T> list, T obj)
Replace all elements of the specified list with the specified elements.

//Replace some elements of list set with fill method
public static void main(String[] args) {
		List<Integer> list1 =new ArrayList<>();
		for(int i =1;i<=5;i++){
			list1.add(i);
		}
		List<Integer> list2 =list1.subList(0, 2);//Intercept 1,2
		Collections.fill(list2,6);//Replace all intercepted elements with 6
		for (Integer integer : list1) {
			System.out.println(integer);
		}
	}
6
6
3
4
5

(4) indexOfSubList(List<?> source, List<?> target)
Returns the starting position of the first occurrence of the specified target list in the specified source list, and - 1 if no such event occurs.

public static void main(String[] args) {
		List<Integer> list1 =new ArrayList<>();
		for(int i =1;i<=5;i++){
			list1.add(i);
		}
		for(int i =1;i<=5;i++){
			list1.add(i);
		}
		//Traversing through elements in the original list
		for (Integer integer : list1) {
			System.out.print(integer+" ");
		}
		System.out.println();
		List<Integer> list2 =list1.subList(2, 4);//Intercept 3,4
		//Traversing through elements in sublists
		for (Integer integer : list2) {
			System.out.print(integer+" ");
		}
		System.out.println();
		//Returns the first occurrence of the sublist in the original list
		int index =Collections.indexOfSubList(list1,list2);
		System.out.println(index);
	}
1 2 3 4 5 1 2 3 4 5 
3 4 
2

(5)reverse(List<?> list)
Reverses the order of elements in the specified list.

public static void main(String[] args) {
		List<Integer> list1 =new ArrayList<>();
		for(int i =1;i<=5;i++){
			list1.add(i);
		}
		Collections.reverse(list1);
		for (Integer integer : list1) {
			System.out.println(integer);
		}
	}
5
4
3
2
1

(6)sort(List list)
Sort the specified list according to the natural ordering of its elements.

package Test02;

public class Student implements Comparable<Student>  {
	private String name;
	private double score;
	
	public Student() {
		super();
	}
	public Student(String name, double score) {
		super();
		this.name = name;
		this.score = score;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	@Override
	public int compareTo(Student o) {
		if(this.score<o.score){
			return -1;
		}else{
			return 1;	
		}
	}
}
public class TestSort {
	public static void main(String[] args) {
		Student s1 =new Student("Zhang San",80);
		Student s2 =new Student("Li Si",70);
		Student s3 =new Student("Wang Wu",90);
		List<Student> list =new ArrayList<>();
		list.add(s1);
		list.add(s2);
		list.add(s3);
		Collections.sort(list);
		for (Student student : list){
			System.out.println(student.getName()+"\t"+student.getScore());
		}
	}
}
Li Si 70.0
 Zhang San 80.0
 Wang Wu 90.0

(7)sort(List list, Comparator<? super T> c)
Sort the specified list according to the order caused by the specified comparator.

public class Student{
	private String name;
	private double score;
	
	public Student() {
		super();
	}
	public Student(String name, double score) {
		super();
		this.name = name;
		this.score = score;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
}
public class TestSort01 {
	public static void main(String[] args) {
		Student s1 =new Student("Zhang San",80);
		Student s2 =new Student("Li Si",70);
		Student s3 =new Student("Wang Wu",90);
		List<Student> list =new ArrayList<>();
		list.add(s1);
		list.add(s2);
		list.add(s3);
		Collections.sort(list,new Comparator<Student>(){
			@Override
			public int compare(Student o1, Student o2) {
				return (int)(o1.getScore()-o2.getScore());
			}
		});
		for (Student student : list) {
			System.out.println(student.getName()+"\t"+student.getScore());
		}
	}
}
Li Si 70.0
 Zhang San 80.0
 Wang Wu 90.0

(8)max/min(Collection<? extends T> coll)
Returns the maximum /(small) element of a given set in the natural order of its elements.

public static void main(String[] args) {
		List<Integer> list1 =new ArrayList<>();
		for(int i =1;i<=5;i++){
			list1.add(i);
		}
		System.out.println("list The largest element in a collection:"+Collections.max(list1));
		System.out.println("list The smallest element in a collection:"+Collections.min(list1));
	}
Maximum element in list set: 5
 Minimum element in list set: 1

There is no need to implement the Comparable interface here, because the wrapper class Integer already implements the interface.
public final class Integer extends Number implements Comparable{}

(9)max/min(Collection<? extends T> coll, Comparator<? super T> comp)
Returns the maximum /(small) element of a given set in the order triggered by the specified comparator.

public static void main(String[] args) {
		List<Integer> list1 =new ArrayList<>();
		for(int i =1;i<=5;i++){
			list1.add(i);
		}
		Integer intemax =Collections.max(list1, new Comparator<Integer>(){
			@Override
			public int compare(Integer o1, Integer o2) {
				return o1-o2;
			}	
		});
		Integer intemin =Collections.min(list1, new Comparator<Integer>(){
			@Override
			public int compare(Integer o1, Integer o2) {
				return o1-o2;
			}	
		});
		System.out.println("list The largest element in a collection:"+intemax);
		System.out.println("list The smallest element in a collection:"+intemin);
	}
Maximum element in list set: 5
 Minimum element in list set: 1