Java Reference
In-Depth Information
Unlike List , Set , and Queue , Map does not extend Collection . However, it
is possible to view a map as a Collection instance by calling Map 's keySet() ,
values() , and entrySet() methods, which respectively return a Set of keys, a
Collection of values, and a Set of key/value pair entries.
Note The values() methodreturns Collection insteadof Set becausemul-
tiplekeyscanmaptothesamevalue,and values() wouldthenreturnmultiplecop-
ies of the same value.
The Collection viewsreturnedbythesemethods(recallthata Set isa Collec-
tion because Set extends Collection ) provide the only means to iterate over a
Map .Forexample,supposeyoudeclare Listing5-17 ' s Color enumwithitsthree Co-
lor constants, RED , GREEN , and BLUE .
Listing 5-17. A colorful enum
enum Color
{
RED(255, 0, 0),
GREEN(0, 255, 0),
BLUE(0, 0, 255);
private int r, g, b;
private Color(int r, int g, int b)
{
this.r = r;
this.g = g;
this.b = b;
}
@Override
public String toString()
{
return "r = "+r+", g = "+g+", b = "+b;
}
}
Search WWH ::




Custom Search