Does Java SE 8 have Pairs or Tuples?

前端 未结 9 880
独厮守ぢ
独厮守ぢ 2020-11-28 18:37

I am playing around with lazy functional operations in Java SE 8, and I want to map an index i to a pair / tuple (i, value[i]), then <

9条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 19:21

    Eclipse Collections has Pair and all combinations of primitive/object Pairs (for all eight primitives).

    The Tuples factory can create instances of Pair, and the PrimitiveTuples factory can be used to create all combinations of primitive/object pairs.

    We added these before Java 8 was released. They were useful to implement key/value Iterators for our primitive maps, which we also support in all primitive/object combinations.

    If you're willing to add the extra library overhead, you can use Stuart's accepted solution and collect the results into a primitive IntList to avoid boxing. We added new methods in Eclipse Collections 9.0 to allow for Int/Long/Double collections to be created from Int/Long/Double Streams.

    IntList list = IntLists.mutable.withAll(intStream);
    

    Note: I am a committer for Eclipse Collections.

提交回复
热议问题