Samual Sam has Published 2491 Articles

Remove a value from Java LinkedHashMap

Samual Sam

Samual Sam

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

2K+ Views

Use the remove() method to remove a single value from LinkedHashMap −At first, create a LinkedHashMap 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, let’s say you need to remove the element 2 from the LinkedHashMap. For ... Read More

Retrieve the last entry in the TreeMap in Java

Samual Sam

Samual Sam

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

2K+ Views

Use the lastEntry() method in TreeMap to retrieve the last entry.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");Now, retrieve the last entry −m.lastEntry()The following is an example to retrieve last entry in the TreeMap −Example Live Demoimport ... Read More

Check if a particular value exists in Java LinkedHashMap

Samual Sam

Samual Sam

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

399 Views

Use the containsValue() method to check whether a particular value 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 value “Saturn” exists or not. For that, use the containsValue() method ... Read More

Remove all values from Java LinkedHashMap

Samual Sam

Samual Sam

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

270 Views

Use the clear() method to remove all the values from LinkedHashMap in Java.Create a LinkedHashMap 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, let us remove all the values −l.clear();The following is an example to remove all ... Read More

Fill elements in a Java int array in a specified range

Samual Sam

Samual Sam

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

566 Views

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

Create a TreeSet in Java

Samual Sam

Samual Sam

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

98 Views

Create a TreeSet and add elements −TreeSet tSet = new TreeSet(); tSet.add("TV"); tSet.add("Radio"); tSet.add("Internet");Iterate through the elements −Iterator i = tSet.iterator(); while(i.hasNext()){ System.out.println(i.next()); }The following is an example to create a TreeSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]){ ... Read More

Get last value in Java TreeSet

Samual Sam

Samual Sam

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

513 Views

To get the last value in TreeSet, use the last() method.First, get the TreeSet and add elements to it −TreeSet tSet = new TreeSet(); tSet.add("10"); tSet.add("20"); tSet.add("30"); tSet.add("40"); tSet.add("50"); tSet.add("60");Now, get the first value −tSet.last()The following is an example to get the last value in TreeSet −Example Live Demoimport java.util.*; public ... Read More

Remove highest element in Java TreeSet

Samual Sam

Samual Sam

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

359 Views

To remove the highest element, use the pollLast() 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 highest element −tSet.pollLast()The following is an example to remove highest element in Java TreeSet −Example Live Demoimport java.util.*; public class Demo { ... Read More

Get Sub Set from TreeSet in Java

Samual Sam

Samual Sam

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

148 Views

To get the sub set from TreeSet, use the subSet() method. Let us first create a TreeSet and add elements to it −TreeSet set = new TreeSet(); set.add("78"); set.add("56"); set.add("88"); set.add("54"); set.add("76"); set.add("34");Let us get the subset now from a range of elements −SortedSet sorted = set.subSet("54", "76");The following is ... Read More

Check two ArrayList for equality in Java

Samual Sam

Samual Sam

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

700 Views

Two ArrayList can be compared to check if they are equal or not using the method java.util.ArrayList.equals(). This method has a single parameter i.e. an ArrayList that is compared with the current object. It returns true if the two ArrayList are equal and false otherwise.A program that demonstrates this is ... Read More

Advertisements