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