Found 9150 Articles for Object Oriented Programming

Check if a particular value exists in Java LinkedHashMap

Aishwarya Naglot
Updated on 28-Jul-2025 14:20:51

578 Views

LinkedHashMap is a class in Java that implements the Map interface. It uses a doubly linked list to maintain the insertion order of elements. So, when we loop through a LinkedHashMap, the elements will be returned in the order we had inserted them. The given task is to check if a particular value exists in a LinkedHashMap in Java. Let's look at some scenarios to understand the problem better: Scenario Input: {1=Mikasa, 2=Eren, 3=Reiner}, Value to check: Mikasa Output: Value Exists Explanation: The LinkedHashMap has the value that we are looking for. Checking if a Value Exists in LinkedHashMapThe following ... Read More

Fill elements in a Java double array in a specified range

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

324 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 to be filled(inclusive), the index of the last element to be filled(exclusive) and the value that is to be stored in the array elements.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo { public static void main(String[] argv) throws Exception { ... Read More

Remove a value from Java LinkedHashMap

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 that, use the remove() method −Object ob = l.remove("2");The following is an example to remove a value from LinkedHashMap in Java −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { LinkedHashMap l = new LinkedHashMap(); ... Read More

Check if a particular key exists in Java LinkedHashMap

Aishwarya Naglot
Updated on 25-Jul-2025 15:49:50

620 Views

LinkedHashMap is a data structure in Java that is part of the Java Collections Framework. It is very similar to HashMap.It stores key-value pairs, where each key is unique, and it allows null values, but allows only one null key. The main difference is that the LinkedHashMap returns the elements in the order they were inserted. This is because it maintains a linked list with entries in the map, which helps it to retain the insertion order of the elements. Our task is to check if a particular key exists in a LinkedHashMap in Java. Scenario 1 Following is a ... Read More

Clone IdentityHashMap in Java

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

168 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 java.util.*; public class Demo {    public static void main(String[] args) {       IdentityHashMap m = new IdentityHashMap();       m.put("1", 100);       m.put("2", 200);       m.put("3", 300);       m.put("4", 150);       m.put("5", 110);       m.put("6", 50); ... Read More

Remove all elements from Java LinkedHashSet

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

377 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 java.util.LinkedHashSet; public class Demo { public static void main(String[] args) { LinkedHashSet hashSet = new LinkedHashSet(); hashSet.add(10); hashSet.add(20); hashSet.add(30); ... Read More

Create NavigableMap from TreeMap in Java

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

201 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 from TreeMap and display it −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put("A", 888); n.put("B", 999); ... Read More

Remove specified element from Java LinkedHashSet

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

768 Views

To remove a specified element from LinkedHashSet, use the remove() and include the element you want to remove as a parameter.First, set LinkedHashSet and add elements −LinkedHashSet hashSet = new LinkedHashSet(); hashSet.add(10); hashSet.add(20); hashSet.add(30); hashSet.add(40); hashSet.add(50); hashSet.add(60);Let us now remove an element −hashSet.remove(10);The following is an example to remove specified element from LinkedHashSet −Example Live Demoimport java.util.LinkedHashSet; public class Demo { public static void main(String[] args) { LinkedHashSet hashSet = new LinkedHashSet(); hashSet.add(10); hashSet.add(20); ... Read More

NavigableMap ceilingEntry() method in Java

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

113 Views

Use the NavigableMap ceilingEntry() method to returna key-value mapping associated with the least key greater than or equal to the given keyThe following is an example to implement ceilingEntry() method −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); n.put(14, "Jamie"); n.put(1, "Tim"); n.put(4, "Jackie"); n.put(15, "Kurt"); n.put(19, "Tiger"); n.put(24, "Jacob"); System.out.println("NavigableMap elements..."+n); System.out.println("Ceiling Entry = " + n.ceilingEntry(11)); } }OutputNavigableMap elements... {1=Tim, 4=Jackie, 5=Tom, 9=John, 14=Jamie, 15=Kurt, 19=Tiger, 24=Jacob} Ceiling Entry = 14=Jamie

NavigableSet Class higher() method in Java

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

121 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) { 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); System.out.println("Returned Value = " + set.higher(35)); } }OutputReturned Value = 40

Advertisements