Java Articles

Page 108 of 450

Display the current method name in Java

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 2K+ Views

The current method name that contains the execution point that is represented by the current stack trace element is provided by the java.lang.StackTraceElement.getMethodName() method.A program that demonstrates this is given as follows −Examplepublic class Demo {    public static void main(String args[]) {       System.out.println       ("The method name is: " + new Exception().getStackTrace()[0].getMethodName());    } }OutputThe method name is: mainNow let us understand the above program.The method getMethodName() is used to obtain the current method name that contains the execution point that is represented by the current stack trace element. This is printed using System.out.println(). ...

Read More

Convert LinkedList to ArrayList in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 7K+ Views

A LinkedList can be converted into an ArrayList by creating an ArrayList such that the parameterized constructor of the ArrayList initialises it with the elements of the LinkedList.A program that demonstrates this is given as follows −Exampleimport java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class Demo {    public static void main(String[] args) {       LinkedList l = new LinkedList();       l.add("Orange");       l.add("Apple");       l.add("Peach");       l.add("Guava");       l.add("Pear");       List aList = new ArrayList(l);       System.out.println("The ArrayList elements are: ");     for ...

Read More

Display the declared methods of java.lang.Math

Nancy Den
Nancy Den
Updated on 11-Mar-2026 200 Views

The methods of the java.lang.Math class can be listed using the java.lang.Class.getDeclaredMethods() method. This method returns an array that contains all the Method objects with public, private, protected and default access. However, the inherited methods are not included. Also, the getDeclaredMethods() method returns a zero length array if the class or interface has no methods or if a primitive type, array class or void is represented in the Class object.A program that demonstrates this is given as follows −Exampleimport java.lang.reflect.Method; public class Demo {    public static void main(final String[] args) {       final Method[] methods = Math.class.getDeclaredMethods();   ...

Read More

Get the last element from a Sorted Set in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

To create a Sorted Set, firstly create a Set −Set s = new HashSet();Add elements to the above set −int a[] = {77, 23, 4, 66, 99, 112, 45, 56, 39, 89}; Set s = new HashSet(); try {    for(int i = 0; i < 5; i++) {    s.add(a[i]); }After that, use TreeSet class to sort −TreeSet sorted = new TreeSet(s);Get the last element, using the last() method −System.out.println("Last element of the sorted set = "+ (Integer)sorted.last());The following is the code to get the last element from a Sorted Set in Java −Exampleimport java.util.*; public class Demo { ...

Read More

Check whether a NavigableMap empty or not in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 273 Views

The isEmpty() method is used in Java to check whether a NavigableMap is empty or not.First, create a NavigableMap and add elements to it −NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); n.put(14, "Jamie"); n.put(1, "Tim"); n.put(4, "Jackie"); n.put(15, "Kurt"); n.put(19, "Tiger"); n.put(24, "Jacob");Now, check whether the Map is empty or not −System.out.println("Map is empty? " + n.isEmpty());The following is an example to implement isEmpty() method and check whether the Map is empty −Exampleimport java.util.*; public class Demo {    public static void main(String[] args) {       NavigableMap n = new TreeMap();       n.put(5, "Tom"); ...

Read More

Get the asymmetric difference of two sets in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 772 Views

Use removeAll() method to get the asymmetric difference of two sets.First set −HashSet set1 = new HashSet (); set1.add("Mat"); set1.add("Sat"); set1.add("Cat");Second set −HashSet set2 = new HashSet (); set2.add("Mat");To get the asymmetric difference −set1.removeAll(set2);The following is an example that displays how to get the asymmetric difference between two sets −Exampleimport java.util.*; public class Demo {    public static void main(String args[]) {       HashSet set1 = new HashSet ();       HashSet set2 = new HashSet ();       set1.add("Mat");       set1.add("Sat");       set1.add("Cat");       System.out.println("Set1 ...

Read More

Get the intersection of two sets in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 15K+ Views

To get the intersection of two sets, use the retainAll() method. Here are out two set −First set −HashSet set1 = new HashSet (); set1.add("Mat"); set1.add("Sat"); set1.add("Cat");Second set −HashSet set2 = new HashSet (); set2.add("Mat"); set2.add("Cat"); set2.add("Fat"); set2.add("Hat");Get the intersection −set1.retainAll(set2);The following is an example to get the intersection of two sets −Exampleimport java.util.*; public class Demo {    public static void main(String args[]) {       HashSet set1 = new HashSet ();       HashSet set2 = new HashSet ();       set1.add("Mat");       set1.add("Sat");       set1.add("Cat");   ...

Read More

Convert an ArrayList to an Array with zero length Array in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 291 Views

An ArrayList can be converted into an Array using the java.util.ArrayList.toArray() method. This method takes a single parameter i.e. the array of the required type into which the ArrayList elements are stored and it returns an Array that contains all the elements of the ArrayList in the correct order.A program that demonstrates this is given as follows −Exampleimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] args) {       List aList = new ArrayList();       aList.add("James");       aList.add("Harry");       aList.add("Susan");       aList.add("Emma");       aList.add("Peter"); ...

Read More

Sort subset of array elements in Java

George John
George John
Updated on 11-Mar-2026 5K+ Views

The java.util.Arrays.sort() method can be used to sort a subset of the array elements in Java. This method has three arguments i.e. the array to be sorted, the index of the first element of the subset (included in the sorted elements) and the index of the last element of the subset (excluded from the sorted elements). Also, the Arrays.sort() method does not return any value.A program that demonstrates this is given as follows −Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       int arr[] = { 1, 9, 7, 3, 2, 8, 4, ...

Read More

Date Formatting Using printf

Samual Sam
Samual Sam
Updated on 11-Mar-2026 8K+ Views

Date and time formatting can be done very easily using the printf method. You use a two-letter format, starting with t and ending in one of the letters of the table as shown in the following code.Exampleimport java.util.Date; public class DateDemo {    public static void main(String args[]) {       // Instantiate a Date object       Date date = new Date();       // display time and date       String str = String.format("Current Date/Time : %tc", date );       System.out.printf(str);    } }This will produce the following result ...

Read More
Showing 1071–1080 of 4,498 articles
« Prev 1 106 107 108 109 110 450 Next »
Advertisements