📜  使用 JavaTuples 在Java中实现 Pair Class with Unit Class

📅  最后修改于: 2022-05-13 01:55:22.129000             🧑  作者: Mango

使用 JavaTuples 在Java中实现 Pair Class with Unit Class

A PairJavaTuples库中处理 2 个元素的元组。由于这个 Pair 是一个泛型类,它可以在其中保存任何类型的值。 UnitJavaTuples库中的一个 Tuple,它只处理 1 个元素。由于这个 Unit 是一个泛型类,它可以在其中保存任何类型的值。

用单元类实现对类的不同方法

  • 使用直接值
  • 使用 Unit 类的 add() 方法 
  • 使用 Unit 类的 addAtX() 方法

方法 1:使用直接值

例子:

Java
// Java Program to Implement Pair 
// Class with Unit Class
// Using JavaTuples Via Direct Values
  
// Importing required classes
import java.util.*;
import org.javatuples.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating a Unit
        Unit unit
            = new Unit("GeeksforGeeks");
  
        // Printing unit
        System.out.println("Unit: " + unit);
  
        // Creating Pair from Unit
        Pair pair
            = new Pair(Integer.valueOf(1),
                                        unit.getValue0());
  
        // Printing the Pair
        System.out.println("Pair: " + pair);
    }
}


Java
// Java Program to Implement Pair Class with Unit Class
// use of add() method with Single Value
  
// Importing required classes
import java.util.*;
import org.javatuples.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Instantiate unit object
        // using with() method
        Unit unit = Unit.with("Geeks");
  
        // Printing unit
        System.out.println("Unit: " + unit);
  
        // Create Pair Using add() method
        Pair pair = unit.add("forGeeks");
  
        // Print the Pair
        System.out.println("Pair: " + pair);
    }
}


Java
// Java program to Implement Pair Class with Unit Class
// By using of addAt0() method with Direct Value
  
// Importing required classes
import java.util.*;
import org.javatuples.Pair;
import org.javatuples.Unit;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Instantiate unit object
        // using with() method
        Unit unit = Unit.with("Geeks");
  
        // Printing unit
        System.out.println("Unit: " + unit);
  
        // Creating Pair using addAtX() method
        Pair pair = unit.addAt0("forGeeks");
  
        // Printing the Pair
        System.out.println("Pair: " + pair);
    }
}


Java
// Java program to Implement 
// Pair Class with Unit Class
// Via addAt1() method with 
// Direct Value
  
// Importing required classes
import java.util.*;
import org.javatuples.Pair;
import org.javatuples.Unit;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Instantiate unit object
        // using with() method
        Unit unit = Unit.with("Geeks");
  
        // Printing Unit
        System.out.println("Unit: " + unit);
  
        // Creating Pair
        // Using addAtX() method
        Pair pair = unit.addAt1("forGeeks");
  
        // Printing the Pair
        System.out.println("Pair: " + pair);
    }
}


输出:

Unit: [GeeksforGeeks]
Pair: [1, GeeksforGeeks]

方法二:使用Unit类的add()方法

例子:

Java

// Java Program to Implement Pair Class with Unit Class
// use of add() method with Single Value
  
// Importing required classes
import java.util.*;
import org.javatuples.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Instantiate unit object
        // using with() method
        Unit unit = Unit.with("Geeks");
  
        // Printing unit
        System.out.println("Unit: " + unit);
  
        // Create Pair Using add() method
        Pair pair = unit.add("forGeeks");
  
        // Print the Pair
        System.out.println("Pair: " + pair);
    }
}

输出:

Unit: [Geeks]
Pair: [Geeks, forGeeks]

方法3:使用Unit类的addAtX()方法

示例 1:使用 addAt0() 在位置 0 添加单位

Java

// Java program to Implement Pair Class with Unit Class
// By using of addAt0() method with Direct Value
  
// Importing required classes
import java.util.*;
import org.javatuples.Pair;
import org.javatuples.Unit;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Instantiate unit object
        // using with() method
        Unit unit = Unit.with("Geeks");
  
        // Printing unit
        System.out.println("Unit: " + unit);
  
        // Creating Pair using addAtX() method
        Pair pair = unit.addAt0("forGeeks");
  
        // Printing the Pair
        System.out.println("Pair: " + pair);
    }
}

输出:

Unit: [Geeks]
Pair: [forGeeks, Geeks]

示例 2:使用 addAt1() 在位置 1 添加单位

Java

// Java program to Implement 
// Pair Class with Unit Class
// Via addAt1() method with 
// Direct Value
  
// Importing required classes
import java.util.*;
import org.javatuples.Pair;
import org.javatuples.Unit;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Instantiate unit object
        // using with() method
        Unit unit = Unit.with("Geeks");
  
        // Printing Unit
        System.out.println("Unit: " + unit);
  
        // Creating Pair
        // Using addAtX() method
        Pair pair = unit.addAt1("forGeeks");
  
        // Printing the Pair
        System.out.println("Pair: " + pair);
    }
}

输出:

Unit: [Geeks]
Pair: [Geeks, forGeeks]