I want to port this example to Java 8:
Map<String, String> data = new HashMap<String, String>() {{ put("name", "Middle"); put("prefix", "Front"); put("postfix", "Back"); }}; String title = ""; if (data.containsKey("prefix")) { title += data.get("prefix"); } if (data.containsKey("name")) { title += data.get("name"); } if (data.containsKey("postfix")) { title += data.get("postfix"); }
The correct conclusion:
FrontMiddleBack
I tried using streamset -> stream, but it does not return in the correct order.
String titles = macroParams.entrySet().stream() .filter(map -> "name".equals(map.getKey()) || "postfix".equals(map.getKey()) || "prefix".equals(map.getKey())) .sorted() .map(Map.Entry::getValue) .collect(Collectors.joining());
Output:
MidleFrontBack
Is it possible to get the same result using Java 8?
source share