Skip to content

Instantly share code, notes, and snippets.

@IT-Berater
Last active June 29, 2016 18:39
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 IT-Berater/ab44abd46f718b1da54feca808ab035b to your computer and use it in GitHub Desktop.
Save IT-Berater/ab44abd46f718b1da54feca808ab035b to your computer and use it in GitHub Desktop.
List to CSV with trim, sort, distinct and toUpperCase
package de.wenzlaff.twpubnub;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import static java.util.stream.Collectors.joining;
/**
* Testklasse für neue Java 1.8 Funktionen.
*
* @author Thomas Wenzlaff
* @since 29.06.2016
*
*/
public class Csv {
/**
* Wie können mit Java 1.8 leicht Listen nach CSV transformiert werden?
*
* Und auch noch leicht verändert
*
* - in Großbuchstaben (toUpperCase) - trimmen (trim) - doppelte rausfiltern
* (distinct) - sortiert (sorted)
*
* ...usw.
*
* das und noch viel mehr geht mit streams.
*/
@Test
public void csvToUpperCollect() {
List<String> farben = Arrays.asList("rot ", "GELB", "grün", "lila", "lila");
String farbenCsv = farben.stream().map(String::toUpperCase).map(String::trim).distinct().sorted()
.collect(joining(","));
assertEquals("GELB,GRÜN,LILA,ROT", farbenCsv);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment