返回一个只包含1个指定元素的列表,相对于AsList(),它优化了内存分配
List<Map<String, Object>> srcNodeMapResult = Collections.singletonList(nodeMap)
// srcNodeMapResult此时只对应了1个源节点的属性集,因此在单源条件下,将List转变成singletonList节省内存
具体示例
import java.util.*;
public class CollectionsDemo {
public static void main(String args[]) {
// create an array of string objs
String init[] = { "One", "Two", "Three", "One", "Two", "Three" };
// create one list
List list = new ArrayList(Arrays.asList(init));
System.out.println("List value before: "+list);
// create singleton list
list = Collections.singletonList("TP");
System.out.println("List value after: "+list);
}
}
输出结果
List value before: [One, Two, Three, One, Two, Three]
List value after: [TP]