Skip to content

Instantly share code, notes, and snippets.

@vfarcic
Created November 2, 2014 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfarcic/62e22b457f5667dee1c1 to your computer and use it in GitHub Desktop.
Save vfarcic/62e22b457f5667dee1c1 to your computer and use it in GitHub Desktop.
package com.technologyconversations.java8exercises.streams;
import java.util.List;
import static java.util.stream.Collectors.joining;
public class Joining {
public static String namesToString7(List<Person> people) {
String label = "Names: ";
StringBuilder sb = new StringBuilder(label);
for (Person person : people) {
if (sb.length() > label.length()) {
sb.append(", ");
}
sb.append(person.getName());
}
sb.append(".");
return sb.toString();
}
public static String namesToString(List<Person> people) {
return people.stream() // Convert collection to Stream
.map(Person::getName) // Map Person to name
.collect(joining(", ", "Names: ", ".")); // Join names
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment