JavaTuples 中的 Triplet 类
Triplet是JavaTuples库中处理 3 个元素的元组。由于这个 Triplet 是一个泛型类,它可以包含任何类型的值。
由于 Triplet 是一个元组,因此它也具有 JavaTuples 的所有特性:
- 它们是类型安全的
- 它们是不可变的
- 它们是可迭代的
- 它们是可序列化的
- 它们是可比较的(实现 Comparable
) - 他们实现了 equals()和hashCode()
- 他们还实现了 toString()
类声明
public final class Triplet extends Tuple
implements IValue0, IValue1, IValue2
类层次结构
创建三元组
- 从构造函数:
语法:
示例:
Java
// Below is a Java program to create
// a Triplet tuple from Constructor
import java.util.*;
import org.javatuples.Triplet;
class GfG {
public static void main(String[] args)
{
Triplet triplet
= new Triplet(Integer.valueOf(1),
"GeeksforGeeks", "A computer portal");
System.out.println(triplet);
}
}
Java
// Below is a Java program to create
// a Triplet tuple from with() method
import java.util.*;
import org.javatuples.Triplet;
class GfG {
public static void main(String[] args)
{
Triplet triplet
= Triplet.with(Integer.valueOf(1),
"GeeksforGeeks", "A computer portal");
System.out.println(triplet);
}
}
Java
// Below is a Java program to create
// a Triplet tuple from Collection
import java.util.*;
import org.javatuples.Triplet;
class GfG {
public static void main(String[] args)
{
// Creating Triplet from List
List list = new ArrayList();
list.add("GeeksforGeeks");
list.add("A computer portal");
list.add("for geeks");
Triplet triplet
= Triplet.fromCollection(list);
// Creating Triplet from Array
String[] arr = { "GeeksforGeeks",
"A computer portal",
"for geeks" };
Triplet otherTriplet
= Triplet.fromArray(arr);
System.out.println(triplet);
System.out.println(otherTriplet);
}
}
Java
// Below is a Java program to get
// a Triplet value
import java.util.*;
import org.javatuples.Triplet;
class GfG {
public static void main(String[] args)
{
Triplet triplet
= Triplet.with(Integer.valueOf(1),
"GeeksforGeeks",
"A computer portal");
System.out.println(triplet.getValue0());
System.out.println(triplet.getValue2());
}
}
Java
// Below is a Java program to set
// a Triplet value
import java.util.*;
import org.javatuples.Triplet;
class GfG {
public static void main(String[] args)
{
Triplet triplet
= Triplet.with(Integer.valueOf(1), "GeeksforGeeks",
"A computer portal");
Triplet otherTriplet
= triplet.setAt1("A computer portal for geeks");
System.out.println(otherTriplet);
}
}
Java
// Below is a Java program to add
// a value
import java.util.*;
import org.javatuples.Triplet;
import org.javatuples.Quartet;
class GfG {
public static void main(String[] args)
{
Triplet triplet
= Triplet.with(Integer.valueOf(1),
"GeeksforGeeks",
"A computer portal");
Quartet quartet
= triplet.addAt3("for geeks");
System.out.println(quartet);
}
}
Java
// Below is a Java program to search
// a value in a Triplet
import java.util.*;
import org.javatuples.Triplet;
class GfG {
public static void main(String[] args)
{
Triplet triplet
= Triplet.with(Integer.valueOf(1),
"GeeksforGeeks",
"A computer portal");
boolean exist = triplet.contains("GeeksforGeeks");
boolean exist1 = triplet.contains(4);
System.out.println(exist);
System.out.println(exist1);
}
}
Java
// Below is a Java program to iterate
// a Triplet
import java.util.*;
import org.javatuples.Triplet;
class GfG {
public static void main(String[] args)
{
Triplet triplet
= Triplet.with(Integer.valueOf(1),
"GeeksforGeeks",
"A computer portal");
for (Object item : triplet)
System.out.println(item);
}
}
输出:
[1, GeeksforGeeks, A computer portal]
- 使用 with() 方法:with() 方法是 JavaTuples 库提供的一个函数,用于实例化具有此类值的对象。
语法:
Triplet triplet =
Triplet.with(value1, value2, value3);
示例:
Java
// Below is a Java program to create
// a Triplet tuple from with() method
import java.util.*;
import org.javatuples.Triplet;
class GfG {
public static void main(String[] args)
{
Triplet triplet
= Triplet.with(Integer.valueOf(1),
"GeeksforGeeks", "A computer portal");
System.out.println(triplet);
}
}
输出:
[1, GeeksforGeeks, A computer portal]
- 从其他集合: fromCollection() 方法用于从集合创建元组,fromArray() 方法用于从数组创建。集合/数组必须与元组具有相同的类型,并且集合/数组中的值的数量必须与元组类匹配。
语法:
Triplet triplet =
Triplet.fromCollection(collectionWith_2_value);
Triplet triplet =
Triplet.fromArray(arrayWith_2_value);
示例:
Java
// Below is a Java program to create
// a Triplet tuple from Collection
import java.util.*;
import org.javatuples.Triplet;
class GfG {
public static void main(String[] args)
{
// Creating Triplet from List
List list = new ArrayList();
list.add("GeeksforGeeks");
list.add("A computer portal");
list.add("for geeks");
Triplet triplet
= Triplet.fromCollection(list);
// Creating Triplet from Array
String[] arr = { "GeeksforGeeks",
"A computer portal",
"for geeks" };
Triplet otherTriplet
= Triplet.fromArray(arr);
System.out.println(triplet);
System.out.println(otherTriplet);
}
}
输出:
[GeeksforGeeks, A computer portal, for geeks]
[GeeksforGeeks, A computer portal, for geeks]
获得价值
getValueX() 方法可用于获取元组中索引 X 处的值。元组中的索引从 0 开始。因此索引 X 处的值表示位置 X+1 处的值。
语法:
Triplet triplet =
new Triplet(value1, value2, value3);
type1 val1 = triplet.getValue0();
示例:
Java
// Below is a Java program to get
// a Triplet value
import java.util.*;
import org.javatuples.Triplet;
class GfG {
public static void main(String[] args)
{
Triplet triplet
= Triplet.with(Integer.valueOf(1),
"GeeksforGeeks",
"A computer portal");
System.out.println(triplet.getValue0());
System.out.println(triplet.getValue2());
}
}
输出:
1
A computer portal
设置三元组值
由于元组是不可变的,这意味着修改索引处的值是不可能的。因此,JavaTuples 提供了 setAtX(value) ,它在索引 X 处创建具有新值的元组副本,并返回该元组。
语法:
Triplet triplet =
new Triplet
(value1, value2, value3);
Triplet
otherTriplet = triplet.setAtX(value);
示例:
Java
// Below is a Java program to set
// a Triplet value
import java.util.*;
import org.javatuples.Triplet;
class GfG {
public static void main(String[] args)
{
Triplet triplet
= Triplet.with(Integer.valueOf(1), "GeeksforGeeks",
"A computer portal");
Triplet otherTriplet
= triplet.setAt1("A computer portal for geeks");
System.out.println(otherTriplet);
}
}
输出:
[1, GeeksforGeeks, A computer portal for geeks]
添加价值
添加值可以在addAtX()方法的帮助下完成,其中 X 表示要添加值的索引。此方法返回比被调用元组多一个元素的元组。
语法:
Triplet triplet =
new Triplet(value1, value2, value3);
Quartet quartet =
triplet.addAt3(value);
示例:
Java
// Below is a Java program to add
// a value
import java.util.*;
import org.javatuples.Triplet;
import org.javatuples.Quartet;
class GfG {
public static void main(String[] args)
{
Triplet triplet
= Triplet.with(Integer.valueOf(1),
"GeeksforGeeks",
"A computer portal");
Quartet quartet
= triplet.addAt3("for geeks");
System.out.println(quartet);
}
}
输出:
[1, GeeksforGeeks, A computer portal, for geeks]
在三元组中搜索
可以使用预定义的方法contains()在元组中搜索元素。无论该值是否存在,它都会返回一个布尔值。
语法:
Triplet triplet =
new Triplet(value1, value2, value3);
boolean res = triplet.contains(value2);
示例:
Java
// Below is a Java program to search
// a value in a Triplet
import java.util.*;
import org.javatuples.Triplet;
class GfG {
public static void main(String[] args)
{
Triplet triplet
= Triplet.with(Integer.valueOf(1),
"GeeksforGeeks",
"A computer portal");
boolean exist = triplet.contains("GeeksforGeeks");
boolean exist1 = triplet.contains(4);
System.out.println(exist);
System.out.println(exist1);
}
}
输出:
true
false
遍历 Triplet
由于 Triplet 实现了Iterable接口。这意味着它们可以像集合或数组一样被迭代。
语法:
Triplet triplet =
new Triplet(value1, value2, value3);
for (Object item : triplet) {
...
}
示例:
Java
// Below is a Java program to iterate
// a Triplet
import java.util.*;
import org.javatuples.Triplet;
class GfG {
public static void main(String[] args)
{
Triplet triplet
= Triplet.with(Integer.valueOf(1),
"GeeksforGeeks",
"A computer portal");
for (Object item : triplet)
System.out.println(item);
}
}
输出:
1
GeeksforGeeks
A computer portal