Listfruits = Arrays.asList("apple", "banana", "cherry", "date"); String joined = fruits.stream().collect(Collectors.joining(", ")); System.out.println(joined);
ListOutput: "Numbers: 1 : 2 : 3 : 4 : 5." In both examples, the Collectors.joining() method concatenates the input elements into a string using a specified delimiter (", " or " : "), prefix ("Numbers: "), and suffix ("." or an empty string). The Collectors.joining() method is included in the java.util.stream package library.numbers = Arrays.asList(1, 2, 3, 4, 5); String joined = numbers.stream().map(String::valueOf).collect(Collectors.joining(" : ", "Numbers: ", ".")); System.out.println(joined);