java - Generic Map with Lists of Comparable -
i want map several lists, each of them composed of comparable objects, not same, there might list of doubles , list of strings, etc...
map<string, list<comparable<?>>>
unfortunately map defined above not useful because elements in each list can't compared. therefore had cheat , introduce type parameter class:
class myclass<t extends comparable<? super t>> { map<string, list<t>> mymap; } this not totally correct because not lists of same type. works, thing have do type cast t when add double list of doubles or string list of strings. make sure each type gets added lists of correct type.
is there better way solve problem?
the alternative have 1 map every type of list. make code uglier , thing lists sort them , insert them db string values, therefore call tostring on comparables.
you can use list<object> make generic , purpose.
might want instance of checks specific datatypes inside list.
map<string, list<object>> map = new hashmap<>(); list<object> list; list = new arraylist<>(arrays.aslist(new object[] { 1, 2, 3.45, "dev", 'a', true, false })); map.put("key1", list); list = new arraylist<>(arrays.aslist(new object[] { false, 100, 5.1234f, "user", 'z', true })); map.put("key2", list); (map.entry<string, list<object>> entry : map.entryset()) { system.out.println(entry.getkey() + " => " + entry.getvalue()); } output: key1 => [1, 2, 3.45, dev, a, true, false] key2 => [false, 100, 5.1234, user, z, true]
Comments
Post a Comment