Is there a native data structure in java that accepts key value pairs and allows duplicates? I am creating a checklist of characters in a string but some characters occur more than once.
ex
j -> false
a -> false
v -> false
a -> false
Is there a native data structure in java that accepts key value pairs and allows duplicates? I am creating a checklist of characters in a string but some characters occur more than once.
ex
j -> false
a -> false
v -> false
a -> false
You can simulate multiple key-value (KV) pairs by saving a list of values for each in a map. This is a standard implementation approach for "multivalue" maps.
So, if the key is a Character
object and the value is Boolean
, you can do
Map<Character, List<Boolean>> multimap = new HashMap<Character, List<Boolean>>();
and every time you want to add a new value to an existing KV pair in the map just call
multimap.get(key).add(value);
where key
is the Character and value
its corresponding Boolean
value.
The Guava
library by Google
(free download) has a Multimap interface implemented in various ways, so essentially you can instantiate a MultiMap<Character, Boolean>
map and use it accordingly. Similarly, you can get the Apache Commons Collections
library and use its MultiValueMap
class. You may also want to check the answers to a similar StackOverflow question, or another one.
If you only want to store one of each value per key, then a Set
should be used in the place of the List
.
Use a List
of Pair
s:
public class Pair<T, U> {
public final T key;
public final U value;
public Pair(T key, U value) {
this.key = key;
this.value = value;
}
}
public class YourApp {
public static void main(String[] args) {
List<Pair<Character, Boolean>> charList = new ArrayList<Pair<Character, Boolean>>();
charList.add(new Pair('j', false));
charList.add(new Pair('a', false));
charList.add(new Pair('v', false));
charList.add(new Pair('a', false));
for (Pair<Character, Boolean> pair : charList) {
System.out.println(pair.key + " -> " + pair.value);
}
}
}
With the selfwritten generic Pair
class you can hold a key and a value of any type you want. If you're adding pairs to a List
, you can even hold duplicates of pair entries.
You can use MultiMap<Character,Boolean>
bcoz it allows duplicate key which exist in org.apache.commons.collections
package.
or
You can use ArrayList
and add the objects of the Class
that contain attribute as char & boolean pair.
I do not know of a build in solution.
A quick alternative would be to use a simple ArrayList, and create an object that is a char/boolean pair that you can add to it.
commons.apache.org have MultiHashMap class. Try this one...!!!
MultiHashMap mp = new MultiHashMap();
mp.put("a", "1");
mp.put("b", "4");
mp.put("c", "2");
mp.put("a", "6");
List list = null;
Set set = mp.entrySet();
Iterator i = set.iterator();
while (i.hasNext()) {
Map.Entry<String, List<String>> me = (Map.Entry) i.next();
for(int j = 0 ; j< me.getValue().size(); j++ ){
System.out.println(me.getKey() +" : " +me.getValue().get(j));
}
}
}