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]

Sunday, January 11, 2026

Sorting String Values of a Collection or Collection Sort by String

Sorting String Values of a Collection, Intersting

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

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

public class CollectionsSortStringValue {

public static void main(String[] args) {

ArrayList<String> l1= new ArrayList<String>();

l1.add("Banana");

l1.add("Apple");

l1.add("Amla");

l1.add("Ginger");

Collections.sort(l1.reversed());

System.out.println("After first reversal " + l1);

Collections.sort(l1.reversed().reversed());

System.out.println("After second reversal - We get the Name in Sorted Order " + l1);

l1.sort(Comparator.reverseOrder());

System.out.println("Comparator.reverseOrder reversal - We get the List in Reverse Sorted Order " + l1);

l1.sort(String.CASE_INSENSITIVE_ORDER);

System.out.println("Comparator.reverseOrder reversal - We get the List in Sorted Order " + l1);

l1.sort(Comparator.reverseOrder());

System.out.println("Again sorting back gives ELements in Reverse Sorted Order " + l1);

}

}


Solution :


You will get the Output as


After first reversal [Ginger, Banana, Apple, Amla]


After second reversal - We get the Name in Sorted Order [Amla, Apple, Banana, Ginger]


Comparator.reverseOrder reversal - We get the List in Reverse Sorted Order [Ginger, Banana, Apple, Amla]


Comparator.reverseOrder reversal - We get the List in Sorted Order [Amla, Apple, Banana, Ginger]


Again sorting back gives ELements in Reverse Sorted Order [Ginger, Banana, Apple, Amla]

Thursday, January 8, 2026

 Lombok @Builder not recognised by IntelliJ 

2026-01-09T12:57:07.327+05:30  WARN 11732 --- [RATING-SERVICE1] [foReplicator-%d] c.n.discovery.InstanceInfoReplicator     : There was a problem with the instance info replicator

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112) ~[eureka-client-2.0.5.jar:2.0.5]

at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-2.0.5.jar:2.0.5]

at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) ~[eureka-client-2.0.5.jar:2.0.5]


SOLUTION : 

In my case I changed the following in File->Settings->Annotation Processors->UserService from (1) 
C:\Users\balaji\.m2\repository\org\projectlombok\lombok\unkn‌​own\lombok-unknown.j‌​ar.lastUpdated to C:\Users\balaji\.m2\repository\org\projectlombok\lombok\1.18‌​.42\lombok-1.18.42.j‌​ar 

once changed click Apply and Ok button

(2) Build -> Rebuild Project (3) Ran the UserServiceApplication and it Ran Successfully

IMAGE ATTACHED : 


to 




Click Ok and Run Now  - You will be able to run successfully