A. 单元 是来自 Java元组 只处理一个元素的库。因为这个单元是一个泛型类,所以它可以包含任何类型的值。 由于Unit是一个元组,因此它也具有JavaTuple的所有特征:
null
- 他们是 类型安全
- 他们是 不变的
- 他们是 可迭代的
- 他们是 可序列化
- 他们是 可比较的 (实现可比较的
) - 他们实施 等于 和 hashCode()
- 他们还实施 toString()
类别声明
public final class Unit<A> extends Tuple implements IValue0<A>
类层次结构
Object ↳ org.javatuples.Tuple ↳ org.javatuples.Unit<A>
创建单位元组
- 来自构造函数 : 语法 :
Unit<A> unit = new Unit<A>(value);
- 实例 :
JAVA
// Below is a Java program to create // a Unit tuple from Constructor import java.util.*; import org.javatuples.Unit; class GfG { public static void main(String[] args) { Unit<String> unit = new Unit<String>( "GeeksforGeeks" ); System.out.println(unit); } } |
- 输出:
[GeeksforGeeks]
- 使用with()方法 :with()方法是JavaTuples库提供的一个函数,用于用这些值实例化对象。 语法 :
Unit<type 1> unit = Unit.with(value);
- 实例 :
JAVA
// Below is a Java program to create // a Unit tuple from with() method import java.util.*; import org.javatuples.Unit; class GfG { public static void main(String[] args) { Unit<String> unit = Unit.with( "GeeksforGeeks" ); System.out.println(unit); } } |
- 输出:
[GeeksforGeeks]
- 来自其他系列 :fromCollection()方法用于从集合创建元组,fromArray()方法用于从数组创建元组。集合/数组的类型必须与元组的类型相同,集合/数组中的值数量必须与元组类匹配。 语法 :
Unit<type> unit = Unit.fromCollection(collectionWith_1_value);Unit<type> unit = Unit.fromArray(arrayWith_1_value);
- 实例 :
JAVA
// Below is a Java program to create // a Unit tuple from Collection import java.util.*; import org.javatuples.Unit; class GfG { public static void main(String[] args) { // Creating Unit from List List<String> list = new ArrayList<String>(); list.add( "GeeksforGeeks" ); Unit<String> unit = Unit.fromCollection(list); // Creating Unit from Array String[] arr = { "A computer portal" }; Unit<String> otherUnit = Unit.fromArray(arr); System.out.println(unit); System.out.println(otherUnit); } } |
- 输出:
[GeeksforGeeks][A computer portal]
获得价值
getValueX()方法可用于在索引X处获取元组中的值。元组中的索引从0开始。因此,索引X处的值代表位置X+1处的值。 语法 :
Unit<type 1> unit = new Unit<type 1>(value);type1 val1 = unit.getValue0();
例子:
JAVA
// Below is a Java program to get // a Unit value import java.util.*; import org.javatuples.Unit; class GfG { public static void main(String[] args) { Unit<String> unit = Unit.with( "GeeksforGeeks" ); System.out.println(unit); } } |
输出:
GeeksforGeeks
设置单位值
因为元组是 不变的 ,这意味着不可能修改索引中的值。因此JavaTuples提供了 setAtX(值) 它在索引X处创建一个具有新值的元组副本,并返回该元组。 语法 :
Unit<type1> unit = new Unit<type1>(value);type1 val1 = unit.setAt0();
实例 :
JAVA
// Below is a Java program to set // a Unit value import java.util.*; import org.javatuples.Unit; class GfG { public static void main(String[] args) { Unit<String> unit = Unit.with( "GeeksforGeeks" ); Unit<String> otherUnit = unit.setAt0( "A computer portal" ); System.out.println(otherUnit); } } |
输出:
[A computer portal]
增加价值
可以使用addAtX()方法添加值,其中X表示要添加值的索引。这个方法返回一个比被调用元组多1个元素的元组。 语法 :
Unit<type1> unit = new Unit<type 1>(value);Pair<type1, type2> pair = unit.addAt1(value2);
实例 :
JAVA
// Below is a Java program to add // a value import java.util.*; import org.javatuples.Unit; import org.javatuples.Pair; class GfG { public static void main(String[] args) { Unit<String> unit = Unit.with( "GeeksforGeeks" ); Pair<String, String> pair = unit.addAt1( "A computer portal" ); System.out.println(pair); } } |
输出:
[GeeksforGeeks, A computer portal]
在元组中搜索
可以使用预定义的方法contains()在元组中搜索元素。它返回一个布尔值,无论该值是否存在。 语法 :
Unit<type1> unit = new Unit<type 1>(value);boolean res = unit.contains(value);
实例 :
JAVA
// Below is a Java program to search a value import java.util.*; import org.javatuples.Unit; class GfG { public static void main(String[] args) { Unit<String> unit = Unit.with( "GeeksforGeeks" ); boolean exist = unit.contains( "GeeksforGeeks" ); boolean exist1 = unit.contains( 4 ); System.out.println(exist); System.out.println(exist1); } } |
输出:
truefalse
遍历单元
因为单元实现了Iterable
Unit<type 1> unit = new Unit<type 1>(value);for (Object item : unit) { ...}
实例 :
JAVA
// Below is a Java program to get // a Unit value import java.util.*; import org.javatuples.Unit; class GfG { public static void main(String[] args) { Unit<String> unit = Unit.with( "GeeksforGeeks" ); for (Object item : unit) System.out.println(item); } } |
输出:
GeeksforGeeks
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END