JavaTuples(元组)中的Decade类

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)

Decade类是从元组JavaTuples。由于此Decade是一个泛型类,因此它可以包含任何类型的值。由于Decade是一个元组,因此它也具有JavaTuples的所有特征:

  • Typesafe
  • 永恒的
  • 可迭代
  • Serializable
  • Comparable(实现Comparable <Tuple>)
  • 实现equals()hashCode()
  • 实现toString()

Decade类声明

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>

Decade类层次结构

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

创造Decade元组

  • 来自构造函数语法
    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()方法用于从集合创建元组,而fromArray()方法用于从数组创建。集合/数组必须与Tuple具有相同的类型,并且集合/数组中的值的数量必须与Tuple类匹配。语法
    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]
    

获得Decade的值

getValueX()方法可用于获取索引X处的元组中的值。元组中的索引从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

设置Decade值

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

语法

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]

添加Decade的值

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

在Decade中搜索

可以使用预定义的方法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迭代

由于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
  •   本文标题:JavaTuples(元组)中的Decade类 - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/5433.html
    扫描二维码添加微信 
  • ,领取淘宝优惠券,淘宝购物更优惠。现在添加微信,还可以领取机械键盘优惠券!添加微信后,分享淘宝选中的机械键盘给淘宝机器人即可领取!
    支持我们,就用微信淘宝!

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注

    更多阅读