踩坑记录 Lists.newArrayList()

String punishTag = "tag1, tag2";
使用如下语句
List tags = Lists.newArrayList(punishTag);
期望结果是tags中有2个对象,实际结果是tags中只有一个“tag1,tag2”的String对象。
正确做法:
String[] tags = punishTag.split(",");
List tagList = Arrays.asList(tags);

关于Lists.newArrayList:
guava-21.0-sources.jar
源码:
@SafeVarargs
@CanIgnoreReturnValue // TODO(kak): Remove this
@GwtCompatible(serializable = true)
public static ArrayList newArrayList(E... elements) {
checkNotNull(elements); // for GWT
// Avoid integer overflow when a large array is passed in
int capacity = computeArrayListCapacity(elements.length);
ArrayList list = new ArrayList(capacity);
Collections.addAll(list, elements);
return list;
}

你可能感兴趣的