东莞网站建设流程,做公司网站多少钱,做期货看资讯什么网站好,北京市基础建设质量监督局网站通常#xff0c;您需要向集合中添加新元素。 因为您是一个优秀而谨慎的开发人员#xff0c;所以您希望尽可能保持不变。 因此#xff0c;向不可变集合中添加新元素将意味着您必须创建一个新的不可变集合#xff0c;其中包含原始集合的所有元素以及新元素。 您可以使用gua… 通常您需要向集合中添加新元素。 因为您是一个优秀而谨慎的开发人员所以您希望尽可能保持不变。 因此向不可变集合中添加新元素将意味着您必须创建一个新的不可变集合其中包含原始集合的所有元素以及新元素。 您可以使用guava库以及最近的pCollection库来创建不可变的集合。 在下面的示例中我们将构建2个不可变列表其中一个来自guava不可变另一个来自pCollection持久。 它们最初都将包含10.000整数。 我们将为每种类型创建20.000个不可变列表我们将测量花费的时间。 package com.marco.pcollections;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.pcollections.PCollection;
import org.pcollections.TreePVector;import com.google.common.collect.ImmutableList;public class PcollectionVSImmutable {public static void main(String[] args) {MapInteger, ImmutableListObject allImmutable new HashMapInteger, ImmutableListObject();MapInteger, PCollectionInteger allPersistent new HashMapInteger, PCollectionInteger();ListInteger bigList new ArrayListInteger();for (int i 0; i 10000; i) {bigList.add(new Integer(i));}ImmutableListInteger immutable ImmutableList.copyOf(bigList);PCollectionInteger persistent TreePVector.from(bigList);long start System.currentTimeMillis();for (int i 10000; i 30000; i) {allPersistent.put(new Integer(i), persistent.plus(new Integer(i)));}System.out.println(creating 20.000 pCollections takes : (System.currentTimeMillis() - start) ms);start System.currentTimeMillis();for (int i 10000; i 30000; i) {allImmutable.put(new Integer(i), ImmutableList.builder().addAll(immutable).add(new Integer(i)).build());}System.out.println(creating 20.000 Guava ImmutableList takes : (System.currentTimeMillis() - start) ms);System.out.println(All immutable size : allImmutable.size() allPersistent size : allPersistent.size());}
} 输出 creating 20.000 pCollections takes : 29ms
creating 20.000 Guava ImmutableList takes : 18347ms
All immutable size : 20000 allPersistent size : 20000翻译自: https://www.javacodegeeks.com/2015/05/simple-benchmarking-immutable-collections-vs-persistent-collections.html