I have some map with 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 which convert set of value to long
public static long bitovaHodnotaSeznamuUdaju(Set<String> udaje) {
long bitovaHodnota = 0;
for (String udaj : udaje) {
Long tmp = hodnotyUdaju.get(udaj);
if (tmp != null)
bitovaHodnota += tmp;
}
return bitovaHodnota;
}
how I can implement this method vice versa ? I need from long to create set set of String
UPDATE
methos should looks like:
public static Set<String> bin2Udaje(long binUdaje) {
Set<String> udaje = new HashSet<String>();
for (Map.Entry<String, Long> entry : hodnotyUdaju.entrySet()) {
if (udaje some operation entry.getValue() ) {
udaje.add(entry.getKey());
}
return udaje;
}
}
It sounds like you should really be using an
Enumfor this instead of a map. For example, while you’re guaranteed to use values 1, 2, 4, 8… you’ll end up with an unambiguous answer – but doing that on a general map won’t work.I would suggest you use an enum which has an extra value for the string if you need it, and you can use
1L << value.ordinal()to get alongvalue if you really want. To represent a general set of these values, however, you can simply useEnumSet.If this isn’t appropriate, please give us more information.
EDIT: If you really want to stick to your existing structure, you could use:
This will give unexpected results if your map doesn’t only contain single-bit-set values.