2016-11-04 53 views
-2
public Set<String> filterAlleles (int threshold) { 
    Set<String> filtered = new HashSet<String>(); 
    Map<String, Integer> counted = this.countAlleles(); 
    for (String allele : _alleles){ 

我以前寫過countAlleles方法,所以我在這個方法聲明中按照指示使用它。 countAlleles方法返回等位基因和它發生的次數。使用foreach循環打印> =閾值的字符串?

+0

這還應該使用編程語言名稱進行標記。 – Bobby

+0

遍歷'counting'映射並刪除一個值小於閾值的條目(使用'#entrySet'上的'Iterator'),然後打印地圖 – Rogue

回答

0

以下是使用for循環的示例代碼,該代碼將過濾> = 7的數據並打印它們。既然你提供了很少的代碼,我會附上希望能幫助你的例子。

public static void main(String[] args) { 
    Map<String,Integer> counted = new HashMap<>(); 
    counted.put("foo", 3); 
    counted.put("bar", 10); 
    counted.put("baz", 6); 
    counted.put("goo", 11); 

    counted.entrySet().stream()    // Stream the entry set 
      .filter(e->e.getValue() >= 7)  // Filter on >= threshold 
      .map(Map.Entry::getKey)   // Get the key since it's the name we are looking for 
      .forEach(System.out::println);  // Print the list 
} 
+0

將它收集到列表中有什麼意義?在這種情況下,它就像一個無操作。 – Tom

+0

如果他們想要進一步操作字符串,但否則收集可以被排除在外。這是一個例子,因爲問題提供的信息很少。 – AlexC