"Not tired yet? Let's carry on, then. I'd like to give you more details about Set and Map and what they can do."
"Set is a set, a group of unnumbered objects. The main feature of a Set is that it only contains unique objects, i.e. each element of the set is different. Here are operations you can perform on a set:"
Operation | Method |
---|---|
Add element(s) | add(), addAll() |
Remove element(s) | remove(), removeAll() |
Check for the presence of element(s) | contains(), containsAll() |
"And that's it?"
"Well, yes. You can also use the size() method to find out how many elements are in the set."
"What about Map?"
"Map is a set of pairs. It's like a Set, except it's a set of key-value pairs rather than unique elements. The only limitation is that each «key» must be unique. A Map can't contain two pairs with the same keys."
"Here's what we can do with Map:"
Operation | Method |
---|---|
Get a set of all pairs | entrySet() |
Get a set of all keys | keySet() |
Get a set of all values | values() |
Add a pair | put(key, value) |
Get the value for the specified key | get(key) |
Check whether the specified key is present | containsKey(key) |
Check whether the specified value is present | containsValue(value) |
Check whether the Map is empty | isEmpty() |
Clear the Map | clear() |
Remove the value for the specified key | remove(key) |
"This is much more interesting than Set."
"Yes. Although Map isn't as popular as List, it is used in many tasks."
GO TO FULL VERSION