Can I have a dual key card in Java

Possible duplicate:
How to implement a card with multiple keys?

Imagine a table with columns K1, K2, and D. I may need to search for data using the keys K1 or K2, or both. Is there a β€œstandard” solution in Java for this, other than creating multiple maps containing links?

+4
source share
4 answers

Consider using guava Table .

Sample from the documentation

Table<Vertex, Vertex, Double> weightedGraph = HashTable.create(); weightedGraph.put(v1, v2, 4); weightedGraph.put(v1, v3, 20); weightedGraph.put(v2, v3, 5); weightedGraph.row(v1); // returns a Map mapping v2 to 4, v3 to 20 weightedGraph.column(v3); // returns a Map mapping v1 to 20, v2 to 5 
+1
source

If you want to search only tuples (k1, k2), I would suggest MultiKeyMap or, more likely, a wrapper object included in the standard Map . But if you want to search from both one and the other, it looks like the built-in SQL engine might be better.

EDIT: depending on your data size, whether you need (k1, k2, k3) a search, how many such structures you will need (the SQL mechanism is relatively heavy) you might be better off with Mayrbek Hadikov's answer to the Guava table.

0
source

If you want to search for data by a range of keys, then you can use a shipping card .

Take a look at this code:

 NavigableMap<String, String> orig = new TreeMap(); orig.put("1", "A"); orig.add("2", "B"); orig.add("3", "C"); orig.add("4", "D"); orig.add("5", "E"); //this submap will now contain "C", "C" SortedMap submap = orig.subMap("2", "4"); 
0
source

I would wrap the map in an object and provide some method of finding data using both keys. After that, you can simply cross 2 sets of results and return them. Nice and just :).

0
source

Source: https://habr.com/ru/post/1391267/


All Articles