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 […]

Random String Generation in Java

import java.util.Random; public class RandomStringGen { private static final String CHAR_LIST =        “1234567890”;    private static final int RANDOM_STRING_LENGTH = 4;      /**     * This method generates random string     * @return     */    public String generateRandomString(){              StringBuffer randStr = […]

Difference between path band classpath in Java

1).Path is an environment variable which is used by the operating system to find the executables. Classpath is an environment variable which is used by the Java compiler to find the path, of in J2EE we give the path of jar files. 2).PATH is nothing but setting up an environment for operating system. Operating […]

Java 7 Features

1. Strings in Switch: public void testStringInSwitch(String param){        final String JAVA5 = “Java 5”;        final String JAVA6 = “Java 6”;        final String JAVA7 = “Java 7”;        switch (param) {            case JAVA5:               […]

Explanation of oops concepts

Object means a real word entity such as pen, chair, table etc.Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts: Object Class Inheritance Polymorphism Abstraction Encapsulation Object Any entity that has state and behavior is known as an object. For […]