Created
July 21, 2015 10:10
-
-
Save MaSven/1d2936c85047a68e89ae to your computer and use it in GitHub Desktop.
Formatiert einen String in eine Map
This file contains hidden or 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
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
/** | |
* | |
*/ | |
/** | |
* Wandelt mittels der Java 8 Stream api einen StringListe in eine Map | |
* @author eiamnacken | |
* | |
*/ | |
public class ListToMap { | |
public static void main(String[] args) { | |
String document = | |
"Lorem Ipsum\n" + | |
"1234567891011\n" + | |
"abcdegfh\n" + | |
"tralala und Filibu\n" + | |
"Filubu\n" + | |
"tralala\n"; | |
Map<Integer, String> map = filterLongLines(document, 10); | |
System.out.println(map); | |
} | |
public static Map<Integer,String> filterLongLines(String document, int length) { | |
Map<Integer, String> map = new HashMap<Integer, String>(); | |
Stream.of(document.split("\n")).filter(e -> e.length()>length).forEach((string) -> map.put(map.size(), string)); | |
return map; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment