• Tags         
  • 2018-08-20  21:51:43        
  • 166 °C    

    Triplet是从元组JavaTuples库的。由于这个Triplet是一个泛型类,它可以包含任何类型的值。

    由于Triplet是一个元组,因此它也具有JavaTuples的所有特征:

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

    Java Triplet类声明

    public final class Triplet<A, B, C> extends Tuple
        implements IValue0<A>, IValue1<B>, IValue2<C>

    Java Triplet类层次结构

    Object
      ↳ org.javatuples.Tuple
          ↳ org.javatuples.Triplet<A, B, C>

    创建Java Triplet元组

    • 从构造函数
      语法

      Triplet<A, B, C> triplet = 
          new Triplet<A, B, C>(value1, value2, value3);

      示例

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

      输出:

      [1,breakyizhan,A computer portal]
      
    • 使用with()方法:with()方法是JavaTuples库提供的函数,用于使用这些值实例化对象。
      语法

      Triplet<type1, type2, type3> triplet = 
             Triplet.with(value1, value2, value3);

      示例

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

      输出:

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

      示例

      // Below is a Java program to create
      // a Triplet tuple from Collection
      
      import java.util.*;
      import org.javatuples.Triplet;
      
      class GfG {
          public static void main(String[] args)
          {
              // Creating Triplet from List
              List<String> list = new ArrayList<String>();
              list.add("GeeksforGeeks");
              list.add("A computer portal");
              list.add("for geeks");
      
              Triplet<Strin, String, String> triplet
                  = Triplet.fromCollection(list);
      
              // Creating Triplet from Array
              String[] arr = { "GeeksforGeeks",
                               "A computer portal",
                               "for geeks" };
      
              Triplet<String, String, String> otherTriplet
                  = Triplet.fromArray(arr);
      
              System.out.println(triplet);
              System.out.println(otherTriplet);
          }
      }
      

      输出:

      [GeeksforGeeks, A computer portal, for geeks]
      [GeeksforGeeks, A computer portal, for geeks]

    Java Triplet获得value

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

    语法

    Triplet<type1, type2, type3> triplet = 
        new Triplet<type1, type2, type3>(value1, value2, value3);
    
    type1 val1 = triplet.getValue0();

    示例

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

    输出:

    1
    A computer portal

    Java 设置Triplet类的值

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

    语法

    Triplet<type1, type2, type3> triplet = 
        new Triplet<type1, type2, type3>
                    (value1, value2, value3);
    
    Triplet<type1, type2, type3> 
        otherTriplet = triplet.setAtX(value);

    示例

    // Below is a Java program to set
    // a Triplet value
     
    import java.util.*;
    import org.javatuples.Triplet;
     
    class GfG {
        public static void main(String[] args)
        {
            Triplet<Integer, String, String> triplet
                = Triplet.with(Integer.valueOf(1), "GeeksforGeeks",
                                              "A computer portal");
     
            Triplet<Integer, String, String> otherTriplet
                = triplet.setAt1("A computer portal for geeks");
     
            System.out.println(otherTriplet);
        }
    }
    

    输出:

    [1,GeeksforGeeks,A computer portal for geeks]
    

    Java 添加Triplet类的值

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

    语法

    Triplet<type1, type2, type3> triplet = 
        new Triplet<type1, type2, type3>(value1, value2, value3);
    
    Quartet<type 1, type 2, type 3, type 4> quartet = 
        triplet.addAt3(value);

    示例

    // Below is a Java program to add
    // a value
    
    import java.util.*;
    import org.javatuples.Triplet;
    import org.javatuples.Quartet;
    
    class GfG {
        public static void main(String[] args)
        {
            Triplet<Integer, String, String> triplet
                = Triplet.with(Integer.valueOf(1),
                               "GeeksforGeeks",
                               "A computer portal");
    
            Quartet<Integer, String, String, String> quartet
                = triplet.addAt3("for geeks");
    
            System.out.println(quartet);
        }
    }
    

    输出:

    [1, GeeksforGeeks, A computer portal, for geeks]

    Java 在Triplet中搜索

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

    语法

    Triplet<type1, type2, type3> triplet = 
        new Triplet<type1, type2, type3>(value1, value2, value3);
    
    boolean res = triplet.contains(value2);

    示例

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

    输出:

    true
    false

    Java 通过Triplet迭代

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

    语法

    Triplet <type1,type2,type3> triplet = 
        new Triplet <type1,type2,type3>(value1,value2,value3);
    
    for(Object item:triplet){
            ...
    }
    

    示例

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

    输出:

    1
    GeeksforGeeks
    A computer portal

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