In this section we will discuss how can values() method be implemented in Map interface in java. Collection
values() method of Map Interface in Java.
In this section we will discuss how can values() method be implemented in Map interface in java.
Syntax
Collection<V> values()
This method returns a view of collection of the values mappings from the underlying map with their keys.
In this method since the collection is returned by the map, so any change may occurs in the map are reflected into the collection, and vice-versa. Remove operation can be performed in this method these can be used as Iterator.remove, Collection.remove, removeAll(), retainAll() and clear operations. This method do not supports add() or addAll() operations.
Parameter description
This method has no parameter.
Example of values() method
In this example we will show you how does values() method work in Map interface. This example will help you to understand how you can find the collection of the values from the underlying map.
Example :
package devmanuals.com; import java.util.Map; import java.util.HashMap; import java.util.Collection; import java.util.Iterator; public class MapValues { public static void main(String args[]) { Map mp = new HashMap(); mp.put(1, "A"); mp.put(2, "B"); mp.put(3, "C"); mp.put(4, "D"); mp.put(5, null); System.out.println("Mappings are : " + mp); Collection c = mp.values(); System.out.println("Values from the set of key-value mappings : " + c); Iterator it = c.iterator(); while (it.hasNext()) System.out.println(it.next()); c.remove("C"); System.out.println("Values from the set of key-value mappings after modifying: "+c); System.out.println("The size of map = " + mp.size()); } }
Output :
Mappings are : {1=A, 2=B, 3=C, 4=D, 5=null} Values from the set of key-value mappings : [A, B, C, D, null] A B C D null Values from the set of key-value mappings after modifying : [A, B, D, null] The size of map = 4 |
[ 0 ] Comments