Convert java Map to custom key=value string

前端 未结 4 1334
感动是毒
感动是毒 2021-02-13 04:16

I have TreeMap which I need to convert to URI-like string and then back to Map. I need to set custom delimiters.

Is there any tool (Gu

4条回答
  •  情歌与酒
    2021-02-13 04:43

    Using Java8:

    private static Map prepareMap() {
        Map map = new LinkedHashMap<>();
        map.put("key1", "val1");
        map.put("key2", "val2");
        return map;
    }
    
    @Test
    public void toStr() {
        assertEquals("key1_val1|key2_val2", prepareMap().entrySet().stream()
                .map(e -> e.getKey() + "_" + e.getValue())
                .collect(Collectors.joining("|")));
    }
    
    @Test
    public void toStrFunction() {
        assertEquals("key1_val1|key2_val2", joiner("|", "_").apply(prepareMap()));
    }
    
    private static Function, String> joiner(String entrySeparator, String valueSeparator) {
        return m -> m.entrySet().stream()
                .map(e -> e.getKey() + valueSeparator + e.getValue())
                .collect(Collectors.joining(entrySeparator));
    }
    
    @Test
    public void toMap() {
        assertEquals("{key1=val1, key2=val2}", Stream.of("key1_val1|key2_val2".split("\\|"))
                .map(e -> e.split("_", 2))
                .collect(Collectors.toMap(e -> e[0], e -> e.length > 1 ? e[1] : null)).toString());
    }
    
    @Test
    public void toMapFunction() {
        assertEquals("{key1=val1, key2=val2}", splitter("\\|", "_").apply("key1_val1|key2_val2").toString());
    }
    
    private static Function> splitter(String entrySeparator, String valueSeparator) {
        return s -> Stream.of(s.split(entrySeparator))
                .map(e -> e.split(valueSeparator, 2))
                .collect(Collectors.toMap(e -> e[0], e -> e.length > 1 ? e[1] : null));
    }
    

提交回复
热议问题