2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaのMapでValueからKeyを逆引き

Posted at

JavaのMapが双方向じゃないので、KeyからValueを取り出す事はできても逆にValueからKeyを取り出すことができない。
なんかそれ用のリスト型のライブラリもあるっぽいけど、うまいやり方はないものか…

Stream.of(fooMap)
    .takeUntil(fooItem -> fooItem.getValue() == targetValue)
    .findLast().get().getKey();

Lightweight-Stream-API使ってこう書いてやってるけど、これがあってるのかどうかもよくわからない。
というかできればO(1)で探したいんだけど…やっぱり逆向きのMapを作ってやるしかないのかなあ?

2
2
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?