65
Java Collections newSetFromMap() Method
The newSetFromMap() method of Java Collections class is used to return a set backed by the specified map. The resulting set displays the same ordering, concurrency, and performance characteristics as the backing map.
Syntax
Following is the declaration of newSetFromMap() method:
Parameter
Parameter | Description | Required/Optional |
---|---|---|
map | It is the backing map. | Required |
Returns
The newSetFromMap() method returns the set backed by the specified map.
Exceptions
IllegalArgumentException– It throws this exception if map is not empty.
Compatibility Version
Java 1.6 and above
Example 1
Output:
Set is: [Karan, Raj, Rahul] Map is: {Karan=true, Raj=true, Rahul=true}
Example 2
Output:
Set is: [1] Map is: {1=true}
Example 3
Output:
Users: [] Users: [Java, PHP]
Next TopicJava Collections Class