3

While doing some functional programming in Java 8, I needed a tuple like in any functional programming languages, but then I figured out that the language designer might be solving the need for Tuple by making the Bifunction.

If you need a function that's taking 2 parameters you have to use Bifunction not Function, but I could not find any Documentation about this, is Java 8 really missing Tuples, or Bifunction can replace it's use?

Edit 1:

this question not as "Does Java SE 8 have Pairs or Tuples?" as the majority here is Bifunction which is not mentioned in the other question, I think question title now is more descriptive.

Bassem Reda Zohdy
  • 12,662
  • 3
  • 33
  • 39
  • yes thanks for correcting me (y) – Bassem Reda Zohdy Nov 11 '15 at 19:28
  • 2
    There are good reasons for not including tuples (and pair). If you were considering using `Function, R>` then yes, `BiFunction` is what you should use. There is no explicit documentation telling you this. Another approach would be to create a tuple like class yourself (but with more sensible accessor methods than `left()` and `right()`), and use that. – aioobe Nov 11 '15 at 19:29
  • An extensive answer why there are no tuples: http://stackoverflow.com/a/24336841/875083 – Joachim Rohde Nov 11 '15 at 19:31
  • Also give this a read https://dzone.com/articles/whats-wrong-java-8-part-v – coderrick Nov 11 '15 at 19:32
  • What I really love about Java is that people would write long articles and white papers explaining why you don't need something :-) Fortunately there are third-party libraries that implement tuples and other convenient features. I use [Javaslang](http://javaslang.com/) – S.D. Nov 11 '15 at 19:35
  • for comments with answers and article they are all good answers but they are not providing Bifunction instead of Tuble except in dzone link "(int x, int y) -> x * y", @S.D. I'm going to check it and I'm believing there are other third-party libraries provides the same, but just wanted to know the reason why not in JDK ? – Bassem Reda Zohdy Nov 11 '15 at 19:44

1 Answers1

1

You should not use BiFunction since it doesn't really match the semantics of what you are trying to do. I also don't see how you would propose to get two different values out of a BiFunction since it only returns one element of one type.

Apache Commons-Lang has a tuple package package that will do.

But otherwise I'd suggest creating a class with two named fields so that the semantics of the pairing are captured.

class UserName {
    private String userName, phoneNumber;
}

communicates intent in the variable names and it is different from

class UserPassword {
    private String userName, password;
}

If you are using these in a limited scope (ie only within a class or package) I wouldn't bother with getters and setters.

Otherwise, you are trying to abstract over the shape of the data and not the type or semantics of the class which is very un-idiomatic in Java. For more on the topic see answer posted as a comment.

Community
  • 1
  • 1
Sled
  • 18,541
  • 27
  • 119
  • 168