Home » Java Collections newSetFromMap() Method with Examples

Java Collections newSetFromMap() Method with Examples

by Online Tutorials Library

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

Test it Now

Output:

Set is: [Karan, Raj, Rahul]  Map is: {Karan=true, Raj=true, Rahul=true}  

Example 2

Test it Now

Output:

Set is: [1]  Map is: {1=true}  

Example 3

Test it Now

Output:

Users: []  Users: [Java, PHP]  

You may also like