Karthikeya Boyini has Published 2383 Articles

Remove all elements from Java LinkedHashSet

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

276 Views

To remove all the elements from LinkedHashSet in Java, use the clear() method.The following is an example to declare LinkedHashSet and add elements to it −LinkedHashSet hashSet = new LinkedHashSet(); hashSet.add(10); hashSet.add(20); hashSet.add(30); hashSet.add(40); hashSet.add(50); hashSet.add(60);Use the clear() method to remove all elements −hashSet.clear();The following is an example −Example Live Demoimport ... Read More

Get last entry from NavigableMap in Java

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

134 Views

To display the last entry from NavigableMap in Java, use the lastEntry() method.Let us first create NavigableMap.NavigableMap n = new TreeMap(); n.put("A", 498); n.put("B", 389); n.put("C", 868); n.put("D", 988); n.put("E", 686); n.put("F", 888); n.put("G", 999); n.put("H", 444); n.put("I", 555); n.put("J", 666);Get the last entry now.n.lastEntry()The following is an example to ... Read More

Check if a particular key exists in Java LinkedHashMap

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

407 Views

Use the containsKey() method to check whether a particular key exists in LinkedHashMap or not.Create a LinkedHashMap −LinkedHashMap l = new LinkedHashMap(); l.put(1, "Mars"); l.put(2, "Earth"); l.put(3, "Jupiter"); l.put(4, "Saturn"); l.put(5, "Venus");Now, let’s say we need to check whether key 4 exists or not. For that, use the containsKey() method ... Read More

Fill elements in a Java double array in a specified range

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

212 Views

Elements can be filled in a Java double array in a specified range using the java.util.Arrays.fill() method. This method assigns the required double value in the specified range to the double array in Java.The parameters required for the Arrays.fill() method are the array name, the index of the first element ... Read More

How to use headSet() method of Java NavigableSet Class

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

82 Views

The headset() method returns elements up to a limit defined as a parameter.First, create a NavigableSet and add elements −NavigableSet set = new TreeSet(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100);Now, use the headset() method −set.headSet(55));The following is an example −Example Live Demoimport java.util.NavigableSet; import java.util.TreeSet; public class Demo {   ... Read More

Copy all elements of Java LinkedHashSet to an Object Array

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

124 Views

First, create a LinkedHashSet and add 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, copy it to an object array like this −// copying Object[] arr = l.toArray();The following is an example to copy all elements of a ... Read More

Create a new ArrayList from another collection in Java

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

278 Views

An ArrayList can be created from another collection using the java.util.Arrays.asList() method. A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Demo {    public static void main(String args[]) throws Exception {       String str[] = { "John", "Macy", ... Read More

Fill elements in a Java long array in a specified range

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

103 Views

Elements can be filled in a Java long array in a specified range using the java.util.Arrays.fill() method. This method assigns the required long value in the specified range to the long array in Java.The parameters required for the Arrays.fill() method are the array name, the index of the first element ... Read More

Copy all elements in Java TreeSet to an Object Array

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

227 Views

Create a TreeSet and add elements −TreeSet tSet = new TreeSet(); tSet.add("TV"); tSet.add("Radio"); tSet.add("Internet");Now, let us copy it to object array −Object[] arr = tSet.toArray(); for (Object ob: arr) {    System.out.println(ob); }The following is an example to copy all elements in TreeSet to an Object Array −Example Live Demoimport java.util.*; ... Read More

Remove lowest element in Java TreeSet

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

465 Views

To remove the lowest element, use the pollFirst() method.Create a TreeSet and add elements to it −TreeSet tSet = new TreeSet(); tSet.add("78"); tSet.add("56"); tSet.add("88"); tSet.add("12");Now, remove the lowest element −tSet.pollFirst()The following is an example to remove lowest element in Java TreeSet −Example Live Demoimport java.util.*; public class Demo { ... Read More

Advertisements