从 Java 方法返回多个值:为什么没有 n 元组对象? [英] Return multiple values from a Java method: why no n-tuple objects?

查看:21
本文介绍了从 Java 方法返回多个值:为什么没有 n 元组对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么没有一个(标准的、Java 认证的)解决方案,作为 Java 语言本身的一部分,从 Java 方法返回多个值,而不是开发人员必须使用自己的方法,例如 Maps、Lists、对等?为什么 Java 不支持 n 元组对象?

Why isn't there a (standard, Java certified) solution, as part of the Java language itself, to return multiple values from a Java method, rather than developers having to use their own means, such as Maps, Lists, Pairs, etc.? Why does Java not support n-tuple objects?

特别考虑可能会一起(串联)修改两个对象的微不足道的私有方法,在这种情况下,将类型化对象作为返回值听起来有些矫枉过正.

Especially thinking for trivial private methods that may modify two objects together (in tandem), and in which case a typed-object as a return sounds overkill.

推荐答案

我认为 OP 的意思是为什么 Java 不支持 n 元组对象?".Python、Haskell、Lisp、ML 等具有异构 n 元组功能.同样,在一种语言中显然返回多个对象的能力常常是语法糖(即在 python 中返回 'a','b').

I assume the OP means "Why does Java not support n-tuple objects?". Python, Haskell, Lisp, ML etc have heterogeneous n-tuple capabilities. Also often times the ability to apparently return multiple objects in a language is syntactical sugar (ie in python return 'a','b').

原因当然是语言设计和一致性.Java 更喜欢非常明确的并且不喜欢匿名数据结构(虽然我希望我们有匿名闭包).

The reason of course is language design and consistency. Java prefers being very explicit and does not like anonymous data structures (although I wish we had anonymous closures).

例如在 Java 中,没有办法说我想要一个接受这些类型参数并返回它的回调.有些人认为这是一个巨大的弱点,其他人则喜欢一致性和明确性.

For example in Java there is no way to say I would like a callback that takes these type parameters and returns this. Some people feel this a huge weakness others like the consistency and explicitness.

恕我直言,虽然很烦人,但我经常通过创建静态内联类来解决这个问题:

IMHO although its annoying I frequently combat this issue by making static inline classes:

private static class Combo {
   String name;
   int weight;
}

是的,它很乏味,但后来我经常重用和重构这些类,使它们成为顶级并添加行为.事实上,采用这条路线的优势之一是,在匿名数据结构(如 FP 语言)中添加新字段要容易得多,添加字段变得更加困难(最终会更改大量代码).

Yes its tedious but then later on I often reuse and refactor those classes making them top level and adding behavior. Infact one of the advantages with going this route is that its much easier to add new fields where is the anonymous data structure (like in FP languages) it becomes much more difficult to add a field (you end up changing a ton of code).

我应该注意到,对于 2 元组,有些人使用(或滥用)java.util.Map.Entry,因为有一个 java.util.AbstractMap.SimpleEntry在 Java 6. 还有一些人现在使用 Commons Lang3 的 Pair 支持(2 元组).

I should note that for 2-tuples some people use (or abuse) java.util.Map.Entry as there is an java.util.AbstractMap.SimpleEntry in Java 6. Also Some people now use Commons Lang3's Pair support (2-tuple).

Scala 通过某种作弊方式支持 n 元组,并拥有一大堆 2-16 元组接口,这些接口在语言中是标准的,并且在语法上对程序员隐藏.

Scala has n-tuple support by sort of cheating and having a whole bunch of 2-16 tuple interfaces that are standard in the language and are syntactically hidden from the programmer.

纯粹出于教育原因,您可能想了解其他语言如何实现这一点.

For purely educational reasons you may want to see how other languages accomplish this.

更新:Java 8

Java 8 将/也许(所以这里是我的号码……也许可以打电话给我)支持一个名为 java.lang.BiValue 的接口,以及您可以使用的名为 java 的具体实现.lang.BiVal .这些类是为了帮助支持新的 lambda 功能.但请注意,这仅适用于 2 元组.

Java 8 will/maybe (so heres my number... call me maybe) support an interface called java.lang.BiValue with a concrete implementation that you can use called java.lang.BiVal . These classes are to help support the new lambda functionality. But notice this is only for 2-tuples.

更新:2015 年

Java 8 没有获得对元组的支持.

Java 8 did not gain support for tuples.

更新:来自作者 2015

如果你仍然想要元组支持,有三个库可以很好地支持元组:

If you still would like tuple support there are three libraries that support tuples well:

  • javatuples - 支持 JDK 5 及更高版本.最多 10 个元组.
  • JOOλ - 来自 jOOQ 的作者,但需要 JDK 8.
  • Commons Lang 3 - 现在支持三元组(3-tuple)并支持JDK 6 及更高版本.
  • javatuples - Supports JDK 5 and above. Up to 10-tuple.
  • JOOλ - From the author of jOOQ but requires JDK 8.
  • Commons Lang 3 - Now supports a Triple (3-tuple) and supports JDK 6 and above.

这篇关于从 Java 方法返回多个值:为什么没有 n 元组对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆