Use joiner's tools of Guava

public class JoinerTest {
    
    private static final List<String> list1 = Arrays.asList
                ("google","guava","java","scala","kafka");
    
    private static final List<String> list2 = Arrays.asList
            ("google","guava","java",null,"scala","kafka");
    
    public static void main(String[] args) throws IOException {
        
        //1.用指定的字符连接
        String join = Joiner.on("#").join(list1);the Join);"string using a # connection is:" +
        System.out.println (
        
        // 2. The null skip value connections 
        String joinNull = Joiner.on ( "#" ) .skipNulls () the Join (List2);. 
        System.out.println ( "# connection string number, skipping intermediate "+: the null value ; joinNull) 
        
        // instead of 3. when the presence of a null value with the specified value 
        String joinUseForNull = Joiner.on (" # the DEFAULT "") useForNull (. " ) .join (List2); 
        the System.out. the println ( "null value instead of the set value with the specified:" + joinUseForNull); 
        
        // 4. put into a set or stringBuilder stringBuffer 
        the stringBuilder Builder = Joiner.on ( "#") useForNull ( "the DEFAULT") the appendTo.. ( new new the StringBuilder (), List2); 
        the StringBuffer Buffer . Joiner.on = ( "#") useForNull ( "the DEFAULT").appendTo(new StringBuffer(), list2);
        System.out.println("拼接成的 builder是:"+builder);
        System.out.println("拼接成的buffer是:"+buffer);
        
        //5.对于key value的分隔
        Map<String, String> map = new HashMap<String, String>();
        map.put("hello", "java");
        map.put("scala", "guava");
        String joinMap = Joiner.on("#").withKeyValueSeparator("=").join(map);
        System.out.println("key value 的分隔符:"+joinMap);
        
    }
}

 

Guess you like

Origin www.cnblogs.com/MrRightZhao/p/11300422.html