java - How to convert long value to Set of string -
i have map values:
private static map<string, long> hodnotyudaju = new hashmap<string, long>(); static { hodnotyudaju.put("a", 1l); hodnotyudaju.put("b", 2l); hodnotyudaju.put("b", 4l); hodnotyudaju.put("d", 8l); hodnotyudaju.put("e", 16l); hodnotyudaju.put("f", 32l); hodnotyudaju.put("g", 64l); hodnotyudaju.put("h", 128l); hodnotyudaju.put("i", 256l); hodnotyudaju.put("j", 512l); . . . } and method convert set of value long
public static long bitovahodnotaseznamuudaju(set<string> udaje) { long bitovahodnota = 0; (string udaj : udaje) { long tmp = hodnotyudaju.get(udaj); if (tmp != null) bitovahodnota += tmp; } return bitovahodnota; } how can implement method vice versa ? need long create set set of string
update
methos should looks like:
public static set<string> bin2udaje(long binudaje) { set<string> udaje = new hashset<string>(); (map.entry<string, long> entry : hodnotyudaju.entryset()) { if (udaje operation entry.getvalue() ) { udaje.add(entry.getkey()); } return udaje; } }
it sounds should using enum instead of map. example, while you're guaranteed use values 1, 2, 4, 8... you'll end unambiguous answer - doing on general map won't work.
i suggest use enum has value string if need it, , can use 1l << value.ordinal() long value if want. represent general set of these values, however, can use enumset.
if isn't appropriate, please give more information.
edit: if really want stick existing structure, use:
public static set<string> valuetokeyset(long value) { set<string> keys = new hashset<string>(); (map.entry<string, long> entry : hodnotyudaju.entryset()) { if ((value & entry.getvalue()) != 0) { keys.add(entry.getkey()); } } return keys; } this will give unexpected results if map doesn't contain single-bit-set values.
Comments
Post a Comment