2016-12-05 20 views
-1

有幾個選項,但我不確定哪個是標準。將範圍<T>轉換爲列表<T>的規範方法是什麼?

  • 手動從低到迭代上限
  • ContiguousSet
  • 別的東西嗎?
+0

@PatrickParker它在谷歌收藏庫 – Andrew

+1

只要你在番石榴世界,並且你的範圍有一個定義的域,那麼ContiguousSet似乎就像要走的路......不知道你的意思是「規範「 –

回答

3

這不是那麼簡單,但當然可以。只要創建ContiguousSet<T>,這是一個ImmutableSortedSet和使用方法asList(),例如:

Range<Integer> range = Range.closed(1, 5); 
ContiguousSet<Integer> ourIntegers = ContiguousSet.create(range, DiscreteDomain.integers()); 
ImmutableList<Integer> ourIntegersList = ourIntegers.asList(); 
System.out.println(ourIntegers); // [1‥5] 
System.out.println(ourIntegersList); // [1, 2, 3, 4, 5] 

請注意,您可能需要使用ContiguousSet堅持(與使用列表視圖),因爲 前者不actualy存儲每個元素在記憶中,而後者則是這樣,這可能是一個大範圍的問題。

相關問題