JavaTuples(元组)中的Ennead 类

作者: Arvin Chen 分类: Java 发布时间: 2018-08-27 20:43

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

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

Ennead 类声明

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

Ennead 类层次结构

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

创造Ennead元组

  • 来自构造函数语法
    Ennead<A, B, C, D, E, F, G, H, I> ennead = 
        new Ennead<A, B, C, D, E, F, G, H, I>
            (value1, value2, value3, value4, value5, value6, value7, value8, value9);

    示例

    // Below is a Java program to create
    // a Ennead tuple from Constructor
    
    import java.util.*;
    import org.javatuples.Ennead;
    
    class GfG {
        public static void main(String[] args)
        {
            Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
                = Ennead.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));
    
            System.out.println(ennead);
        }
    }
    

    输出:

    [1,2,3,4,5,6,7,8,9]
    
  • 使用with()方法:with()方法是JavaTuples库提供的函数,用于使用这些值实例化对象。语法
    Ennead <type1,type2,type3,type4,type5,type6,type7,type8> ennead = 
        Ennead.with(value1,value2,value3,value4,value5,value6,value7,value8,value9);
    

    示例

    // Below is a Java program to create
    // a Ennead tuple from with() method
    
    import java.util.*;
    import org.javatuples.Ennead;
    
    class GfG {
        public static void main(String[] args)
        {
            Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
                = Ennead.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));
    
            System.out.println(ennead);
        }
    }
    

    输出:

    [1,2,3,4,5,6,7,8,9]
    
  • 来自其他集合:fromCollection()方法用于从集合创建元组,而fromArray()方法用于从数组创建。集合/数组必须与Tuple具有相同的类型,并且集合/数组中的值的数量必须与Tuple类匹配。语法
    Ennead <type1,type2,type3,type4,type5,type6,type7,type8> ennead = 
        Ennead.fromCollection(collectionWith_9_value);
    
    Ennead <type1,type2,type3,type4,type5,type6,type7,type8> ennead = 
        Ennead.fromArray(arrayWith_9_value);
    

    示例

    // Below is a Java program to create
    // a Ennead tuple from Collection
    
    import java.util.*;
    import org.javatuples.Ennead;
    
    class GfG {
        public static void main(String[] args)
        {
            // Creating Ennead 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);
    
            Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
                = Ennead.fromCollection(list);
    
            // Creating Ennead from Array
            Integer[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
            Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> otherEnnead
                = Ennead.fromArray(arr);
    
            System.out.println(ennead);
            System.out.println(otherEnnead);
        }
    }
    

    输出:

    [1,2,3,4,5,6,7,8,9]
    [1,2,3,4,5,6,7,8,9]
    

获得Ennead的值

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

语法

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

type1 val1 = ennead.getValue0();

示例

// Below is a Java program to get
// a Ennead value

import java.util.*;
import org.javatuples.Ennead;

class GfG {
    public static void main(String[] args)
    {
        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
            = Ennead.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));

        System.out.println(ennead.getValue0());
        System.out.println(ennead.getValue2());
    }
}

输出:

1
3

设置Ennead值

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

句法:

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

Ennead <type1,type2,type3,type4,type5,type6,type7,type8> 
    otherEnnead = ennead.setAtX(value);

例:

// Below is a Java program to set
// a Ennead value

import java.util.*;
import org.javatuples.Ennead;

class GfG {
    public static void main(String[] args)
    {
        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
            = Ennead.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));

        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> otherEnnead
            = ennead.setAt3(40);

        System.out.println(otherEnnead);
    }
}

输出:

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

添加Ennead的值

可以在addAtX()方法的帮助下添加值,其中X表示要添加值的索引。此方法返回元素的元组,比被调用的元组多一个。

语法

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

Ennead<type 1, type 2, type 3, type 4, type 5, type 6, type 7, type 8> ennead = 
    ennead.addAtx(value);

示例

// Below is a Java program to add
// a value

import java.util.*;
import org.javatuples.Ennead;
import org.javatuples.Decade;

class GfG {
    public static void main(String[] args)
    {
        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
            = Ennead.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));

        Decade<Integer, Integer, Integer, Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> ennead
            = ennead.addAt9(10);

        System.out.println(ennead);
    }
}

输出:

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

在Ennead中搜索

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

语法

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

boolean res = ennead.contains(value2);

示例

// Below is a Java program to search
// a value in a Ennead

import java.util.*;
import org.javatuples.Ennead;

class GfG {
    public static void main(String[] args)
    {
        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
            = Ennead.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));

        boolean exist = ennead.contains(5);
        boolean exist1 = ennead.contains(false);

        System.out.println(exist);
        System.out.println(exist1);
    }
}

输出:

true
false

通过Ennead迭代

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

语法

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

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

示例

// Below is a Java program to iterate
// a Ennead

import java.util.*;
import org.javatuples.Ennead;

class GfG {
    public static void main(String[] args)
    {
        Ennead<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer> ennead
            = Ennead.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));

        for (Object item : ennead)
            System.out.println(item);
    }
}

输出:

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

    发表笔记

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

    更多阅读