java set foreach
List<String> items = new ArrayList<>();
items.add("A");
items.add("B");
items.add("C");
items.add("D");
items.add("E");
//lambda
//Output : A,B,C,D,E
items.forEach(item->System.out.println(item));
//Output : C
items.forEach(item->{
if("C".equals(item)){
System.out.println(item);
}
});
//method reference
//Output : A,B,C,D,E
items.forEach(System.out::println);
//Stream and filter
//Output : B
items.stream()
.filter(s->s.contains("B"))
.forEach(System.out::println);
4
1
Set<String> set = new HashSet<String>();
//populate set
for (String s : set) {
System.out.println(s);
}
Thank you!
1
0
4
2
names.forEach(name -> System.out.println(name));
Thank you!
2
0
Are there any code examples left?
New code examples in category Java