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