guava-tricks

StringJoiner application scenario: in conditional splicing of sql

You only use StringBuilder? Try StringJoiner, it's delicious!

When using multiple identical delimiters, use StringJoiner,

head and tail contain, and spaced

 public static void main(String[] args) {
        StringJoiner stringJoiner = new StringJoiner(",", "[", "]");// 对新增的字符串用逗号间隔,用[]作为收尾包含
        stringJoiner.add("hello");
        stringJoiner.add("guys");
        stringJoiner.add("欢迎关注公众号Java技术栈");
        System.out.println(stringJoiner.toString()); 
        //输出   [hello,guys,欢迎关注公众号Java技术栈]
    }

interval only

public static void main(String[] args) {
        StringJoiner stringJoiner = new StringJoiner(",");//用逗号间隔新增的字符串
        stringJoiner.add("hello");
        stringJoiner.add("guys");
        stringJoiner.add("欢迎关注公众号Java技术栈");
        System.out.println(stringJoiner.toString()); 
        //输出    hello,guys,欢迎关注公众号Java技术栈
    }

create collection

// 普通Collection的创建
List<String> list = Lists.newArrayList();
Set<String> set = Sets.newHashSet();
Map<String, String> map = Maps.newHashMap();

// 不变Collection的创建
ImmutableList<String> iList = ImmutableList.of("a", "b", "c");
ImmutableSet<String> iSet = ImmutableSet.of("e1", "e2");
ImmutableMap<String, String> iMap = ImmutableMap.of("k1", "v1", "k2", "v2");

ImmutableList (Immutable immutable)

Usage scenario: safe under multi-threading, if you want to change, consider the copyAndWriter mechanism: that is, build a new list, and then point the old reference to the new list. Can handle high concurrent read and write,

おすすめ

転載: blog.csdn.net/u013372493/article/details/122726443