Reduce to one string
Description
The following code shows how to reduce to one string.
Example
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/*from w w w . ja va2 s. c o m*/
public class Main {
public static void main(String...args){
Student raoul = new Student("Raoul", "Cambridge");
Student mario = new Student("Mario","Milan");
Student alan = new Student("Alan","Cambridge");
Student brian = new Student("Brian","Cambridge");
List<Graduate> transactions = Arrays.asList(
new Graduate(brian, 2011, 300),
new Graduate(raoul, 2012, 1000),
new Graduate(raoul, 2011, 400),
new Graduate(mario, 2012, 710),
new Graduate(mario, 2012, 700),
new Graduate(alan, 2012, 950)
);
String traderStr =
transactions.stream()
.map(transaction -> transaction.getTrader().getName())
.distinct()
.sorted()
.reduce("", (n1, n2) -> n1 + n2);
System.out.println(traderStr);
}
}
class Student{
private String name;
private String city;
public Student(String n, String c){
this.name = n;
this.city = c;
}
public String getName(){
return this.name;
}
public String getCity(){
return this.city;
}
public void setCity(String newCity){
this.city = newCity;
}
public String toString(){
return "Student:"+this.name + " in " + this.city;
}
}
class Graduate{
private Student trader;
private int year;
private int value;
public Graduate(Student trader, int year, int value)
{
this.trader = trader;
this.year = year;
this.value = value;
}
public Student getTrader(){
return this.trader;
}
public int getYear(){
return this.year;
}
public int getValue(){
return this.value;
}
public String toString(){
return "{" + this.trader + ", " +
"year: "+this.year+", " +
"value:" + this.value +"}";
}
}
The code above generates the following result.