在Java中使用JavaTuples实现Pair 类和Unit 类
以下是在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); Pair<String, String> pair = unit.addAt0("Learning"); System.out.println("Pair (Implemented from Unit Tuple): " + pair); } }
输出结果
单元类元素: [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]