• Tags
  •         
  • www.breakyizhan.com
  •    

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

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

    Sextet类声明

    public final class Sextet<A, B, C, D, E, F> extends Tuple
    implements IValue0<A>, IValue1<B>, IValue2<C>, IValue3<D>, IValue4<E>, IValue5<F>

    Sextet类层次结构

    Object
      ↳ org.javatuples.Tuple
          ↳ org.javatuples.Sextet<A, B, C, D, E, F>

    创建Sextet元组

    • 来自构造函数语法
      Sextet<A, B, C, D, E, F> sextet = 
          new Sextet<A, B, C, D, E. F>
              (value1, value2, value3, value4, value5, value6);

      示例

      // Below is a Java program to create
      // a Sextet tuple from Constructor
      
      import java.util.*;
      import org.javatuples.Sextet;
      
      class GfG {
          public static void main(String[] args)
          {
              Sextet<Integer, Integer.Integer, Integer, Integer, Integer> sextet
                  = Sextet.with(Integer.valueOf(1),
                                Integer.valueOf(2),
                                Integer.valueOf(3),
                                Integer.valueOf(4),
                                Integer.valueOf(5),
                                Integer.valueOf(6));
      
              System.out.println(sextet);
          }
      }
      

      输出:

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

      示例

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

      输出:

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

      示例

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

      输出:

      [1,2,3,4,5,6]
      [1,2,3,4,5,6]
      

    获得Sextet的值

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

    语法

    Sextet<type1, type2, type3, type4, type5, type6> sextet = 
        new Sextet<type1, type2, type3, type4, type5, type6>
                   (value1, value2, value3, value4, value5, value6);
    
    type1 val1 = sextet.getValue0();

    示例

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

    输出:

    1
    3
    

    设置Sextet的值

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

    语法

    Sextet<type1, type2, type3, type4, type5, type6> sextet = 
        new Sextet<type1, type2, type3, type4, type5, type6>
                    (value1, value2, value3, value4, value5, value6);
    
    Sextet<type1, type2, type3, type4, type5, type6> 
        otherSextet = sextet.setAtX(value);

    示例

    // Below is a Java program to set
    // a Sextet value
    
    import java.util.*;
    import org.javatuples.Sextet;
    
    class GfG {
        public static void main(String[] args)
        {
            Sextet<Integer, Integer.Integer, Integer, Integer, Integer> sextet
                = Sextet.with(Integer.valueOf(1),
                              Integer.valueOf(2),
                              Integer.valueOf(3),
                              Integer.valueOf(4),
                              Integer.valueOf(5),
                              Integer.valueOf(6));
    
            Sextet<Integer, Integer.Integer, Integer, Integer, Integer> otherSextet
                = sextet.setAt3(40);
    
            System.out.println(otherSextet);
        }
    }
    

    输出:

    [1,2,3,40,5,6]
    

    添加Sextet的值

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

    语法

    Sextet<type1, type2, type3, type4, type5, type6> sextet = 
        new Sextet<type1, type2, type3, type4, type5, type6>
            (value1, value2, value3, value4, value5, value6);
    
    Sextet<type 1, type 2, type 3, type 4, type 5, type 6> sextet = 
        sextet.addAtx(value);

    示例

    // Below is a Java program to add
    // a value
    
    import java.util.*;
    import org.javatuples.Sextet;
    import org.javatuples.Septet;
    
    class GfG {
        public static void main(String[] args)
        {
            Sextet<Integer, Integer.Integer, Integer, Integer, Integer> sextet
                = Sextet.with(Integer.valueOf(1),
                              Integer.valueOf(2),
                              Integer.valueOf(3),
                              Integer.valueOf(4),
                              Integer.valueOf(5),
                              Integer.valueOf(6));
    
            Septet<Integer, Integer.Integer, Integer, Integer, Integer> septet
                = sextet.addAt6(7);
    
            System.out.println(septet);
        }
    }
    

    输出:

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

    在Sextet中搜索

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

    语法

    Sextet<type1, type2, type3, type4, type5, type6> sextet = 
        new Sextet<type1, type2, type3, type4, type5, type6>
            (value1, value2, value3, value4, value5, value6);
    
    boolean res = sextet.contains(value2);

    示例

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

    输出:

    true
    false
    

    通过Sextet迭代

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

    语法

    Sextet<type1, type2, type3, type4, type5, type6> sextet = 
        new Sextet<type1, type2, type3, type4, type5, type6>
                (value1, value2, value3, value4, value5, value6);
    
    for (Object item : sextet) {
            ...
    }

    示例

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

    输出:

    1
    2
    3
    4
    5
    6

     
    转载请保留页面地址:https://www.breakyizhan.com/java/5428.html