Friday, January 16, 2026

Mutable and Immutable Lists - The Many Ways

---------------------------------------------------------------

Suppose there is an Employee Array like below statement

Employee[] employee = empoyeeService.getEmployee(employeeId);

and 

(1)if we want an Immutable List then we can give the below 

List<Rating> ratings2 = Arrays.stream(ratings1).toList();

(2)But if we want an Mutable List then we have to give like the below 2 Options

// Option 1: Using Collectors.toList() (returns ArrayList which is mutable)

List<Employee> employee = Arrays.stream(employee).collect(Collectors.toList());

// Option 2: Using Collectors.toCollection() (explicitly specify the List implementation)

List<Employee> employee = Arrays.stream(employee)

                            .collect(Collectors.toCollection(ArrayList::new));

Monday, January 12, 2026

How to do Customized SortingOrder For Custom Class Objects


(1)

public class Student {

private int studentRollNo;

private String studentName;

public Student(int studentRollNo, String studentName) {

this.studentRollNo = studentRollNo;

this.studentName = studentName;

}

public int getStudentRollNo() {

return studentRollNo;

}

public String getStudentName() {

return studentName;

}

public String toString() {

return "\n Student Roll Number is " + studentRollNo + " and Student Name is " + studentName + "\n";

}

}



(2)

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashSet;

import java.util.List;


public class CustomizedSortingOrderForCustomClassObjects {

public static void main(String[] args) {

HashSet<Student> hashSetStudentObj = new HashSet<Student>();

hashSetStudentObj.add(new Student(7,"Krishna"));

hashSetStudentObj.add(new Student(2,"Raghavendra"));

hashSetStudentObj.add(new Student(5,"Kumari"));

hashSetStudentObj.add(new Student(3,"Prasad"));

hashSetStudentObj.add(new Student(1,"Sachin"));

hashSetStudentObj.add(new Student(2,"Rajagopal"));

List<Student> arrayListTransformedIs = new ArrayList<Student>(hashSetStudentObj);

Collections.sort(arrayListTransformedIs,

(I1,I2)->(((Student) I1).getStudentRollNo()>((Student) I2).getStudentRollNo())?1:(((Student) I1).getStudentRollNo()<((Student) I2).getStudentRollNo())?-1:0

);

System.out.println("ArrayListObj is \n" + arrayListTransformedIs);

}

}


Solution is :


ArrayListObj is

[

Student Roll Number is 1 and Student Name is Sachin

,

Student Roll Number is 2 and Student Name is Raghavendra

,

Student Roll Number is 2 and Student Name is Rajagopal

,

Student Roll Number is 3 and Student Name is Prasad

,

Student Roll Number is 5 and Student Name is Kumari

,

Student Roll Number is 7 and Student Name is Krishna

]

 TreeMap Sorting Example

-----------------------------------

import java.util.TreeMap;

public class TreeMapExample {

public static void main(String[] args) {

TreeMap<Double, String> treeMap = new TreeMap<Double, String>((I1,I2)->(I1>I2)?1:(I1<I2?-1:0));

treeMap.put(200.0, "initial");

treeMap.put(300.0, "Final");

treeMap.put(100.0, "Rather");

treeMap.put(1000.0,"No Worries");

System.out.println("treeMap Values in ascending Order is " + treeMap);

}

}


Solution :


treeMap Values in ascending Order is {100.0=Rather, 200.0=initial, 300.0=Final, 1000.0=No Worries}


treeMap Sorts according to its Key and therefore the result as 100,200,300,1000.....

TreeSet Sorting Example

-------------------------------- 

import java.util.TreeSet;


public class TreeSetSortingExample {


public static void main(String[] args) {

// TODO Auto-generated method stub


TreeSet<Integer> ts = new TreeSet<Integer>((I1,I2)->(I1>I2)?-1:(I1<I2)?1:0);

ts.add(10);

ts.add(200);

ts.add(90);

ts.add(77);

ts.add(50);

ts.add(150);

ts.add(40);

System.out.println(ts);

}

}


Solution :


[10, 40, 50, 77, 90, 150, 200]



We are passing the argument in TreeSet<Integer> ts = new TreeSet<Integer>(

);

the compare method values
(I1,I2)->(I1>I2)?-1:(I1<I2)?1:0

Collections Sort using Lambda Expressions

-----------------------------------------


import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Comparator;


class CollectionsSort {

public static void main(String[] args) {

ArrayList<Integer> al = new ArrayList<Integer>();

al.add(10);

al.add(29);

al.add(17);

al.add(14);

al.add(23);

System.out.println("Before sorting " + al);

Collections.sort(al, ((I1,I2)->(I1 > I2) ? -1 : (I1 < I2) ? 1 : 0));

System.out.println("After sorting " + al);

}

}


Solution :


I1, I2 is the feed to compare(Object o1, Object o2) or compare(Integer o1, Integer o2) method


Which then checks for the


if(I1 > I2) {

return -1;

} else if(I1 < I2) {

return 1;

} else {

return 0;

}



Before sorting [10, 29, 17, 14, 23]

After sorting [29, 23, 17, 14, 10]

Comparing String by Lengths and then Sorting in reverse Order

 import java.util.Arrays;

import java.util.Comparator;


public class StringLengthComparebetweenTwoValues {

public static void main(String[] args) {

// TODO Auto-generated method stub

String values[] = {"Balaji","Appa","Amma","KovilAunty"};

Comparator<String> compareLengths = (str1, str2) -> Integer.compare(str1.length(), str2.length());


Arrays.sort(values, compareLengths.reversed());

System.out.println("Arrays.asList(values) " + Arrays.asList(values));

}

}



Solution :


Arrays.asList(values) [KovilAunty, Balaji, Appa, Amma]

 Combining Comparators by Length and then Alphabetically in Order

 -------------------------------------------------------------------------------------------

import java.util.Arrays;

import java.util.Comparator;

import java.util.List;


public class CollectionStringArraySortingOrder {

public static void main(String[] args) {

String fruits[] = {"Apple","Google","Ball","Ankara"};

List<String> list = Arrays.asList(fruits);

Comparator<String> ref = Comparator.comparingInt(String::length).thenComparing(Comparator.naturalOrder());

list.sort(ref);

System.out.println("List Value is " + list);

}

}


Solution :


List Value is [Ball, Apple, Google, Ankara]