使用 JavaTuples,用 Java 中的 Unit 类实现 Pair 类
以下是一个在 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]
广告