📜  JavaTuples 中的十年类

📅  最后修改于: 2022-05-13 01:55:32.868000             🧑  作者: Mango

JavaTuples 中的十年类

DecadeJavaTuples库中处理 3 个元素的元组。由于这个 Decade 是一个泛型类,它可以在其中包含任何类型的值。
由于 Decade 是一个元组,因此它也具有 JavaTuples 的所有特性:

  • 它们是类型安全
  • 它们是不可变
  • 它们是可迭代
  • 它们是可序列化
  • 它们是可比较的(实现 Comparable
  • 他们实现了 equals()hashCode()
  • 他们还实现了 toString()

类声明

public final class Decade
extends Tuple
implements IValue0, IValue1, IValue2, IValue3, IValue4, 
           IValue5, IValue8

类层次结构

创建十年元组

Java


Java
// Below is a Java program to create
// a Decade tuple from with() method
 
import java.util.*;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Decade decade
            = Decade.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9),
                          Integer.valueOf(10));
 
        System.out.println(decade);
    }
}


Java
// Below is a Java program to create
// a Decade tuple from Collection
 
import java.util.*;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        // Creating Decade from List
        List list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
        list.add(10);
 
        Decade decade
            = Decade.fromCollection(list);
 
        // Creating Decade from Array
        Integer[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 
        Decade otherDecade
            = Decade.fromArray(arr);
 
        System.out.println(decade);
        System.out.println(otherDecade);
    }
}


Java
// Below is a Java program to get
// a Decade value
 
import java.util.*;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Decade decade
            = Decade.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9),
                          Integer.valueOf(10));
 
        System.out.println(decade.getValue0());
        System.out.println(decade.getValue2());
    }
}


Java
// Below is a Java program to set
// a Decade value
 
import java.util.*;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Decade decade
            = Decade.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9),
                          Integer.valueOf(10));
 
        Decade otherDecade
            = decade.setAt3(40);
 
        System.out.println(otherDecade);
    }
}


Java
// Below is a Java program to search
// a value in a Decade
 
import java.util.*;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Decade decade
            = Decade.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9),
                          Integer.valueOf(10));
 
        boolean exist = decade.contains(5);
        boolean exist1 = decade.contains(false);
 
        System.out.println(exist);
        System.out.println(exist1);
    }
}


Java
// Below is a Java program to iterate
// a Decade
 
import java.util.*;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Decade decade
            = Decade.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9),
                          Integer.valueOf(10));
 
        for (Object item : decade)
            System.out.println(item);
    }
}


获得价值

getValueX() 方法可用于获取元组中索引 X 处的值。元组中的索引从 0 开始。因此,索引 X 处的值表示位置 X+1 处的值。
语法

示例

输出:

设置十年值

由于元组是不可变的,这意味着不可能修改任何索引处的值。
因此,JavaTuples 提供了 setAtX(value) ,它在索引 X 处创建具有新值的元组副本,并返回该元组。
语法

示例

输出:

添加一个值

JavaTuples 不支持超过 10 个值的元组。因此,在 Decade 中没有添加值的函数

寻找十年

可以使用预定义的方法contains()在元组中搜索元素。无论该值是否存在,它都会返回一个布尔值。
语法

示例

输出:

迭代十年

由于 Decade 实现了Iterable接口。这意味着它们可以像集合或数组一样被迭代。
语法

Decade decade = 
    new Decade
            (value1, value2, value3, value4, value5, value6, value7, value8, value9, value10);

for (Object item : decade) {
        ...
}

示例

Java

// Below is a Java program to iterate
// a Decade
 
import java.util.*;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Decade decade
            = Decade.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9),
                          Integer.valueOf(10));
 
        for (Object item : decade)
            System.out.println(item);
    }
}

输出:

1
2
3
4
5
6
7
8
9
10