Does this datatype exist in Java?

I found this in redis and was trying to see if there is something like this in Java. Let's say I have the following data:

3:1
3:2
3:3
4:1

      

As you can see, the individual data points are unique, but the combination is unique. There is a command in redis:

sadd 3 1 2
sadd 3 3
sadd 4 1

      

This will give me something like this:

3 -> 1, 2, 3
4 -> 1

      

Doing something like smembers 3

(this will return everything for 3) or smembers 3 2

(this will return if it exists).

I'm wondering what is the closest I can get to this functionality in Java?

+3


source to share


5 answers


The Guava interface MultiMap

does exactly that. Note that it is implementation specific whether it allows duplicate pairs <K,V>

. It looks like you are after one where the K, V pairs are always unique. If so, review the class HashMultimap

.


However, if you want to browse your own, you are probably looking for a combination of Map

and Set

:Map<Integer,Set<Integer>>

When you add items (key, value) to the map:

  • First check if the key is there. If not, you need to add blank Set<Integer>

    .
  • Then do map.get(key).put(value);



If you want to get all elements with a specific key:

  • do map.get(key)

    and iterate over the result

If you want to see if a specific key / value pair exists:

  • do if(map.containsKey(key) && map.get(key).contains(value))

For extra credit, you can implement it all inside the wrapper. ForwardingMap

from guava might be a good place to start.

+7


source


You can create your own MultivalueMap class like this:



import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

public class MultiValueMap<T1, T2> {

    public Map<T1, List<T2>> map = null;

    public MultiValueMap(){
        this.map = new HashMap();
    }

    public void putList(T1 key, List<T2> list){

        map.put(key, list);
    }

    public void put(T1 key, T2 value){
        List<T2> list = null;
        if(map.get(key) == null){
            list = new ArrayList<T2>();
            map.put(key, list);
        }
        else {
            list = map.get(key);
        }
        list.add(value);
    }

    public List<T2> get(T1 key){
        return map.get(key);
    }

    public Set<T1> keySet(){

        return map.keySet();
    }

    public Map getMap(){

        return this.map;
    }

    public boolean contains(T1 key, T2 listValue){

        List<T2> list = map.get(key);

        return list.contains(listValue);
    }
}

      

+2


source


Nothing is directly built into Java. You can use something like

Map<Integer, Set<Integer>>

      

to keep the relationship. This kind of construction is discussed in the Java Tutorial on InterfaceMap

. You can also use something like Guava MultiMap<Integer, Integer>

.

+1


source


From Wikipedia:

In its outer layer, the Redis data model is a dictionary in which keys are mapped to values.

In other words, just use a map to store key value pairs. Please note: the map is only an interface. You will need to create a Map object using subclasses that implement the map interface, like HashMap, TreeMap, etc. I think you are confused between the data structure itself and the implementation of its methods. These functions that you mentioned can be easily implemented in Java.

+1


source


you can achieve this using the Framework collection in Java.

as you are looking for key-value pairs to be stored.

you can use Map and Set in Java.

Map<Integer ,Set<Integer>>

      

+1


source







All Articles