Posts

Main method not found in class Test, please define the main method as: public static void main(String[] args)

Image
Main method not found in class Test, please define the main method as: public static void main(String[] args) Ans. Check the main method with argument type String[].

What are all the Classes and Interfaces that are available in the collections in Java?

Image
What are all the Classes and Interfaces that are available in the collections in Java? You can check in eclipse ide.  1. Go to the interface Collection in eclipse. 2. right click it and see type hierarchy. You can see as below. You can check all the methods also.

How do I sort objects of ArrayList by its date?

How do I sort objects of ArrayList by its date? Here is the example How to sort the Students by their Date of birth. import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class ArrayListSort2 { public static void main(String[] args) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); List list = new ArrayList<>( Arrays.asList(new Student[] { new Student(1, "Hardik", format.parse("31/12/1998")), new Student(2, "Rohit", format.parse("30/06/1991")), new Student(3, "Aaryan", format.parse("05/05/1995")) })); System.out.println("Sort by ascending order."); Collections.sort(list, new ConpareByDate(ShortBy.ASC)); System.out.println(list); System.out.println("Sort by d...

Java: How do you sort an ArrayList in descending order?

Java: How do you sort an ArrayList in descending order? There can be 2 scenarios to shot the ArrayList.  (1) Sort the ArrayList which contains the in build types like Long, String,Integer ect. (2) Sort ArrayList which contains the custom type. Let's say List of Student class and you want to sort by name. See the code for both scenario. (1) Sort List of inbuilt types import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class ArrayListSort1 { public static void main(String[] args) { List list = new ArrayList<>(Arrays.asList(new Long[] { 50l, 4l, 10l, 1l, 2l, 3l })); System.out.println("Before Short"); System.out.println(list); System.out.println("After Short"); list.sort(Collections.reverseOrder()); System.out.println(list); } } You can see the output as below: Before Short [50, 4, 10, 1, 2, 3] After Short [50, 10, 4, 3, 2, 1] ...

How many ways can I create a thread in Java?

How many ways can I create a thread in Java? There are 2 ways to create thread in java. 1. Using the Thread class. 2. Using the Runnable Interface. Below are the examples: 1. Using the Thread class. public class ThreadDemo1 { public static void main(String[] args) { Printer1 thread1 = new Printer1(); thread1.setName("Simple Thread"); thread1.start(); } } class Printer1 extends Thread{ @Override public void run() { System.out.println("Inside "+Thread.currentThread().getName()); } } Here you can see that we extends the Thread class and implements its run method. We have to create thread class object and call the start() method of it to start the execution of the thread. 2. Using the Runnable Interface. public class ThreadDemo2 { public static void main(String[] args) { Printer2 task = new Printer2(); Thread thread2 = new Thread(task); thread2.setName("Runnable Thread"); thread2.star...

How do I copy an object in Java?

How do I copy an object in Java? Upcoming post...

How do you count the number of String objects created in a program in Java? What is the easiest way to identify it?

How do you count the number of String objects created in a program in Java? What is the easiest way to identify it? First of all you need to understand that the String object is the special object in java. There are two types of memories which are used for Sting object. One is string pool and other is the main memory where object will be stored. Upcoming answer of this question in details...