あなたはCQEngine使用することができます。
IndexedCollection<GamePlayer> gamePlayers = new ConcurrentIndexedCollection<>();
SimpleAttribute<GamePlayer, Game> gameAttribute = attribute(GamePlayer::getGame);
SimpleAttribute<GamePlayer, Player> playerAttribute = attribute(GamePlayer::getPlayer);
gamePlayers.addIndex(HashIndex.onAttribute(gameAttribute));
gamePlayers.addIndex(HashIndex.onAttribute(playerAttribute));
Game spades = new Game("Spades");
Game hearts = new Game("Hearts");
Game rummy = new Game("Rummy");
Player joe = new Player("Joe", 23);
Player mary = new Player("Mary", 24);
Player jane = new Player("Jane", 19);
Player charles = new Player("Charles", 56);
Player daniel = new Player("Daniel", 29);
gamePlayers.add(new GamePlayer(hearts, joe));
gamePlayers.add(new GamePlayer(hearts, mary));
gamePlayers.add(new GamePlayer(hearts, jane));
gamePlayers.add(new GamePlayer(hearts, charles));
gamePlayers.add(new GamePlayer(spades, charles));
gamePlayers.add(new GamePlayer(spades, jane));
gamePlayers.add(new GamePlayer(spades, daniel));
gamePlayers.add(new GamePlayer(spades, mary));
gamePlayers.add(new GamePlayer(rummy, charles));
gamePlayers.add(new GamePlayer(rummy, jane));
System.out.println("Spades' players:");
for (GamePlayer spadesGamePlayer : gamePlayers.retrieve(equal(gameAttribute, spades))) {
Player player = spadesGamePlayer.getPlayer();
System.out.println(" - " + player.getName() + " (age " + player.getAge() + ")");
}
System.out.println();
System.out.println("Mary's games:");
for (GamePlayer maryGamePlayer : gamePlayers.retrieve(equal(playerAttribute, mary))) {
Game game = maryGamePlayer.getGame();
System.out.println(" - " + game.getName());
}
出力:
Spades' players:
- Daniel (age 29)
- Jane (age 19)
- Mary (age 24)
- Charles (age 56)
Mary's games:
- Hearts
- Spades
私の答えに印をつけた人が役に立たないと答えた人がいたら、それはいいだろう。 – mfulton26