簡體   English   中英

轉換范圍的規范方法是什么 <T> 進入列表 <T> ?

[英]What is the canonical way to transform a Range<T> into a List<T>?

有幾種選擇,但是我不確定哪個是標准的。

  • 從下到上手動進行迭代
  • 連續集
  • 還有嗎

這不是那么簡單,但是當然可以。 只需創建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 (與使用列表視圖相比),因為前者實際上並沒有將每個元素存儲在內存中,而后者確實存在,這可能是大范圍的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM