Home guava example - Joiner
Post
Cancel

guava example - Joiner

view source

collection에 null이 포함되어 있으면 NullPointerException이 발생한다.

1
2
3
4
5
6
7
    @Test(expected = NullPointerException.class)
    public void joinerNullTest() throws Exception {
        List<Integer> integers = Arrays.asList(1, 2, 3, null, 5);
        String joined = Joiner.on(", ")
                .join(integers);
        System.out.println(joined);
    }

null을 무시하는 방법.

1
2
3
4
5
6
7
8
    @Test
    public void joinerSkipNullTest() throws Exception {
        List<Integer> integers = Arrays.asList(1, 2, 3, null, 5);
        String joined = Joiner.on(", ")
                .skipNulls()
                .join(integers);
        System.out.println(joined); // 1, 2, 3, 5
    }

null 인 경우 특정 값으로 세팅도 가능

1
2
3
4
5
6
7
8
    @Test
    public void joinerUseForNullTest() throws Exception {
        List<Integer> integers = Arrays.asList(1, 2, 3, null, 5);
        String joined = Joiner.on(", ")
                .useForNull("empty")
                .join(integers);
        System.out.println(joined); // 1, 2, 3, empty, 5
    }

Map을 간단하게 String으로 변경하기

1
2
3
4
5
6
7
8
9
10
11
12
    @Test
    public void mapJoinerTest() throws Exception {
        // map to String
        Map<Integer, String> integerStringHashMap = new HashMap<>();
        integerStringHashMap.put(1, "one");
        integerStringHashMap.put(2, "two");
        integerStringHashMap.put(3, "three");
        String mapJoiner = Joiner.on("; ")
                .withKeyValueSeparator("->")
                .join(integerStringHashMap);
        System.out.println(mapJoiner); // 1->one; 2->two; 3->three
    }
This post is licensed under CC BY 4.0 by the author.