從 Java 中的 List 集合建立 Decade Tuple


使用 fromCollection() 方法從 Java 中的 List 集合中建立 Decade tuple。讓我們首先了解在使用 JavaTuples 時需要做哪些準備。若要在 JavaTuples 中使用 Decade 類,你需要匯入以下包。

import org.javatuples.Decade;

注意 下載 JavaTuples Jar 庫以執行 JavaTuples 程式。如果使用的是 Eclipse IDE,請右鍵單擊專案 -> 屬性 -> Java 構建路徑 -> 新增外部 Jar,然後上傳下載的 JavaTuples Jar 檔案。請參閱以下指南,瞭解執行 JavaTuples 的所有步驟。

步驟: 如何在 Eclipse 中執行 JavaTuples 程式

以下是一個示例,展示如何從 Java 中的 List 集合中建立 Decade Tuple。

示例

import java.util.List;
import org.javatuples.Decade;
import java.util.*;
public class Demo {
   public static void main(String[] args) {
      // 10 elements in the List
      List<String> myList = new ArrayList<String> ();
      myList.add("Football");
      myList.add("Cricket");
      myList.add("Tennis");
      myList.add("Hockey");
      myList.add("Badminton");
      myList.add("Archery");
      myList.add("Billiards");
      myList.add("Cycling");
      myList.add("Handball");
      myList.add("Squash");
      // creating Decade Tuple from List
      Decade<String, String, String, String, String, String, String, String, String, String>
      d = Decade.fromCollection(myList);
      System.out.println("Elements in the Decade Tuple = ");
      for (Object ele : d) System.out.println(ele);
   }
}

輸出

Elements in the Decade Tuple =
Football
Cricket
Tennis
Hockey
Badminton
Archery
Billiards
Cycling
Handball
Squash

更新時間:30-Jul-2019

80 次瀏覽

職業起步

完成課程獲取認證

立即開始
廣告
© . All rights reserved.