Samual Sam has Published 2491 Articles

Get ceiling key from NavigableMap in Java

Samual Sam

Samual Sam

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

96 Views

Getting ceiling key means to return the least key greater than or equal to the given key. For this, use the ceilingKey() method.The following is an example to get ceiling key from NavigableMap.Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { ... Read More

Get Enumeration over HashSet in Java

Samual Sam

Samual Sam

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

192 Views

To get enumeration over HashSet, first declare the HashSet and add elements −HashSet hs = new HashSet(); hs.add("P"); hs.add("Q"); hs.add("R");To get enumeration −Enumeration e = Collections.enumeration(hs);The following is an example to get Enumeration over HashSet −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {   ... Read More

Initialize a Set without using add() method in Java

Samual Sam

Samual Sam

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

466 Views

With Java, you can initialize a set without using add() method.For this create a string array −String arr[] = { "A", "B", "C", "D", "E"};Now, use Set and asList() method to initialize the above string array to our Set −Set s = new HashSet(Arrays.asList(arr));The following is an example to initialize ... Read More

Java Program to check if a particular element exists in HashSet

Samual Sam

Samual Sam

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

739 Views

Use the contains() method to check if a particular element exists −Set hs = new HashSet(); hs.add(30); hs.add(67); hs.add(88); hs.add(33); hs.add(54); hs.add(90); hs.add(66); hs.add(79);To check for let’s say, element 89, use the contains() method −hs.contains(89));The following is an example to check if a particular element exists in HashSet −Example Live Demoimport ... Read More

NavigableSet Class higher() method in Java

Samual Sam

Samual Sam

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

79 Views

The higher() method in NavigableSet returns the least element strictly greater than the given element i.e. 35 here −higher(35);The following is an example to implement the higher method in Java −Example Live Demoimport java.util.NavigableSet; import java.util.TreeSet; public class Demo { public static void main(String[] args) { ... Read More

Add elements to LinkedHashMap collection in Java

Samual Sam

Samual Sam

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

2K+ Views

Use the put() method to add elements to LinkedHashMap collection.First, let us create a LinkedHashMap −LinkedHashMap l = new LinkedHashMap();Now, add elements −l.put("1", "Jack"); l.put("2", "Tom"); l.put("3", "Jimmy"); l.put("4", "Morgan"); l.put("5", "Tim"); l.put("6", "Brad");The following is an example to add elements to LinkedHashMap collection −Example Live Demoimport java.util.LinkedHashMap; public class Demo ... Read More

Convert an ArrayList to HashSet in Java

Samual Sam

Samual Sam

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

878 Views

To convert ArrayList to HashSet, firstly create an ArrayList −List l = new ArrayList();Add elements to the ArrayList −l.add("Accent"); l.add("Speech"); l.add("Diction"); l.add("Tone"); l.add("Pronunciation");Now convert the ArrayList to HashSet −Set s = new HashSet(l);The following is an example to convert an ArrayList to HashSet in Java.Example Live Demoimport java.util.ArrayList; import java.util.List; import ... Read More

Create NavigableMap from TreeMap in Java

Samual Sam

Samual Sam

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

104 Views

To create NavigableMap from TreeMap. The following is the declaration −NavigableMap n = new TreeMap();Now, add some elements to the NavigableMap created above −n.put("A", 888); n.put("B", 999); n.put("C", 444); n.put("D", 555); n.put("E", 666); n.put("F", 888); n.put("G", 999); n.put("H", 444); n.put("I", 555); n.put("J", 666);The following is an example to create NavigableMap ... Read More

Get the first element from a Sorted Set in Java

Samual Sam

Samual Sam

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

433 Views

To create a Sorted Set, firstly create a Set.Set s = new HashSet();Add elements to the above set.int a[] = {77, 23, 4, 66, 99, 112, 45, 56, 39, 89}; Set s = new HashSet(); try { for(int i = 0; i < 5; i++) { ... Read More

Clone IdentityHashMap in Java

Samual Sam

Samual Sam

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

108 Views

To clone the IdentityHashMap in Java, use the clone() method.Create an IdentityHashMap and add elements to it −IdentityHashMap m = new IdentityHashMap(); m.put("1", 100); m.put("2", 200); m.put("3", 300); m.put("4", 150); m.put("5", 110);Now get the size of the IdentityHashMap −m.size()The following is an example to clone IdentityHashMap in Java −Example Live Demoimport ... Read More

Advertisements