CORE JAVA - Day 57
Agenda
values() method
keyset() method
entrySet() method
getKey(), getValue(), setValue()
method
values () method in HashMap: values() method returns a view of all
the values present in entries of the HashMap.
Example: To fetch all the values present inside the HashMap.
Full Stack Web Development HTML Rooman Technologies
Output:
In this example, values() method will return collection of values
stored in the HashMap and we are storing this in a variable values
which is of type collection.
Example: values () method in for each loop
Output:
Full Stack Web Development HTML Rooman Technologies
Here, the values() method returns a view of all values. The variable
value access each value from the view using for each loop.
keySet() method in HashMap: Returns a Set view of all the keys
present in entries of the HashMap.
Example:
Output:
Here, the keySet() method returns a view of all keys. The variable
key access each value from the view using for each loop.
Full Stack Web Development HTML Rooman Technologies
Example: To print key and value present in the HashMap
Output:
Here, we are printing the keys using keySet() method to view all the
keys present in the HashMap. Key is used to fetch the key present in
the view of keys using key we are fetching the value i.e using
get(key) method.
Full Stack Web Development HTML Rooman Technologies
Understanding entry and entrySet in the HashMap.
MAP
put()
Entry is simple key-value pair, entrySet is a get()
set of key-value pairs present inside the values()
HashMap. Map is an interface inside which keyset()
.
we have so many abstract methods, inside
.
the map interface there is another interface
called Entry. Inside the entry interface we Entry
have few more abstract method, let’s getKey()
understand all these methods with an getValue()
setValue()
example.
Example: entrySet() method
Full Stack Web Development HTML Rooman Technologies
Output:
Here, entrySet() method returns set of entrys present in the HashMap.
The variable entry access each value from the view of entrys using for
each loop.
Example: getKey() method
Full Stack Web Development HTML Rooman Technologies
Output:
Here, getKey() returns the key corresponding to the entry.
Example: getValue() method
Full Stack Web Development HTML Rooman Technologies
Output:
Here, getValue() method returns the value corresponding to this entry.
Example: SetValue() method
Output:
Here, setValue() method replaces the corresponding to this entry with
the specified value.
Full Stack Web Development HTML Rooman Technologies