2017-08-28 37 views
0

我想簡單地從ArrayList中選擇元素,就像我可以在R中做的那樣。例如,在RI中可以選擇索引列包含的值大於1500的行,導致一個新的數據幀:R數據幀類型的行選擇,但對於Java 8 ArrayList

ndf = df[df$index>1500,] 

在Java 8中,我想做一個ArrayList的等價物。我能想出的最簡潔的方式是:

List<IndexCount> signficantRowIndexList = new ArrayList<>(); 
<the list gets loaded with some objects> 

List<IndexCount> selectedList = new ArrayList<>(); 

signficantRowIndexList.stream() 
     .filter((ic)->ic.index > 1500) 
     .forEach((ic)->selectedList.add(ic)); 

這是最簡潔的方法嗎?

回答

0

您應該將流收集到List

List<IndexCount> selectedList = signficantRowIndexList.stream() 
     .filter(ic-> ic.index > 1500) 
     .collect(Collectors.toList()); 

作爲一個側面說明,括號不需要如果你只有一個參數爲lambda參數:
使用collect()只要你想從當前流創建一個新的List似乎在你的情況更自然。