| /******************************************************************************* |
| * Copyright (c) 2020 seanmuir. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * seanmuir - initial API and implementation |
| * |
| *******************************************************************************/ |
| package org.eclipse.mdht.cda.xml; |
| |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.stream.Collectors; |
| |
| /** |
| * @author seanmuir |
| * |
| */ |
| public class FooBar { |
| |
| /** |
| * @param args |
| */ |
| public static void main(String[] args) { |
| // fail("Not yet implemented"); |
| |
| ArrayList<String> aaaa = new ArrayList<String>(); |
| // aaaa.add("aaaa"); |
| // aaaa.add("bbbb"); |
| // aaaa.add("cccc"); |
| // |
| // String foo2 = convertWithStream(aaaa); |
| // System.out.println(foo2); |
| |
| Map<String, List<String>> wordsByKey = new HashMap<String, List<String>>(); |
| wordsByKey.put("1", new ArrayList<String>()); |
| wordsByKey.put("2", new ArrayList<String>()); |
| wordsByKey.put("3", new ArrayList<String>()); |
| wordsByKey.put("4", new ArrayList<String>()); |
| |
| wordsByKey.get("1").add("aaaaaaa"); |
| wordsByKey.get("2").add("aaaaaaa"); |
| wordsByKey.get("3").add("aaaaaaa"); |
| wordsByKey.get("4").add("aaaaaaa"); |
| |
| wordsByKey.get("1").add("bbb"); |
| wordsByKey.get("2").add("bbb"); |
| wordsByKey.get("3").add("bb"); |
| wordsByKey.get("4").add("bbb"); |
| |
| String foo = convertWithStream(wordsByKey); |
| System.out.println(foo); |
| |
| Map<String, List<String>> t = convertWithStream(foo); |
| |
| for (String k : t.keySet()) { |
| System.out.println(k + " >> " + t.get(k)); |
| } |
| |
| } |
| |
| public static String convertWithStream(List<String> list) { |
| String mapAsString = " " + String.join(" ", list) + " "; |
| return mapAsString; |
| } |
| |
| public static String convertWithStream(Map<String, List<String>> map) { |
| String mapAsString = map.keySet().stream().map(key -> key + "=" + convertWithStream(map.get(key))).collect( |
| Collectors.joining(", ", " ", " ")); |
| return mapAsString; |
| } |
| |
| public static Map<String, List<String>> convertWithStream(String mapAsString) { |
| Map<String, String> map1 = Arrays.stream(mapAsString.split(",")).map(entry -> entry.split("=")).collect( |
| Collectors.toMap(entry -> entry[0], entry -> entry[1])); |
| |
| Map<String, List<String>> map2 = new HashMap<String, List<String>>(); |
| for (String k : map1.keySet()) { |
| map2.put(k, new ArrayList<String>()); |
| map2.get(k).addAll(Arrays.asList(map1.get(k).split(" "))); |
| } |
| |
| return map2; |
| } |
| |
| } |