java - Nested Map or equivalent to store triple -
i have store values list/map contains 3 values:
size | amount | description 2,34 | 4 | "i'm long string" 1,14 | 2 | "i'm long string"
so thought using nested map :
map<double, map<integer,string>> = new hashmap<double, map<integer,string>>();
but how can easy add entries map? syntax
a.put(1,43, new map.entry<>(7,"9890"));
didn't work, cause map
abstract. best way nested map?
it's going tedious:
map<integer,string> val1 = a.get(2.34); if (val1 == null) { val1 = new treemap<integer, string>(); a.put(2.34, val1); } val1.put(4, "i'm long string");
i've updated treemap since want access smallest element. should change map "a" treemap too.
edit 2
i made effort here, hope it's you're looking :)
import java.io.ioexception; import java.util.map; import java.util.treemap; import java.util.treeset; public class main { public static void main(string[] args) throws ioexception { treemap<double, treemap<integer, string>> map = new treemap<>(); add(2.5, 5, "wrong value 1", map); add(3, 2, "wrong value 2", map); add(2.5, 3, "good value", map); system.out.println(map.pollfirstentry().getvalue().pollfirstentry().getvalue()); } public static void add(double val1, int val2, string val3, treemap<double, treemap<integer, string>> map) { treemap<integer,string> submap = map.get(val1); if (submap == null) { submap = new treemap<integer, string>(); map.put(val1, submap); } submap.put(val2, val3); } }
Comments
Post a Comment