Found 9150 Articles for Object Oriented Programming

Java Program to remove a key from a HashMap only if it is associated with a given value

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

155 Views

Use the remove() method to remove a key only if it is associated with a given value.Let’s say the following is our HashMap −// Create a hash map HashMap hm = new HashMap(); // Put elements to the map hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Here, we are removing a key “Belt” only if it is associated with the given value 600 −hm.remove("Belt", 600);The following is an example to remove a key only if it is associated with a given value −Example Live Demoimport java.util.*; public class Demo {    public static void ... Read More

Java program to remove a key from a HashMap

karthikeya Boyini
Updated on 07-Nov-2024 01:07:01

696 Views

In this article, we will learn how to remove a specific key from a HashMap in Java. By doing this, we can dynamically update the contents of the map, which is helpful in many data manipulation scenarios. We will demonstrate two different approaches: one that uses the straightforward remove() method to delete a specific key, and another that leverages an iterator to find and remove the key based on a condition while iterating. Each approach allows us to efficiently manage data in HashMap based on different requirements. Problem Statement Given a HashMap with key-value pairs, write Java programs to ... Read More

Modify the value associated with a given key in Java HashMap

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

303 Views

First, create a HashMap and add elements to it −// Create a hash map HashMap hm = new HashMap(); // Put elements to the map hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Now, to modify the value associated with a given key, use the put() method. Here, we are modifying the value for the key “Frames” −hm.put("Frames", "900");The following is an example to modify the value associated with a given key −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { ... Read More

Get the value associated with a given key in Java HashMap

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

2K+ Views

Use the get() method to get the value associated with a given key.Here is our HashMap and its elements −// Create a hash map HashMap hm = new HashMap(); // Put elements to the map hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));To get the associated value. the key is set as a parameter like this −hm.get("Frames")Above, the key is Frames and we are fetching the value associated with it.The following is an example to get the value associated with a given key in Java HashMap −Example Live Demoimport java.util.*; public class Demo { ... Read More

Java Program to check if a given value exists in a HashMap

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

3K+ Views

Use the containsValue() method to check if a given value exists or not in a HashMap.First, let us create a HashMap and add some elements −HashMap hm = new HashMap(); // Put elements to the map hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Now let us check whether a given value exist or not. Here, we are checking for the value “800” −hm.containsValue(800);The following is an example to check if a given value exists in a HashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { ... Read More

Remove a range of elements from a LinkedList in Java

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

409 Views

A range of elements can be removed from a LinkedList by using the method java.util.LinkedList.clear() along with the method java.util.LinkedList.subList(). A program that demonstrates this is given as follows −Example Live Demoimport java.util.LinkedList; public class Demo { public static void main(String[] args) { LinkedList l = new LinkedList(); l.add("Clark"); l.add("Bruce"); l.add("Diana"); l.add("Wally"); l.add("Oliver"); System.out.println("The LinkedList is: ... Read More

Add elements to HashSet in Java

Aishwarya Naglot
Updated on 21-Jul-2025 11:49:27

393 Views

The java.util.HashSet class is a part of the Java collection framework. It stores unique elements, which means we cannot store the same element more than once. A HashSet can contain only one null value. If we try to add duplicate elements to a HashSet object, the elements will not be added. Adding elements to HashSet in JavaThe following are the methods used to add elements to an existing HashSet object: Using add() method Using Collections.addAll() method Using add() Method The add() method of the HashSet class is used for adding ... Read More

IdentityHashMap size() method in Java

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

95 Views

The size() method is used in IdentityHashMap to get the size of the map i.e. the count of elements.Let us first create IdentityHashMap and add some elements −Map 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);Now, get the size −m.size()The following is an example to get the size of the IdentityHashMap using size() method −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { Map m = new IdentityHashMap(); m.put("1", 100); ... Read More

Get the count of NavigableMap in Java

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

131 Views

To get the count of NavigableMap in Java, use the size() method.Let us first create NavigableMap and add elements −NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); n.put(14, "Jamie"); n.put(1, "Tim"); n.put(4, "Jackie");Now, get the count −n.size()The following is an example to implement the size() method to get the count of the NavigableMap elements −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"); ... Read More

Append all elements of other Collection to ArrayList in Java

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

480 Views

The elements of a Collection can be appended at the end of the ArrayList using the method java.util.ArrayList.addAll(). This method takes a single parameter i.e. the Collection whose elements are added to the ArrayList.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.Vector; public class Demo { public static void main(String[] args) { ArrayList aList = new ArrayList(); aList.add("John"); aList.add("Sally"); aList.add("Harry"); aList.add("Martha"); ... Read More

Advertisements