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

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

问题描述

为什么没有(标准的,Java认证的)解决方案,作为Java语言本身的一部分,从Java方法返回多个值,而不是开发人员必须使用他们自己的方法,如地图,列表,对等?为什么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 6中有一个 java.util.AbstractMap.SimpleEntry 。也有人现在使用 Commons Lang3的Pair支持(2-tuple)

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-tuple支持作弊,并且拥有一大堆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.

更新:for Java 8

Java 8将/可能(所以我的号码......请给我打电话)支持一个名为 java.lang.BiValue 的接口,你可以使用一个名为<$ c的具体实现$ c> 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 - 现在支持Triple(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天全站免登陆