0 votes
in Amazon VPC by
What are different ways of iterating collection list in Java 8?

1 Answer

0 votes
by
List<String> notes = new ArrayList<>();
	notes.add("note1");
	notes.add("note2");
	notes.add("note3");
	notes.add("note4");
	notes.add("note5");

	//Using lambda expression
	//Output : note1,note2,note3,note4,note5
	notes.forEach(note->System.out.println(note));

	//Output : note3
	notes.forEach(note->{
		if("note3".equals(note)){
			System.out.println(note);
		}
	});

	//Using Stream and filter
	//Output : note2
	notes.stream()
		.filter(s->s.contains("note2"))
		.forEach(System.out::println);

	//Using method reference
	//Output : note1,note2,note3,note4,note5
	notes.forEach(System.out::println);

Related questions

0 votes
asked Aug 4, 2023 in Amazon VPC by DavidAnderson
0 votes
asked Aug 4, 2023 in JAVA by DavidAnderson
...