Java map with 3-column keys

I need a map in which my key should be based on 3 columns, say C1, C2, C3. C1has the highest priority. C2has one less than C1, and C3has less than C2.

How to create a key on the map so that if someone asks for information about C1, I have to give all the values ​​that have C1. I should also be able to return all values ​​if requestedC1 & C2

+5
source share
8 answers

You can use the same strategy as multi-column indexes in databases if your key columns can be ordered (i.e. in Java, they should be Comparable) and can easily determine the maximum and minimum values ​​for all but the first.

An example with integer columns:

public class Key implements Comparable<Key> {
    int c1, c2, c3;

    private static final int c2_min = Integer.MIN_VALUE;
    private static final int c2_max = Integer.MAX_VALUE;
    private static final int c3_min = Integer.MIN_VALUE;
    private static final int c3_max = Integer.MAX_VALUE;

    @Override
    public int compareTo(Key o) {
        if (c1!=o.c1) return Integer.compare(c1, o.c1);
        if (c2!=o.c2) return Integer.compare(c2, o.c2);
        return Integer.compare(c3, o.c3);
    }

    // constructor, equals, ...

}

and then you can get all the entries for some value k1in the c1following way:

map.subMap(new Key(k1, Key.c2_min, 0), new Key(k1, Key.c2_max, 0));

Similarly, using the first two columns:

map.subMap(new Key(k1, k2, Key.c3_min), new Key(k1, k2, Key.c3_max));
+2
source

Use three cards.

One Map<C1, V> and one Map<C2, V> and one Map<C3, V>. 

You can wrap three cards in a class and implement your method.

+2
source

"" , , , .

, . C1 + C2 + C3 ( "+" ).

0

. .

.

0

, , .

class ThreeLevelMap<K1,K2,K3,V>
{
    private Map<K1,Map<K2,Map<K3,V>>> store = new HashMap<K,Map<K2,Map<K3,V>>>();
    ...
    public V put(K1 key1, K2 key2, K3 key3, V value) { ... }
    public V get(K1 key1, K2 key2, K3 key3) { ... }

    public static class TLMEntry<K1,K2,K3,V>
    {
    ...
    }
    public Collection<TLMEntry<K1,K2,K3,V>> get(K1 key1, K2 key2) { ... }
    public Collection<TLMEntry<K1,K2,K3,V>> get(K1 key1) { ... }
}

, .

0

. :

CREATE TABLE MyMap (
    id IDENTITY PRIMARY KEY,
    c1 int, -- Change data types as needed.
    c2 int,
    c3 int,
    v  int);

SELECT. , Java .

, Java , :

class Cdata {
    private int c1;
    private int c2;
    private int c3;
    private int v;
    // Constructors and getters elided.
    public boolean match(int c1) {
        return this.c1 == c1;
    }
    public boolean match(int c1, int c2) {
        return match(c1) && this.c2 == c2;
    }
    public boolean match(int c1, int c2, int c3) {
        return match(c1, c2) && this.c3 == c3;
    }
}

. , Java 8 lambdas. Map<Integer, Map<Integer, Map<Integer, Integer>>>> .

0

TreeMap . : 3 i.e

C1 = 1, C2 = 2, C3 = 3

C1 = 1 - , C2 = 2 - ..

. , , Comparator TreeMap.

- :

TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();
treeMap.put(1, "One");
treeMap.put(2, "two");
treeMap.put(3, "three");

List<String> list = getMappedValues(treeMap, 1);// returns One, Two, Three
//List<String> list = getMappedValues(treeMap, 2);// returns Two, Three
//List<String> list = getMappedValues(treeMap, 3);// returns Three
//List<String> list = getMappedValues(treeMap, 4);// returns null
if(list != null){
    //do something with the list of values
}

private static List<String> getMappedValues(TreeMap<Integer, String> map, Integer key) {
    Entry<Integer, String> e = map.ceilingEntry(key);
    if(e == null){
        return null;
    }
    List<String> list = new ArrayList<String>();
    while(e != null){
        list.add(e.getValue());
        key = e.getKey();
        e = map.higherEntry(key);
    }
    return list;
}
0

private class C {

    public C() {
        Map <Object ,String> ObjectC =new HashMap<Object, String>();
    }
}

private class B {

    public B() {
        Map <Object ,C> ObjectB =new HashMap<Object, C>();
    }
}

private class A {

    public A() {
        Map <Object ,B> ObjectA =new HashMap<Object, B>();
    }
}

, , , A

MapVar.ObjectA.get(C1);

C1, C2.

MapVar.ObjectA.get(C1).ObjectB.get(C2);

C1, C2, C3.

MapVar.ObjectA.get(C1).ObjectB.get(C2).ObjectC.get(C3);;

.

0
source

All Articles