精华内容
下载资源
问答
  • <div><p>Hi, please release latest version of kryo-serializers that have support for guava's ImmutableList.</p><p>该提问来源于开源项目:magro/kryo-serializers</p></div>
  • <div><p>Harden immutability of ImmutableList. <p>This closes a few avenues that could have lead to mutating the supposed to be immutable list. In particular, the backing array was non-final and ...
  • ImmutableList的使用方法

    2020-12-09 21:20:48
    1、pom.xml <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId>...import com.google.common.collect.ImmutableList; /** * I

    1、pom.xml

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
    </dependency>

    2、测试类

    import com.google.common.collect.ImmutableList;
    
    /**
     * ImmutableList是一个不可变、线程安全的列表集合,它只会获取传入对象的一个副本。
     */
    public class ImmutableListTest {
        public static void main(String [] args) {
            ImmutableList<String> list = ImmutableList.of("a", "b", "c", "d");
            System.out.println(list);
        }
    }
    展开全文
  • <code>Error:(18, 51) error: incompatible types: ArrayList cannot be converted to ImmutableList<String></code></p> <p>When I try to rebuild this sample class from official ...
  • It should not throw an exception and return a new instance of ImmutableList that is a deep clone of the original, like it doesfor other List implementations like ArrayList. <p><em>Original issue: ...
  • t <code>ImmutableList</code> implement <code>Equals</code> as <code>SequenceEquals</code>? <pre><code>CSharp var a = ImmutableList.Create(42); var b = ImmutableList.Create(42); // This ...
  • 1、不允许null值,不可变集合 2、代码例子 import com.google.common.collect.ImmutableList; public class MainTest { ... names = ImmutableList.of("张三", "李四"); System.out.println(names); .
    1、不允许null值,不可变集合
    
    2、代码例子
    
    import com.google.common.collect.ImmutableList;
    
    public class MainTest {
        public static void main(String [] args) {
            ImmutableList<String> names = ImmutableList.of("张三", "李四");
            System.out.println(names);
        }
    }

     

    //结果:

    [张三, 李四]
     

    展开全文
  • )Lcom/google/common/collect/ImmutableList;</code></p> <pre><code>at org.robolectric.internal.bytecode.ShadowProviders.<init>(ShadowProviders.java:25) at sun.reflect.NativeConstructorAccessorImpl...
  • guava中ImmutableList使用示例详解

    万次阅读 2018-07-03 22:47:54
    ImmutableList是一个不可变、线程安全的列表集合,它只会获取传入对象的一个副本,而不会影响到原来的变量或者对象; 验证上面的说法代码: int a = 23; ImmutableList&amp;amp;amp;lt;Integer&amp;...

    ImmutableList是一个不可变、线程安全的列表集合,它只会获取传入对象的一个副本,而不会影响到原来的变量或者对象,如下代码:

            int a = 23;
            ImmutableList<Integer> list = ImmutableList.of(a, 12);
            System.out.println(list);
            a = 232;
            System.out.println(list);

    输出结果:

    [23, 12]
    [23, 12]

    ImmutableList创建不可变对象有两种方法,一种是使用静态of方法,另外一种是使用静态内部类Builder。

    • 静态内部类构建方法
      先看下源码,他是一个静态内部类,使用static和final修饰符,来辅助外部类ImmutableList构建不可变的集合对象
      public static final class Builder<E>
         extends ImmutableCollection.Builder<E>
       {
         private Object[] contents;
         private int size;
         public Builder()
         {
           this(4);
         }
         Builder(int capacity)
         {
           this.contents = new Object[capacity];
           this.size = 0;
         }
    
         Builder<E> ensureCapacity(int minCapacity)
         {
           if (this.contents.length < minCapacity) {
             this.contents = ObjectArrays.arraysCopyOf(this.contents, expandedCapacity(this.contents.length, minCapacity));
           }
    
           return this;
         }
         public Builder<E> add(E element)
         {
           Preconditions.checkNotNull(element);
           ensureCapacity(this.size + 1);
           this.contents[(this.size++)] = element;
           return this;
         }
    
         public Builder<E> addAll(Iterable<? extends E> elements)
         {
           if ((elements instanceof Collection)) {
             Collection<?> collection = (Collection)elements;
             ensureCapacity(this.size + collection.size());
           }
           super.addAll(elements);
           return this;
         }
         public Builder<E> add(E... elements)
         {
           for (int i = 0; i < elements.length; i++) {
             ObjectArrays.checkElementNotNull(elements[i], i);
           }
           ensureCapacity(this.size + elements.length);
           System.arraycopy(elements, 0, this.contents, this.size, elements.length);
           this.size += elements.length;
           return this;
         }
         public Builder<E> addAll(Iterator<? extends E> elements)
         {
           super.addAll(elements);
           return this;
         }
         public ImmutableList<E> build()
         {
           switch (this.size) {
           case 0: 
             return ImmutableList.of();
    
           case 1: 
             E singleElement = this.contents[0];
             return ImmutableList.of(singleElement);
           }
           if (this.size == this.contents.length)
           {
             return new RegularImmutableList(this.contents);
           }
           return new RegularImmutableList(ObjectArrays.arraysCopyOf(this.contents, this.size));
         }
       }

    使用的具体示例:

            //使用内部类的方式
            ImmutableList<Integer> list = ImmutableList .<Integer>builder()
                                                            .add(12)
                                                            .add(23)
                                                            .add(34)
                                                            .build();
    • 静态方法
      静态方法提供了多个参数的of静态方法
            //获取一个空的不可变集合对象
            ImmutableList<String> list1 = ImmutableList .<String>of();
            //获取一个有一个元素的不可变集合对象
            ImmutableList<String> list2 = ImmutableList .<String>of("12");
            //获取一个有两个元素的不可变集合对象
            ImmutableList<String> list3 = ImmutableList .<String>of("12","23");
    • 复制数组中的元素到ImmutableList中
            List<String> list4 = new ArrayList<String>();
            list4.add("1");
            list4.add("2");
            list4.add("3");
            //copy数组list4的一个副本
            List<String> list5 = ImmutableList .<String>copyOf(list4);
    展开全文
  • <div><p>Fixes CME caused by java 8 stream api by using an ImmutableList for cached admins. <h2>Review <ul><li>[ ] Tests</li><li>[ ] Documentation</li></ul>该提问来源于开源项目:zalando/nakadi...
  • t we using bitmapped Vector Tries for ImmutableList? Forgive me if this question has been asked. <p>As far as I know, this data structure is used in Closure's PersistentVector, and ImmutableJS'...
  • [guava]ImmutableList使用问题分析

    千次阅读 2016-10-25 22:43:38
    今天用到ImmutableList的时候,在使用ImmutableList的过程中发现针对于 使用场景:  public ImmutableList validatePredicates = ImmutableList.of(  new BmZJGroupService()  ); 其中 BmZJGroupService 是 ...
    今天用到ImmutableList的时候,在使用ImmutableList的过程中发现针对于
    使用场景:
        public ImmutableList<BmAbstractGroupService> validatePredicates = ImmutableList.of(
                new BmZJGroupService()
        );
    其中
    BmZJGroupService 是 BmAbstractGroupService的子类
    此时会报错,报错提示将BmAbstractGroupService转换为BmZJGroupService
    但是同样的使用场景
    public ImmutableList<ValidatePredicate> validatePredicates = ImmutableList.of(
                new CommonValidatePredicate(),
                new ZJFullTimeRiderValidatePredicate(),
                new ZJPartTimeRiderValidatePredicate(),
                new CityProxyPartTimeRiderValidatePredicate(),
                new ZJFullTimeZDValidatePredicate(),
                new ZJPartTimeZDValidatePredicate()
        );
    new CommonValidatePredicate(),new ZJFullTimeRiderValidatePredicate(),new ZJPartTimeRiderValidatePredicate(),
    new CityProxyPartTimeRiderValidatePredicate(),
    new ZJFullTimeZDValidatePredicate(),new ZJPartTimeZDValidatePredicate()
    都继承自ValidatePredicate,但是该场景下没有问题
    并不会报错,追踪源码如下:
    1.第一种情况
      public static <E> ImmutableList<E> of(E element) {
        return new SingletonImmutableList<E>(element);
      }

    2.第二种情况
      /**
       * Returns an immutable list containing the given elements, in order.
       *
       * @throws NullPointerException if any element is null
       */
      public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
        return construct(e1, e2, e3, e4, e5, e6);
      }

    分析原因是因为针对第一种情况是因为在返回ImmutableList<E>时将BmZJGroupService转为了 E,ImmutableList<BmZJGroupService>返回给了
    ImmutableList<BmAbstractGroupService> validatePredicates
    但是此时发ImmutableList<BmAbstractGroupService> !=ImmutableList<BmZJGroupService>,因此会报错
    但是对于第二种情况这五个实例具有共同的父类ValidatePredicate,在进行构造时,选取这几个实例的共同的父类作为返回值。

    考虑第三种情况,一个BmAbstractGroupService类,一个String类,提示中,显示将Object的类型作为返回值。间接证明了上述是正确的,因为java中所有类的统一父类事Object。



    展开全文
  • <div><p><code>ImmutableList<T>.Builder</code> should have <code>Replace()</code> methods like <code>ImmutableList<T></code> does, in order to make it easier switching between the two in ...
  • | ImmutableList | 512 | 13,338.16 us | | ContainsFalse<String> | ImmutableList | 512 | 24,118.15 us | | ContainsTrue<Int32> | ImmutableList | 512 | 6,590.86 us | | ContainsTrue<String&...
  • typeof(ImmutableList).GetMethods().Where(m => m.Name == nameof(ImmutableList.Create) && m.GetParameters().Length == 0).Single().MakeGenericMethod(dummType); var list &...
  • System.Collections.Immutable.ImmutableList`1[System.Int32]' could not be found. <p>I suspect other immutable types won't work either... <pre><code>cs using ExtendedXmlSerializer; using ...
  • <div><p>First change set makes the template use IImutableList instead of ImmutableList, which in itself doesn't work because ToImmutable() cannot pass interface types variables directly to the ...
  • guava不可变集合ImmutableList: [img]http://dl2.iteye.com/upload/attachment/0124/2896/855a84e2-1491-3f8d-ad27-c0ff73b1bfb5.png[/img]
  • </code> properties not having a <code>setFoo()</code> by using <code>ImmutableList<T></code>. <p>It looks like the latest master (237dbfa603f90e18c50e710c1e1a56127a5e137c) no longer emits <code>...
  • Immutable Collections(2)ImmutableList<T>实现原理.(上) 文/玄魂 前言 在上一篇文章(Immutable Collections(1)),我简要说明了不可变集合的基本概念和简单应用。从本篇博文开始,会探讨下几个典型...
  • This works with normal List because this guy returns -1 when an incompatible type is used, where ImmutableList throws an exception (doesn't do any check actually) and ComboBox doesn't ...
  • 17.4 | 74 | 163 | System.Collections.Immutable.ImmutableList1+Enumerator[System.Collections.Generic.KeyValuePair2[System.__Canon,System.__Canon]].Dispose() | JIT | System.Collections.Immutable.dll...
  • <div><p>Doesn't feel like this fits the criteria of being a 'baseline requirement' - in Java 6 there's an advantage to the former, but in Java 7+ there's no practical advantage...
  • <div><h3>Environment <p>JRuby 9.1.12.0 Java 1.8.0_92 Mac OS X <p>rspec 3.6.0 <h3>Expected Behavior <p>It's complicated....<p>I was still in the process of figuring out why this only happened for <em>...
  • <div><p>Avoids mistakes with other list implementations being dispatched to a different method overload (intended for #varargs). Resolves #1908</p><p>该提问来源于开源项目:querydsl/querydsl</p>...
  • Google Collections使用笔记-ImmutableList

    千次阅读 2019-02-28 11:03:26
    ImmutableMap ,ImmutableList,ImmutableSet 一经初始化是无法改变的,包括往里面新增数据。 package cn.com.osd.test_goolecollection; import java.util.ArrayList; import java.util.List; import ...

空空如也

空空如也

1 2 3 4 5 ... 19
收藏数 374
精华内容 149
关键字:

immutablelist