Articles on Trending Technologies

Technical articles with clear explanations and examples

Create a TreeMap in Java and add key-value pairs

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

A TreeMap cannot contain duplicate keys. TreeMap cannot contain the null key. However, It can have null values.Let us first see how to create a TreeMap −TreeMap m = new TreeMap();Add some elements in the form of key-value pairs −m.put(1, "India"); m.put(2, "US"); m.put(3, "Australia"); m.put(4, "Netherlands"); m.put(5, "Canada");The following is an example to create a TreeMap and add key-value pairs −Exampleimport java.util.*; public class Demo {    public static void main(String args[]) {       TreeMap m = new TreeMap();       m.put(1, "India");       m.put(2, "US");       m.put(3, "Australia");       ...

Read More

Display File class constants in Java

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

The java.io.File class has display constants File.separatorChar and File.pathSeparatorChar mainly. The File.separatorChar is ‘/’ and the File.pathSeparatorChar is ‘:’ for Unix.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          System.out.println("File.pathSeparatorChar = " + File.pathSeparatorChar);          System.out.println("File.separatorChar = " + File.separatorChar);       } catch(Exception e) {          e.printStackTrace();       }    } }The output of the above program is as follows −OutputFile.pathSeparatorChar = : File.separatorChar = /Now let us ...

Read More

LongStream parallel() method in Java

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

The parallel() method of the LongStream class in Java returns an equivalent stream that is parallel.The syntax is as follows:LongStream parallel()To use the LongStream class in Java, import the following packageExampleimport java.util.stream.LongStream;The following is an example to implement LongStream parallel() method in Java:import java.util.stream.LongStream; public class Demo {    public static void main(String[] args) {       LongStream longStream = LongStream.range(10L, 20L);       System.out.println("Parallel LongStream = ");       longStream.parallel().forEach(System.out::println);    } }outputParallel LongStream = 16 15 10 17 11 13 12 19 18 14

Read More

Can we write any statements between try, catch and finally blocks in Java?

raja
raja
Updated on 11-Mar-2026 3K+ Views

No, we cannot write any statements in between try, catch and finally blocks and these blocks form one unit.  The functionality of try keyword is to identify an exception object and catch that exception object and transfer the control along with the identified exception object to the catch block by suspending the execution of the try block. The functionality of the catch block is to receive the exception class object that has been sent by the try and catch that exception class object and assigns that exception class object to the reference of the corresponding exception class defined in the catch block. The finally blocks are ...

Read More

Java Program to remove a key from a TreeMap only if it is associated with a given value

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

Use the remove() method to remove a key from a TreeMap only if it is associated with a given value. Let us first create a TreeMap and add some elements −TreeMap m = new TreeMap(); m.put(1, "India"); m.put(2, "US"); m.put(3, "Australia"); m.put(4, "Netherlands"); m.put(5, "Canada");To remove a key, set the key and the associated value here. If the associate value exists, then the key will get removed −m.remove(3, "Australia")The following is an example to remove a key from a TreeMap only if it is associated with a given value −Exampleimport java.util.*; public class Demo {    public static void main(String ...

Read More

List the file system roots in Java

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

The method java.io.File.listRoots() is used to list the file system roots in Java. This method requires no parameters. It returns the available file system roots in the form of an array of file objects and if the file system roots cannot be determined, it returns null.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo {    public static void main(String[] args) {       File[] roots = File.listRoots();       try {          for(File r : roots) {             System.out.println(r);          } ...

Read More

Duration ZERO field in Java

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

The ZERO field sets the duration to zero in the Duration class in Java. This field is quite the same as the null value in different data types in Java.A program that demonstrates the ZERO field is given as follows −Exampleimport java.time.Duration; public class Demo {    public static void main(String[] args) {       Duration d = Duration.ZERO;       boolean flag = d.isZero();       System.out.println("The duration is: " + d);       if(flag)          System.out.println("The above duration is of zero length");       else          System.out.println("The ...

Read More

LongStream generate() method in Java

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

The generate() method of the LongStream class returns an infinite sequential unordered stream where each element is generated by the provided LongSupplier.The syntax is as follows:static LongStream generate(LongSupplier s)Here, s is the LongSupplier for generate elements. The LongSupplier is the supplier of long-valued results.To use the LongStream class in Java, import the following package:import java.util.stream.LongStream;The following is an example to implement LongStream generate() method in Java −Exampleimport java.util.stream.LongStream; public class Demo {    public static void main(String[] args){       LongStream longStream = LongStream.generate(()          -> { return (long)(Math.random() * 100); });       System.out.println("Unordered ...

Read More

Can we create an object for an interface in java?

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

No, you cannot instantiate an interface. Generally, it contains abstract methods (except default and static methods introduced in Java8), which are incomplete.Still if you try to instantiate an interface, a compile time error will be generated saying “MyInterface is abstract; cannot be instantiated”.In the following example we an interface with name MyInterface and a class with name InterfaceExample.In the interface we have an integer filed (public, static and, final) num and abstract method demo().From the class we are trying to − create an object of the interface and print the num value.Exampleinterface MyInterface{    public static final int num = ...

Read More

Collectors averagingDouble() method in Java 8

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 681 Views

The averagingDouble() method of the Collectors class in Java 8 returns a Collector that is the arithmetic mean of a double-valued function applied to the input elements.The syntax is as follows −public static Collector averagingDouble(ToDoubleFunction

Read More
Showing 29261–29270 of 61,297 articles
Advertisements