Javatuples of Java tuple types

Source: Internet
Author: User

Reprint: Java meta-group type Javatuples

With respect to the return value of a method, it is often necessary to return a sequence of 2 or more values, such as a record of a data table, a row of files, and so on. In addition to using array arrays, collections (list, set, MAP) of these container types, in Java we have to create a class to be the return type.

Tuple-type support is provided in many languages, such as the. NET Framework supports a maximum of 7 element tuples, refer to here:

C # code
var New tuple<stringintint int int int int.int>(                              " New York " 7891957 7781984 ,                               7894862 7071639 7322564 8008278);  

Scala supports up to 22 elements of tuples, see here:

JS Code
New Tuple4 (4,3,2,1)  //  syntactic sugar    = t._1 + t._2 + t._3 + t._4  

For the C + + (STL) tuple, refer to here:

C + + code
tuple<intstringstring> t (5"foo"" Bar " );   << T.get<1> ();  // outputs "foo"  

First look at Java if you want to return the "one" key value pair, what should I do?

(1) Apache Struts1 's Labelvaluebean

Java code
New Labelvaluebean ("rensanning.iteye.com", "9527");  

(2) Guava's maps.immutableentry

Java code
map.entry<string,integer> Entry2 = maps.immutableentry ("rensanning.iteye.com", 9527);  

(3) Apache Commons-collections's KeyValue

Java code
New Defaultmapentry ("rensanning.iteye.com", 9527);  KeyValueNew defaultkeyvalue ("rensanning.iteye.com", 9527);  


(4) Apache Commons-lang3 's pair

Java code
1 New Immutablepair<string, integer> ("rensanning.iteye.com", 9527);  

(5) Apache HttpClient's Namevaluepair

Java code
1 New Basicnamevaluepair ("rensanning.iteye.com", "9527");  


(6) Android pair

Java code
1 New Pair<string, integer> ("rensanning.iteye.com", 9527);  

。。。。。。 And so many more, you can also expand your own map.entry or package class.

Java 6 provides abstractmap.simpleentry<k,v> and abstractmap.simpleimmutableentry<k,v>

Java code
1 New Abstractmap.simpleentry<string, integer> ("rensanning.iteye.com", 9527);  

The two entity as static to embed in the Abstractmap marked as abstract, this API is quite strange!

Javatuples is a very simple lib, it does not have any gorgeous function, is to provide support to return multiple elements of some classes.

Https://github.com/javatuples/javatuples

Version: Javatuples-1.2.jar

Supports a maximum of 10 elements:

    • Unit<a> (1 Element)
    • Pair<a,b> (2 elements)
    • Triplet<a,b,c> (3 elements)
    • Quartet<a,b,c,d> (4 elements)
    • Quintet<a,b,c,d,e> (5 elements)
    • Sextet<a,b,c,d,e,f> (6 elements)
    • Septet<a,b,c,d,e,f,g> (7 elements)
    • Octet<a,b,c,d,e,f,g,h> (8 elements)
    • Ennead<a,b,c,d,e,f,g,h,i> (9 elements)
    • Decade<a,b,c,d,e,f,g,h,i,j> (elements)


Commonly used 2-tuple pair:

    • Keyvalue<a,b>
    • Labelvalue<a,b>
Java code
1 //1 tuples2unit<string> U =NewUnit<string> ("rensanning.iteye.com"); 3 //2 tuples4Pair<string,integer> p = pair.with ("rensanning.iteye.com", 9527); 5 //3 tuples6triplet<string,integer,double> Triplet = Triplet.with ("rensanning.iteye.com", 9527, 1.0); 7 //...  8           9keyvalue<string,string> kv = Keyvalue.with ("rensanning.iteye.com", "9527"); Tenlabelvalue<string,string> LV = labelvalue.with ("rensanning.iteye.com", "9527");

Reference Links:
http://tech.puredanger.com/2010/03/31/do-we-want-a-java-util-pair/
Http://mail.openjdk.java.net/pipermail/core-libs-dev/2010-March/003995.html

Javatuples of Java tuple types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.