简体   繁体   English

Java中的三元组是什么?

[英]What is a Triple in Java?

I've come across code that looks like the following:我遇到了如下代码:

public List<Triple<String, String, Instant>> methodName() {
    // Do something
}

What is the Triple , how should It be used?什么是Triple ,应该如何使用?

Triple is useful when you want to save 3 values at a time and you can pass different data types.当您想一次保存 3 个值并且可以传递不同的数据类型时, 三重非常有用。 If you just want to learn then below is an example of its usage but if you want to use in code then I would suggest you to use Objects instead.如果您只是想学习,那么下面是它的用法示例,但是如果您想在代码中使用,那么我建议您改用Objects

public class Triple<T, U, V> {

    private final T first;
    private final U second;
    private final V third;

    public Triple(T first, U second, V third) {
        this.first = first;
        this.second = second;
        this.third = third;
    }

    public T getFirst() { return first; }
    public U getSecond() { return second; }
    public V getThird() { return third; }
}

And this is how you can instantiate it:这就是您可以实例化它的方式:

List<Triple<String, Integer, Integer>> = new ArrayList<>();

EDIT编辑

As discussed in comments, please note that it belongs to org.apache.commons.lang3.tuple It is not a built-in class in Java.正如评论中所讨论的,请注意它属于org.apache.commons.lang3.tuple它不是Java中的内置类。

Think "tuple" for 3 values!考虑 3 个值的“元组”!

Many programming languages provide means to efficiently deal with lists of a "fixed" length but different types for each entry.许多编程语言提供了有效处理“固定”长度但每个条目类型不同的列表的方法。

That Triple class is the Java way of providing you something like that. Triple 类是为您提供类似内容的 Java 方式。 Like a Pair, but one more entry.就像一对,但多了一个条目。

At its core, a fixed length tuple allows you to "loosely couple" multiple values of different types based on some sort of "ordering".在其核心,固定长度的元组允许您基于某种“排序”“松散耦合”不同类型的多个值。

When you need a data structure to deal with a list of three entities, you might use a Triple.当您需要一个数据结构来处理三个实体的列表时,您可能会使用 Triple。 Here's a small example:这是一个小例子:

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.tuple.Triple;
    
public class Test {

    public static void main(String[] args) {
        List<Triple<Integer, String, String>> tripleList = new ArrayList<>();
        tripleList.add(Triple.of(100, "Japan", "Tokyo"));
        tripleList.add(Triple.of(200, "Italy", "Rome"));
        tripleList.add(Triple.of(300, "France", "Paris"));

        for (Triple<Integer, String, String> triple : tripleList) {
            System.out.println("Triple left = " + triple.getLeft());
            System.out.println("Triple right = " + triple.getRight());
            System.out.println("Triple middle = " + triple.getMiddle());
        }
    }

}

Output:输出:

Triple left = 100三左 = 100
Triple right = Tokyo三重权利 = 东京
Triple middle = Japan三重中间=日本
Triple left = 200三左 = 200
Triple right = Rome三重权利 = 罗马
Triple middle = Italy三重中间=意大利
Triple left = 300三左 = 300
Triple right = Paris三重权利 = 巴黎
Triple middle = France三重中间=法国

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM