2016-01-03 44 views
-1

我的问题是在removeDuplicates(C)中,为什么它不以这种方式接受它?另外,有人可以解释流如何工作?无法在功能图中将void无效转换为无效

List<String> countries = new ArrayList<String>() { 
    { 
     add("Indiaaaa"); 
     add("Rommania"); 
     add("UUUUK"); 
    } 
}; 
countries.stream() 
    .map(c -> removeDuplicates(c)) 
    .forEach(n -> System.out.print(n + " ")); 
} 

这里是功能removeDuplicates:

public static void removeDuplicates(String s) { 
    char[] chars = s.toCharArray(); 
    Set<Character> result = new LinkedHashSet<>(); 
    for(Character c : chars) { 
     result.add(c); 
    } 
    result.forEach(n -> System.out.print(n)); 
} 
+0

请解释一下你期望的事情以及实际发生的事情。如果你不这样做,谁能帮助你? –

回答

0

removeDuplicates没有返回值。这就是void的含义。因此你不能使用它map

你写它的方式,是最接近于我认为你的意思是

countries.stream().forEach(country -> { 
    removeDuplicates(country); // prints out the letters without duplicates 
           // doesn't actually modify country, 
           // or return the deduplicated characters 
    System.out.print(" "); 
}); 

...虽然你也可以删除.stream(),和只写countries.forEach(...)

相关问题