java - how to put map values into list with different type? -
- i have
map<integer,object> - i want put values of map te
list<string> - the
arraylist<string>(map.values)gives me error the constructor arraylist<string>(collection<object>) undefined
edit: thank you. did not make myself clear. object class objects name traveldata have few variables doubles, dates , strings. processing them , put map <integer,traveldata>. must put them list<string> , print them all. cant change type of list :(
solved by: override tostring method
multilangoffers = new arraylist<string>(); (traveldata value : offers.values()) { multilangoffers.add(value.tostring()); } thank !
depending on initialized map <integer, object> call map.values() return collection<object>. means had initialize arraylist as:
list<object> value = arraylist<object>(map.values()); but useless, because return of map.values() collection , when not depending on api parts of interface list stick that.
but want list<string> values of map string have this:
list<string> stringvalues = new arraylist<string>(); (object value : map.values()) { if (value instanceof string) { stringvalues.add((string) value); } } this iterating on values objects , checks instance type of value on string. if so, add value string list.
so makes questions strange, depending on comment. if map handling custom complex object instead of object. have access desired string value of object manualy:
list<string> stringvalues = new arraylist<string>(); (complexobj value : map.values()) { stringvalues.add(value.getdesiredstringvaluegettermethodifyouusebeansyntax()); }
Comments
Post a Comment