Collection Interface
Included Signatures
size()
isEmpty()
contains(Object)
iterator()
toArray()
toArray(T[])
toArray(IntFunction<T[]>)
Example
System.out.println(list);
Student[] s = list.toArray(Student[]::new);
System.out.println(s);
for(Student x:s) {
System.out.println(x);
}
add(E)
remove(Object)
containsAll(Collection<?>)
addAll(Collection<? extends E>)
removeAll(Collection<?>)
removeIf(Predicate<? super E>)
retainAll(Collection<?>)
clear()
equals(Object)
hashCode()
spliterator()
stream()
parallelStream()
Collections Class
Sorting
Natural Order Sorting
List<Integer> list = Arrays.asList(4, 3, 8, 6, 9, 8, 10);
Collections.sort(list);
System.out.println(list);
Custom Comparator Sorting
Comparator<Integer> com = (o1, o2) -> o1 < o2 ? -1 : 1;
Collections.sort(list,com);
System.out.println(list);
Reverse Sorting
Collections.reverse(list);
System.out.println(list);
Swap by indexes
Collections.swap(list, 0, list.size()-1);
System.out.println(list);
Fill all the element with one value
Collections.fill(list, 5);
System.out.println(list);
Get the natural min value
Integer x = Collections.min(list);
System.out.println(x);
Get the custom min value
Comparator<Integer> com = (o1, o2) -> o1 > o2 ? -1 : 1;
Integer x = Collections.min(list, com);
System.out.println(x);
Get the natural max value
x = Collections.max(list);
System.out.println(x);
Binary search for a value and get the index
Collections.sort(list);
System.out.println(list);
Integer x = Collections.binarySearch(list, 8);
Find no of occurrence of a value
Integer x = Collections.frequency(list, 8);
System.out.println(x);
Sort Custom Classes
Method 1
public class Application {
public static void main(String[] args) {
List<Student> list = Arrays.asList(new Student(30, "Raja"), new Student(20, "Kumar"));
System.out.println(list);
Collections.sort(list);
System.out.println(list);
}
}
class Student implements Comparable<Student> {
private int age;
private String name;
public Student(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public int compareTo(Student incommingStudent) {
if (this.age > incommingStudent.age) {
return 1;
}
return -1;
}
@Override
public String toString() {
return "Student [age=" + age + ", name=" + name + "]";
}
}
Method 2 Using Lamda
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Application {
public static void main(String[] args) {
List<Student> list = Arrays.asList(new Student(30, "Raja"), new Student(20, "Kumar"));
System.out.println(list);
Comparator<Student> com = (o1, o2) -> o1.age < o2.age ? -1 : 1;
Collections.sort(list,com);
System.out.println(list);
}
}
class Student {
int age;
String name;
public Student(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Student [age=" + age + ", name=" + name + "]";
}
}
Method 3 Without Collections Class
Comparator<Student> com = (o1, o2) -> o1.age < o2.age ? -1 : 1;
list.sort(com);
System.out.println(list);
Comments
Post a Comment