Collections class operation collection

Collections class operation collection

Collections class is a tool class for manipulating collections such as Set,List and Map provided by Java. Collections provides many static methods for manipulating collections, with which collection elements can be implemented
Sort, find, replace, and copy. The following describes the methods commonly used in the colactions class

Sort (forward and reverse)

The Collections class provides the following methods to sort the List collection elements

  • Void reverse (list): reverse sort the specified set of elements
List<Integer> list=new ArrayList<>();
list.add(100);
list.add(3);
list.add(23);
System.out.println("before revesed");
list.forEach(x-> System.out.println(x));
Collections.reverse(list)
System.out.println("after revesed");
list.forEach(x-> System.out.println(x));
//Operation results
before revesed
100
3
23
after revesed
23
3
100
  • Void shuffle (list): randomly sort the list set, that is, disrupt the order
List<Integer> list=new ArrayList<>();
list.add(100);
list.add(3);
list.add(23);
System.out.println("before shuffled");
list.forEach(x-> System.out.println(x));
Collections.shuffle(list);
System.out.println("after shuffled");
list.forEach(x-> System.out.println(x));
//Operation results
before shuffled
100
3
23
after shuffled
3
23
100
  • Void sort (list): sort the incoming collection in ascending order
List<Integer> list=new ArrayList<>();
list.add(100);
list.add(3);
list.add(23);
System.out.println("before sorted");
list.forEach(x-> System.out.println(x));
Collections.sort(list);
System.out.println("after sort");
list.forEach(x-> System.out.println(x));

//Operation results
before sorted
100
3
23
after sort
3
23
100

  • Void sort (list, Comparator C): sort the list according to the rules specified by Comparator
//Define student classes
class Student implements Cloneable {
    String name;
    int age;

    public Student clone() throws CloneNotSupportedException {
        Student stu = (Student) super.clone();
        return stu;
    }


    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}


//Sort by custom comparator
List<Student> students=new ArrayList<>();
Student stu1=new Student("a",19);
Student stu2=new Student("b",25);
Student stu3=new Student("c",45);
students.add(stu3);
students.add(stu2);
students.add(stu1);
System.out.println("before sort");
for (Student student : students) {
    System.out.println(student.toString());
}
Collections.sort(students, new Comparator<Student>() {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge()-o2.getAge();
    }
});
System.out.println("after sorted");
for (Student student : students) {
    System.out.println(student.toString());
}
//Operation results
before sort
Student{name='c', age=45}
Student{name='b', age=25}
Student{name='a', age=19}
after sorted
Student{name='a', age=19}
Student{name='b', age=25}
Student{name='c', age=45}
  • Void swap (list, int i, int j): call the two elements in the following table in the set list
List<Student> students=new ArrayList<>();
Student stu1=new Student("a",19);
Student stu2=new Student("b",25);
Student stu3=new Student("c",45);
//students.add(stu3);
students.add(stu2);
students.add(stu1);
System.out.println("before swap");
for (Student student : students) {
    System.out.println(student.toString());
}

Collections.swap(students,0,1);
System.out.println("after swapped");
for (Student student : students) {
    System.out.println(student.toString());
}
//Operation results
before swap
Student{name='b', age=25}
Student{name='a', age=19}
after swapped
Student{name='a', age=19}
Student{name='b', age=25}
  • Void rotate (list, int distance): rotate the set. Rotate to the right when distance is greater than zero and to the left when distance is less than 0
List<Integer> list=new ArrayList<>();
list.add(100);
list.add(3);
list.add(23);

System.out.println("before rotate");
list.forEach(x-> System.out.println(x));
Collections.rotate(list,1);
System.out.println("after rotated");
list.forEach(x-> System.out.println(x));
//Operation results
before rotate
100
3
23
after rotated
23
100
3

Find and replace operations

The Collections class also provides the following methods that are often used to find and replace collection elements

  • Int binarysearch (list, object key): use the binary search method to return the position index of elements in the collection. The collection must be ordered
List<Integer> list=new ArrayList<>();
list.add(3);
list.add(23);
list.add(100);

int index_23=Collections.binarySearch(list,100);
System.out.println("100 The following table is"+index_23);

//Operation results
100 The following table is 2
  • Void max (list): returns the largest element in the collection
  • Void min (list): returns the smallest element in the set
  • Void max (list, comparator com): find the largest element according to the comparison rules provided
  • Void min (list, comparator com): find the smallest element according to the comparison rules provided
List<Integer> list=new ArrayList<>();
list.add(3);
list.add(23);
list.add(100);
System.out.println("Max="+Collections.max(list));
System.out.println("Min="+Collections.min(list));
  • Void fill (list, object obj): replace all elements in the collection with the specified element obj
List<Integer> list=new ArrayList<>(10);
list.add(3);
list.add(23);
list.add(100);
Collections.fill(list,10);
list.forEach(x-> System.out.println(x));
//Operation results
10
10
10

  • Int frequency (list, object obj): calculates the frequency of occurrence of the specified element in the collection
List<Integer> list=new ArrayList<>(10);
list.add(3);
list.add(23);
list.add(100);
list.add(100);
list.add(100);
list.add(100);

int f=Collections.frequency(list,100);
System.out.println(f);
//The result of the operation is
4

  • int indexOfSubList(List source,List target): returns the position where the List object first appears in the parent List object. If it does not exist in the parent List object, it returns - 1;
List<Integer> list=new ArrayList<>(10);
list.add(3);
list.add(23);
list.add(100);
list.add(100);
list.add(100);
list.add(100);

List<Integer> l=new ArrayList<>();
l.add(100);
l.add(100);
int index=Collections.indexOfSubList(list,l);
System.out.println(index);

  • int indexOfLastSubList(List source,List target): returns the index of the last position of the child list in the parent list. If it does not exist, return - 1
  • Boolean replaceall (list, object oldval, object newval): replace all old values in the collection with new values
List<Integer> list=new ArrayList<>(10);
list.add(3);
list.add(23);
list.add(100);
list.add(100);
list.add(100);
list.add(100);

Collections.replaceAll(list,100,50);
list.forEach(x-> System.out.println(x));

//Operation results
3
23
50
50
50
50

copy

  • The static method in the Collections class is used to copy all the elements in the specified collection to another collection. Execute copy()

Reference articles

Keywords: Java Algorithm data structure

Added by leeue on Sun, 20 Feb 2022 14:48:56 +0200