EnumMap
is similar toMap
, except thatEnumMap
only takes theEnum
type as keys. Also, all the keys must be from a singleenum
type. For further details, refer here.
The toString()
method returns the String
representation of the EnumMap
object.
public String toString()
This method doesn’t take any parameters.
toString()
method returns String
as a result.entrySet
method.{}
.map
is separated by a comma ,
.key = value
.key
and value
are converted to strings with the String.valueOf()
method.import java.util.EnumMap;class ToString {enum Subject {MATHS, SCIENCE, PROGRAMMING, ECONOMICS};public static void main( String args[] ) {EnumMap<Subject, Integer> map = new EnumMap<>(Subject.class);map.put(Subject.MATHS, 50);map.put(Subject.SCIENCE, 60);map.put(Subject.PROGRAMMING, 70);System.out.println(map.toString());}}
In the code above, we:
Create an Enum
for the subjects with the name Subject
.
Create an EnumMap
with the name map
.
Add three entries to map
.
Use the toString()
method to get the string representation of map
.