java - Hashmap's getValue returns Object -
i've got following data structure:
cfu66=[{bild1=cfu6606}, {bild2=cfu6603}, {bild3=cfu6605}, {bild4=cfu6601}, {bild5=cfu6602}]
structure: hashmap_1(string key, list(hashmap_2(string key, string value)))
i'm trying access values hashmap_2
:
// each hashmap_1 entry (map.entry<string, list> csvdictentry : csvdict.entryset()) { // each list in entry.getvalue (list<hashmap> hashlist : csvdictentry.getvalue()) { // each hashmap_2 in list (hashmap<string, string> hashlistdict : hashlist) { // each entry in hashmap_2 print value (map.entry<string, string> entry :hashlistdict.entryset()){ system.out.println(entry.getvalue()); } } } }
the compiler gives message, csvdictentry.getvalue()
in second for-loop returns object
instead of hashmap
. why?
however, i'm pretty new java , i'm sure there more convenient way this.
based on
for (map.entry<string, list> csvdictentry : csvdict.entryset()) {
i assume type of csvdict
map<string, list> csvdict = ...;
where based on data example cfu66=[{bild1=cfu6606}, {bild2=cfu6603}, {bild3=cfu6605}, {bild4=cfu6601}, {bild5=cfu6602}]
it should
map<string, list<map<string,string>>> csvdict = ...;
problem reference type list
raw-type, means actual type unknown (it can store kind of objects) object
type compiler assumes when trying iterate on such list when expect
for (type t : list<type>)
for raw type getting
for (object o : rawlist)
other problem way iterating because if change reference proper type
for (list<hashmap> hashlist : csvdictentry.getvalue())
will not compile because getvalue()
returns list<hashmap>
loop iterate on hashmap
s, not list
of hashmap
s should
for (hashmap hashlist : csvdictentry.getvalue())
hint: try avoid concrete types in generics, use parent type if possible, interface or abstract type. allow later changing actual type, instance hashmap
linkedhashmap
.
so iteration should
for (map.entry<string, list<map<string, string>>> csvdictentry : csvdict.entryset()) { // each map in list stored value (map<string, string> hashlistdict : csvdictentry.getvalue()) { // each entry in hmap_2 print value (map.entry<string, string> entry : hashlistdict.entryset()) { system.out.println(entry.getvalue()); } } }
btw in java 8 code simplified to
csvdict.entryset().stream() .flatmap(m -> m.getvalue().stream()) .flatmap(m -> m.values().stream()) .foreach(system.out::println);
Comments
Post a Comment