karthikeya Boyini

karthikeya Boyini

1,421 Articles Published

Articles by karthikeya Boyini

Page 11 of 143

Check if a particular element exists in Java LinkedHashSet

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

Use the contains() method to check if a specific element exists in LinkedHashSet or not.Let us first create a LinkedHashSet and add some elements −LinkedHashSet l = new LinkedHashSet(); l.add(new String("1")); l.add(new String("2")); l.add(new String("3")); l.add(new String("4")); l.add(new String("5")); l.add(new String("6")); l.add(new String("7"));Now, check whether it contains element “5” or not −l.contains("5")The following is an example to check if a particular element exists in LinkedHashSet −Exampleimport java.util.*; public class Demo {    public static void main(String[] args) {       LinkedHashSet l = new LinkedHashSet();       l.add(new String("1"));       l.add(new String("2"));       l.add(new ...

Read More

Convert elements in a HashSet to an array in Java

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

First, create a HashSet and elements to it −HashSet hs = new HashSet(); // add elements to the hash set hs.add("B"); hs.add("A"); hs.add("D"); hs.add("E"); hs.add("C"); hs.add("F"); hs.add("K");Let us now convert the above HashSet to an array −Object[] ob = hs.toArray();The following is an example to convert elements in a HashSet to an array −Exampleimport java.util.*; public class Demo { public static void main(String args[]) { // create a hash set HashSet hs = new HashSet(); // add elements to ...

Read More

Convert a Set into a List in Java

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

First, create a Set and add elements −Set s = new HashSet(); s.add("P"); s.add(new Date()); s.add(new Long(898999)); s.add("Q"); s.add("R"); s.add(new Integer(1));Convert the above Set to a List −List l = new ArrayList(s);The following is an example to convert a set into a list in Java −Exampleimport java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Set; import java.util.HashSet; public class Demo {    public static void main(String[] args) {       Set s = new HashSet();       s.add("P");;       s.add(new Date());       s.add(new Long(898999));       s.add("Q");       s.add("R");       s.add(new ...

Read More

Find maximum element of HashSet in Java

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

To get the maximum element of HashSet, use the Collections.max() method.Declare the HashSet −Set hs = new HashSet();Now let us add the elements −hs.add(29); hs.add(879); hs.add(88); hs.add(788); hs.add(456);Let us now get the maximum element −Object obj = Collections.max(hs);The following is an example to find maximum element of HashSet −Exampleimport java.util.*; public class Demo { public static void main(String args[]) { // create hash set Set hs = new HashSet(); hs.add(29); hs.add(879); ...

Read More

Fetch key-valuepair from Java LinkedHashSet

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

To fetch LinkedHashSet key-value pair in Java, use the entrySet() method.Let us first create LinkedHashSet and add some elements −LinkedHashMap l = new LinkedHashMap(); l.put("1", "Jack"); l.put("2", "Tom"); l.put("3", "Jimmy"); l.put("4", "Morgan"); l.put("5", "Tim"); l.put("6", "Brad");Now, fetch a key-value pair −l.entrySet()The following is an example to fetch key-value pair from LinkedHashSet −Exampleimport java.util.*; public class Demo {    public static void main(String[] args) {       LinkedHashMap l = new LinkedHashMap();       l.put("1", "Jack");       l.put("2", "Tom");       l.put("3", "Jimmy");       l.put("4", "Morgan");       l.put("5", "Tim");       ...

Read More

Create a TreeMap in Java and add key-value pairs

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ 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 160 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

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 206 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 412 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

ByteBuffer asShortBuffer() method in Java

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

A view of the ByteBuffer can be created as a ShortBuffer using the asShortBuffer() method in the class java.nio.ByteBuffer. This method requires no parameters and it returns a short buffer as required. This buffer reflects the changes made to the original buffer and vice versa.A program that demonstrates this is given as follows −Exampleimport java.nio.*; import java.util.*; public class Demo {    public static void main(String[] args) {       int n = 50;       try {          ByteBuffer bufferB = ByteBuffer.allocate(n);          ShortBuffer bufferS = bufferB.asShortBuffer();         ...

Read More
Showing 101–110 of 1,421 articles
« Prev 1 9 10 11 12 13 143 Next »
Advertisements