2016-11-04 50 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