使用 JavaTuples 在 Java 中实现 Unit 类和 Pair 类

java 8object oriented programmingprogramming更新于 2025/6/26 19:07:17

以下是在 Java 中从 Unit 类实现 Pair 类的示例 −

示例

import org.javatuples.Unit;
import org.javatuples.Pair;
public class MyDemo {
   public static void main(String[] args) {
      Unit<String> unit = Unit.with("Tutorial");
      System.out.println("Unit class element: " + unit);
      Pair<String, String> pair = unit.addAt0("Learning");
      System.out.println("Pair (Implemented from Unit Tuple): " + pair);
   }
}

输出

Unit class element: [Tutorial]
Pair (Implemented from Unit): [Learning, Tutorial]

让我们看另一个示例,其中我们将从 Unit 类实现 Pair 类 −

示例

import org.javatuples.Unit;
import org.javatuples.Pair;
public class MyDemo {
   public static void main(String[] args) {
      Unit<String> unit = Unit.with("Tutorial");
      System.out.println("Unit class element = " + unit);
      Pair<String, String> pair = unit.addAt1("Learning");
      System.out.println("Pair Class (Implemented from Unit Tuple) = " + pair);
   }
}

输出

Unit class element = [Tutorial]
Pair Class (Implemented from Unit Tuple) = [Tutorial, Learning]

相关文章