Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple database-like collection class in Java
    primarykey
    data
    text
    <p>The problem: Maintain a bidirectional many-to-one relationship among java objects.</p> <p>Something like the Google/Commons Collections bidi maps, but I want to <em>allow</em> duplicate values on the forward side, and have <em>sets</em> of the forward keys as the reverse side values. Used something like this:</p> <pre><code>// maintaining disjoint areas on a gameboard. Location is a space on the // gameboard; Regions refer to disjoint collections of Locations. MagicalManyToOneMap&lt;Location, Region&gt; forward = // the game universe Map&lt;Region, &lt;Set&lt;Location&gt;&gt;&gt; inverse = forward.getInverse(); // live, not a copy Location parkplace = Game.chooseSomeLocation(...); Region mine = forward.get(parkplace); // assume !null; should be O(log n) Region other = Game.getSomeOtherRegion(...); // moving a Location from one Region to another: forward.put(parkplace, other); // or equivalently: inverse.get(other).add(parkplace); // should also be O(log n) or so // expected consistency: assert ! inverse.get(mine).contains(parkplace); assert forward.get(parkplace) == other; // and this should be fast, not iterate every possible location just to filter for mine: for (Location l : mine) { /* do something clever */ } </code></pre> <p>The simple java approaches are: 1. To maintain only one side of the relationship, either as a <code>Map&lt;Location, Region&gt;</code> or a <code>Map&lt;Region, Set&lt;Location&gt;&gt;</code>, and collect the inverse relationship by iteration when needed; Or, 2. To make a wrapper that maintains both sides' Maps, and intercept all mutating calls to keep both sides in sync.</p> <p>1 is O(n) instead of O(log n), which is becoming a problem. I started in on 2 and was in the weeds straightaway. (Know how many different ways there are to alter a Map entry?)</p> <p>This is almost trivial in the sql world (Location table gets an indexed RegionID column). Is there something obvious I'm missing that makes it trivial for normal objects?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload