使用 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]

更新於:2019 年 9 月 20 日

65 次瀏覽

開啟您的 職業

完成本課程後獲得認證

開始行動
廣告
© . All rights reserved.