Java Tuples Decade类

Java Tuples Decade类

DecadeJavaTuples 库中的一个Tuple,它处理3个元素。由于这个Decade是一个通用类,它可以容纳任何类型的值。
由于Decade是一个Tuple,因此它也具有JavaTuples的所有特性:

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

类声明

public final class Decade<A, B, C, D, E, F, G, H, I, J>
extends Tuple
implements IValue0<A>, IValue1<B>, IValue2<C>, IValue3<D>, IValue4<E>, 
           IValue5<F, IValue6<G, IValue7<H>, IValue8<I, J>

Class层次结构

Object
  ↳ org.javatuples.Tuple
      ↳ org.javatuples.Decade<A, B, C, D, E, F, G, H, I, J>

创建十年期元组

  • 来自构造函数 :

    语法 :

Decade<A, B, C, D, E, F, G, H, I, J> decade = 
    new Decade<A, B, C, D, E, F, G, H, I, J>
        (value1, value2, value3, value4, value5, value6, value7, value8, value9, value10);

例如

// Below is a Java program to create
// a Decade tuple from Constructor
 
import java.util.*;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Decade<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                            Integer, Integer, Integer> 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);
    }
}
  • 输出:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  • 使用with()方法 :with()方法是由JavaTuples库提供的一个函数,用于用这些值来实例化对象。
    语法
Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> decade = 
    Decade.with(value1, value2, value3, value4, value5, value6, value7, value8, value9, value10);

例如

// 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<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                                               Integer, Integer> 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);
    }
}
  • 输出:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  • 来自其他集合 :fromCollection()方法用于从一个集合中创建一个Tuple,fromArray()方法用于从一个数组中创建。集合/数组的类型必须与元组的类型相同,并且集合/数组中的值的数量必须与元组类相匹配。
    语法 :
Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> decade = 
    Decade.fromCollection(collectionWith_10_value);

Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> decade = 
    Decade.fromArray(arrayWith_10_value);

例如

// 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<Integer> list = new ArrayList<Integer>();
        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<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                              Integer, Integer, Integer> decade
            = Decade.fromCollection(list);
 
        // Creating Decade from Array
        Integer[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 
        Decade<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                           Integer, Integer, Integer> otherDecade
            = Decade.fromArray(arr);
 
        System.out.println(decade);
        System.out.println(otherDecade);
    }
}
  • 输出:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

获取值

getValueX()方法可以用来获取一个Tuple中索引为X的值。Tuple中的索引从0开始。因此,索引X处的值代表X+1位置的值。
语法

Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> decade = 
    new Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9>
        (value1, value2, value3, value4, value5, value6, value7, value8, value9, value10);

type1 val1 = decade.getValue0();

例如

// 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<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                               Integer, Integer, Integer> 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());
    }
}

输出:

1
3

设置十进制值

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

Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> decade = 
    new Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9>
                (value1, value2, value3, value4, value5, value6, value7, value8, value9, value10);

Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> 
    otherDecade = decade.setAtX(value);

例如

// 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<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                               Integer, Integer, Integer> 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<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                            Integer, Integer, Integer> otherDecade
            = decade.setAt3(40);
 
        System.out.println(otherDecade);
    }
}

输出:

[1, 2, 3, 40, 5, 6, 7, 8, 9, 10]

增加一个值

JavaTuples不支持有超过10个值的Tuple。因此, 没有一个函数 可以在十年中添加一个值。

在十进制中搜索

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

Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> decade = 
    new Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9>
        (value1, value2, value3, value4, value5, value6, value7, value8, value9, value10);

boolean res = decade.contains(value2);

例如

// 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<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                             Integer, Integer, Integer> 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);
    }
}

输出:

true
false

遍历十年

由于Decade实现了 Iterable <Object>接口。这意味着它们可以以与集合或数组相同的方式进行迭代。

语法

Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> decade = 
    new Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9>
            (value1, value2, value3, value4, value5, value6, value7, value8, value9, value10);

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

例如

// 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<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                               Integer, Integer, Integer> 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程