Java Articles

Page 64 of 450

Checking if a HashSet contains certain value in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 929 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 266 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 145 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 394 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

How many ways are there to initialize a final variable in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 3K+ 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.Initializing a final variableOnce you declare a final variable, it is a must to initialize it. You can initialize the final instance variable −At the time of declaration as.public final String name = "Raju"; public final int age = 20;Within an instance (non-static) block.{    this.name = "Raju";    this.age = 20; }Within a default constructor.public final ...

Read More

Why main() method must be static in java?

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

Static − If you declare a method, subclass, block, or a variable static it is loaded along with the class.In Java whenever we need to call an (instance) method we should instantiate the class (containing it) and call it. If we need to call a method without instantiation it should be static. Moreover, static methods are loaded into the memory along with the class.In the case of the main method, it is invoked by the JVM directly, so it is not possible to call it by instantiating its class. And, it should be loaded into the memory along with the ...

Read More

Why final variable doesn&#039;t require initialization in main method in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 823 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 variable as final, it is mandatory to initialize it before the end of the constructor. If you don’t, a compile-time error is generated.Examplepublic class Student {    public final String name;    public final int age;    public void display(){       System.out.println("Name of the Student: "+this.name);   ...

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