Java Articles

Page 64 of 450

How to track the order of insertion using Java collections?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 348 Views

To track the order of insertion, you can use Map.Entry() in case of a Map. Let’s say we have the following LinkedHashMap −Mapmap = new LinkedHashMap(); map.put("Jack", 0); map.put("Tim", 1); map.put("David", 2); map.put("Tom", 3); map.put("Kevin", 4); map.put("Jeff", 5);Now, loop through Map.Entry and get the order of insertion correctly with Key and Value −for (Map.Entryentry: map.entrySet()) {    System.out.println(entry.getKey() + " => " + entry.getValue()); }Exampleimport java.util.LinkedHashMap; import java.util.Map; public class Demo {    public static void main(String[] args) {       Mapmap = new LinkedHashMap();       map.put("Jack", 0);       map.put("Tim", 1);       map.put("David", ...

Read More

How to use null value as key in Java HashMap

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

Yes, you can set null as key in Java HashMap. For this, let’s first create a HashMap with key and value pair −Mapmap = new HashMap(); map.put("Football", "A"); map.put("Squash", "B"); map.put("Cricket", "C"); map.put("Hockey", "D"); map.put("Rugby", "E");Now, let’s add null value as key −map.put(null, "H");You can try to get the value for key as “null” −map.get(null);Exampleimport java.util.HashMap; import java.util.Map; public class Demo {    public static final void main(String[] args) {       Mapmap = new HashMap();       map.put("Football", "A");       map.put("Squash", "B");       map.put("Cricket", "C");       map.put("Hockey", "D");       ...

Read More

Java Program to insert a value to a SortedSet

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

Let’s say we have the following SortedSet −SortedSet set = new TreeSet(); set.add("T"); set.add("R"); set.add("S"); set.add("Q"); set.add("V"); set.add("U"); set.add("W");Now, after displaying the above elements with Iterator, you can insert values like this to a SortedSetset.add("Z"); set.add("Y");Exampleimport java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet; public class Demo {    public static void main(String[] argv) throws Exception {       SortedSet set = new TreeSet();       set.add("T");       set.add("R");       set.add("S");       set.add("Q");       set.add("V");       set.add("U");       set.add("W");       Iterator i = set.iterator();       ...

Read More

Checking if a HashSet contains certain value in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 931 Views

Let’s say the following is our Integer array −Integer arr[] = { 50, 100, 150, 200, 250, 300 };Set the above integer array in HashSet −Setset = new HashSet(Arrays.asList(arr));Now, let us check whether the HashSet contains certain value or not −set.contains(150)TRUE is returned if the value is in the List, else FALSE is the return value.Exampleimport java.util.Arrays; import java.util.HashSet; import java.util.Set; public class Demo {    public static void main(String[] a) {       Integer arr[] = { 50, 100, 150, 200, 250, 300 };       Setset = new HashSet(Arrays.asList(arr));       System.out.println(set.contains(200));       ...

Read More

Make a Hashtable read-only in Java

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

Read-only Hashtable would mean users won’t be able to add or remove elements from it. Let us first create a Hashtable with key-value pair −Hashtablehash = new Hashtable(); hash.put("1", "A"); hash.put("2", "B"); hash.put("3", "C"); hash.put("4", "D"); hash.put("5", "E"); hash.put("6", "F"); hash.put("7", "G");Now, use unmodifiableMap() to form a Hashtable read-only −Mapm = Collections.unmodifiableMap(hash);Exampleimport java.util.Collections; import java.util.Hashtable; import java.util.Map; public class Demo {    public static void main(String[] s) {       Hashtablehash = new Hashtable();       hash.put("1", "A");       hash.put("2", "B");       hash.put("3", "C");       hash.put("4", "D");       hash.put("5", "E"); ...

Read More

Java Program to get the reverse of the NavigableSet

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

We will create a NavigableSet collection and add some elements −NavigableSet set = new TreeSet(); set.add("ABC"); set.add("DEF"); set.add("GHI"); set.add("JKL"); set.add("MNO"); set.add("PQR"); set.add("STU");Now, use descendingSet() for the reverse of NavigableSet −NavigableSetreverse = set.descendingSet();Exampleimport java.util.NavigableSet; import java.util.TreeSet; public class Demo {    public static void main(String[] args) {       NavigableSet set = new TreeSet();       set.add("ABC");       set.add("DEF");       set.add("GHI");       set.add("JKL");       set.add("MNO");       set.add("PQR");       set.add("STU");       NavigableSetreverse = set.descendingSet();       System.out.println("NavigableSet = " + set);       System.out.println("NavigableSet ...

Read More

Java Program to get day of week for the last day of each month in 2019

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

For last day of each month, at first create a List collection for day of week −ListdayOfWeek = new ArrayList();Now, get day of week for the last day of each month in 2019, set the year using withYear() and use lastDayOfMonth() and getDayOfWeek methods −for (Month m: Month.values()) {    DayOfWeek days = LocalDate.now().withYear(2019).with(m).with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek(); }Exampleimport java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; import java.time.temporal.TemporalAdjusters; import java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] argv) {       ListdayOfWeek = new ArrayList();       for (Month m: Month.values()) {          DayOfWeek days = LocalDate.now().withYear(2019).with(m).with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek(); ...

Read More

Java Program to fill an array with random numbers

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

Let us first crate an array −double[] arr = new double[5];Now, create Random class object −Random randNum = new Random();Now, fill the above array with random numbers −for (int i = 0; i < 5; i++) { arr[i] = randNum.nextInt(); }Exampleimport java.util.Arrays; import java.util.Random; public class Demo {    public static void main(String args[]) {       double[] arr = new double[5];       Random randNum = new Random();       for (int i = 0; i < 5; i++) {          arr[i] = randNum.nextInt();       }       System.out.println("Random numbers = "+Arrays.toString(arr));    } }OutputRandom numbers = [-6.59888981E8, 1.141160731E9, -9.931249E8, 1.335266582E9, 8.27918412E8]

Read More

Is it possible to use this keyword in static context in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

A static method or, block belongs to the class and these will be loaded into the memory along with the class. You can invoke static methods without creating an object. (using the class name as reference).Whereas "this" in Java acts as a reference to the current object. But static contexts(methods and blocks) doesn't have any instance they belong to the class.In a simple sense, to use “this” the method should be invoked by an object, which is not always necessary with static methods.Therefore, you cannot use this keyword from a static method.ExampleIn the following Java program, the class ThisExample contains a private ...

Read More

Can we declare final variables without initialization in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 5K+ Views

In Java, final is the access modifier which can be used with a filed class and a method.When a method if final it cannot be overridden.When a variable is final its value cannot be modified further.When a class is final it cannot be extended.Declaring final variable without initializationIf you declare a final variable later on you cannot modify or, assign values to it. Moreover, like instance variables, final variables will not be initialized with default values.Therefore, it is mandatory to initialize final variables once you declare them.Still, if you try to declare final variables without initialization that will generate a ...

Read More
Showing 631–640 of 4,495 articles
« Prev 1 62 63 64 65 66 450 Next »
Advertisements