Found 34488 Articles for Programming

Java Program to retrieve the set of all values in HashMap

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

200 Views

First, create a HashMap and add elements −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200));Now, retrieve all the values −Collection getValues = hm.values(); System.out.println("Values..."); Iterator i = getValues.iterator(); while (i.hasNext()) { System.out.println(i.next()); }The following is an example to get the set of all the values in the HashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create hash map HashMap hm = new HashMap(); ... Read More

Java Program to copy all the key-value pairs from one Map into another

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

440 Views

To copy, use the putAll() method.Let us first create two Map −First Map −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Second Map −HashMap hm2 = new HashMap(); hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800));Now, copy the key-value pairs from one Map to another −hm.putAll(hm2);The following is an example to copy all the key-value pairs from one Map into another −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create hash map 1 HashMap hm = ... Read More

Iterate through the values of HashMap in Java

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

362 Views

Use Iterator to iterate through the values of HashMap −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, use Iterator to display each and every value and key −// Get an iterator Iterator i = set.iterator(); // Display elements while(i.hasNext()) { Map.Entry me = (Map.Entry)i.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); }The following is an example to iterate through the values of HashMap −Example Live Demoimport java.util.*; public class Demo { public static ... Read More

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

93 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 30-Jul-2019 22:30:24

499 Views

To remove a key from HashMap, use the remove() method.First, create a HashMap and add 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));Let us now remove a key −hm.remove("Wallet");The following is an example to remove a key from a HashMap −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       // Create a hash map       HashMap hm = new HashMap();       // Put elements ... Read More

Modify the value associated with a given key in Java HashMap

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

240 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

1K+ 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

290 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

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

241 Views

First, create a HashSet −HashSet hs = new HashSet();Now, add some elements using the add() method. Set the elements as a parameter. Here, we have set string −hs.add("B"); hs.add("A"); hs.add("D"); hs.add("E"); hs.add("C"); hs.add("F"); hs.add("K"); hs.add("M");The following is an example to add elements to a HashSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { HashSet hs = new HashSet(); // add elements to the hash set hs.add("B"); hs.add("A"); ... Read More

Advertisements