How to sort list by two fields in Java?

private static void order(List persons) { Collections.sort(persons, new Comparator() { public int compare(Object o1, Object o2) { String x1 = ((Person) o1).getName(); String x2 = ((Person) o2).getName(); int sComp = x1.compareTo(x2); if (sComp != 0) { return sComp; } Integer x1 = ((Person) o1).getAge(); Integer x2 = ((Person) o2).getAge(); return x1.compareTo(x2); } }); }

What’s the difference between equals() and ==?

The “==” operator In Java, when the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory. In other words, it checks to see if the 2 object names are basically references to the same memory location. A very simple example will help […]

Difference between Set, List and Map

Set vs List vs Map in Java As I said Set, List and Map are interfaces, which defines core contract e.g. a Set contract says that it can not contain duplicates. Based upon our knowledge of List, Set and Map let’s compare them on different metrics. Duplicate Objects Main difference between List and Set interface in Java is that List allows duplicates while Set doesn’t allow duplicates. All implementation of Set honor this contract. Map  holds two […]

Concurrency API in Java

The Concurrency API introduces the concept of an ExecutorService as a higher level replacement for working with threads directly. Executors are capable of running asynchronous tasks and typically manage a pool of threads, so we don’t have to create new threads manually. All threads of the internal pool will be reused under the hood for […]

Run task periodically in Java

Schedular Task : For this functionality, You should create a class extending TimerTask(available in java.util package). TimerTask is a abstract class. Write your code in public void run() method that you want to execute periodically. Insert below code in your Main class. import java.util.TimerTask; import java.util.Date; /**  *   * @author Dhinakaran P.  */ // Create […]