• Tags
  •         
  • www.breakyizhan.com
  •    

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

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

    Quintet类类声明

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

    Quintet类类层次结构

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

    创建Quintet类元组

    • 来自构造函数语法
      Quintet<A, B, C, D, E> quintet = 
          new Quintet<A, B, C, D, E>
              (value1, value2, value3, value4, value5);

      示例

      // Below is a Java program to create
      // a Quintet tuple from Constructor
      
      import java.util.*;
      import org.javatuples.Quintet;
      
      class GfG {
          public static void main(String[] args)
          {
              Quintet<Integer, String, String, Double, Boolean> quintet
                  = Quintet.with(Integer.valueOf(1),
                                 "breakyizhan",
                                 "A computer portal",
                                 Double.valueOf(20.18),
                                 true);
      
              System.out.println(quintet);
          }
      }
      

      输出:

      [1, breakyizhan, A computer portal, 20.18, true]
    • 使用with()方法:with()方法是JavaTuples库提供的函数,用于使用这些值实例化对象。语法
      Quintet<type1, type2, type3, type4, type5> quintet = 
          Quintet.with(value1, value2, value3, value4, value5);

      示例

      // Below is a Java program to create
      // a Quintet tuple from with() method
      
      import java.util.*;
      import org.javatuples.Quintet;
      
      class GfG {
          public static void main(String[] args)
          {
              Quintet<Integer, String, String, Double, Boolean> quintet
                  = Quintet.with(Integer.valueOf(1),
                                 "breakyizhan",
                                 "A computer portal",
                                 Double.valueOf(20.18),
                                 true);
      
              System.out.println(quintet);
          }
      }
      

      输出:

      [1, breakyizhan, A computer portal, 20.18, true]
    • 来自其他集合:fromCollection()方法用于从集合创建元组,而fromArray()方法用于从数组创建。集合/数组必须与Tuple具有相同的类型,并且集合/数组中的值的数量必须与Tuple类匹配。语法
      Quintet<type1, type2, type3, type4, type5> quintet = 
          Quintet.fromCollection(collectionWith_5_value);
      
      Quintet<type1, type2, type3, type4, type5> quintet = 
          Quintet.fromArray(arrayWith_5_value);

      示例

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

      输出:

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

    获得Quintet类的值

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

    语法

    Quintet<type1, type2, type3, type4, type5> quintet = 
        new Quintet<type1, type2, type3, type4, type5>
                       (value1, value2, value3, value4, value5);
    
    type1 val1 = quintet.getValue0();

    示例

    // Below is a Java program to get
    // a Quintet value
    
    import java.util.*;
    import org.javatuples.Quintet;
    
    class GfG {
        public static void main(String[] args)
        {
            Quintet<Integer, String, String, Double, Boolean> quintet
                = Quintet.with(Integer.valueOf(1),
                               "breakyizhan",
                               "A computer portal",
                               Double.valueOf(20.18),
                               true);
    
            System.out.println(quintet.getValue0());
            System.out.println(quintet.getValue2());
        }
    }
    

    输出:

    1
    A computer portal

    设置Quintet类的值

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

    语法

    Quintet<type1, type2, type3, type4, type5> quintet = 
        new Quintet<type1, type2, type3, type4, type5>
                      (value1, value2, value3, value4, value5);
    
    Quintet<type1, type2, type3, type4, type5> 
        otherQuintet = quintet.setAtX(value);

    示例

    // Below is a Java program to set
    // a Quintet value
    
    import java.util.*;
    import org.javatuples.Quintet;
    
    class GfG {
        public static void main(String[] args)
        {
            Quintet<Integer, String, String, Double, Boolean> quintet
                = Quintet.with(Integer.valueOf(1),
                               "breakyizhan",
                               "A computer portal",
                               Double.valueOf(20.18),
                               true);
    
            Quintet<Integer, String, String, Double> otherQuintet
                = quintet.setAt3(2.018);
    
            System.out.println(otherQuintet);
        }
    }
    

    输出:

    [1, breakyizhan, A computer portal, 2.018, true]

    添加Quintet类的值

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

    语法

    Quintet<type1, type2, type3, type4, type5> quintet = 
        new Quintet<type1, type2, type3, type4, type5>
                       (value1, value2, value3, value4, value5);
    
    Quintet<type 1, type 2, type 3, type 4, type 5> quintet = 
        quintet.addAtx(value);

    示例

    // Below is a Java program to add
    // a value
    
    import java.util.*;
    import org.javatuples.Quintet;
    import org.javatuples.Sextet;
    
    class GfG {
        public static void main(String[] args)
        {
            Quintet<Integer, String, String, Double, Boolean> quintet
                = Quintet.with(Integer.valueOf(1),
                               "breakyizhan",
                               "A computer portal",
                               Double.valueOf(20.18),
                               true);
    
            Quintet<Integer, String, String, Double, Boolean, Boolean> sextet
                = quintet.addAt5(false);
    
            System.out.println(sextet);
        }
    }
    

    输出:

    [1, breakyizhan, A computer portal, for geeks, 20.18, true, false]

    在Quintet类中搜索

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

    语法

    Quintet<type1, type2, type3, type4, type5> quintet = 
        new Quintet<type1, type2, type3, type4, type5>
                       (value1, value2, value3, value4, value5);
    
    boolean res = quintet.contains(value2);

    示例

    // Below is a Java program to search
    // a value in a Quintet
    
    import java.util.*;
    import org.javatuples.Quintet;
    
    class GfG {
        public static void main(String[] args)
        {
            Quintet<Integer, String, String, Double, Boolean> quintet
                = Quintet.with(Integer.valueOf(1),
                               "breakyizhan",
                               "A computer portal",
                               Double.valueOf(20.18),
                               true);
    
            boolean exist = quintet.contains(20.18);
            boolean exist1 = quintet.contains(false);
    
            System.out.println(exist);
            System.out.println(exist1);
        }
    }
    

    输出:

    true
    false

    Quintet类的迭代

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

    语法

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

    示例

    // Below is a Java program to iterate
    // a Quintet
    
    import java.util.*;
    import org.javatuples.Quintet;
    
    class GfG {
        public static void main(String[] args)
        {
            Quintet<Integer, String, String, Double, Boolean> quintet
                = Quintet.with(Integer.valueOf(1),
                               "breakyizhan",
                               "A computer portal",
                               Double.valueOf(20.18),
                               true);
    
            for (Object item : quintet)
                System.out.println(item);
        }
    }
    

    输出:

    1
    breakyizhan
    A computer portal
    20.18
    true

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