Created
November 2, 2014 21:51
-
-
Save vfarcic/62e22b457f5667dee1c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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