zoukankan      html  css  js  c++  java
  • Collections.singletonList方法的使用(转载)

    方法注释

        /**
         * Returns an immutable list containing only the specified object.
         * The returned list is serializable.
         *
         * @param  <T> the class of the objects in the list
         * @param o the sole object to be stored in the returned list.
         * @return an immutable list containing only the specified object.
         * @since 1.3
         */
    

    应用

    这个方法主要用于只有一个元素的优化,减少内存分配,无需分配额外的内存,可以从SingletonList内部类看得出来,由于只有一个element,因此可以做到内存分配最小化,相比之下ArrayList的DEFAULT_CAPACITY=10个。

    //SingletonList类的源码
        private static class SingletonList<E>
            extends AbstractList<E>
            implements RandomAccess, Serializable {
    
            private static final long serialVersionUID = 3093736618740652951L;
    
            private final E element;
    
            SingletonList(E obj)                {element = obj;}
    
            public Iterator<E> iterator() {
                return singletonIterator(element);
            }
    
            public int size()                   {return 1;}
    
            public boolean contains(Object obj) {return eq(obj, element);}
    
            public E get(int index) {
                if (index != 0)
                  throw new IndexOutOfBoundsException("Index: "+index+", Size: 1");
                return element;
            }
    
            // Override default methods for Collection
            @Override
            public void forEach(Consumer<? super E> action) {
                action.accept(element);
            }
            @Override
            public boolean removeIf(Predicate<? super E> filter) {
                throw new UnsupportedOperationException();
            }
            @Override
            public void replaceAll(UnaryOperator<E> operator) {
                throw new UnsupportedOperationException();
            }
            @Override
            public void sort(Comparator<? super E> c) {
            }
            @Override
            public Spliterator<E> spliterator() {
                return singletonSpliterator(element);
            }
        }
    
    
    //普通写法
        List<MyBean> beans= MyService.getInstance().queryBean(param);
        if (CollectionUtils.isEmpty(beans)) {
          beans= new ArrayList<>();
          MyBean bean= new MyBean(param);
          beans.add(bean);
        }
    
    //优化写法
        List<MyBean> beans= MyService.getInstance().queryBean(param);
        if (CollectionUtils.isEmpty(beans)) {
          MyBean bean= new MyBean(param);
          beans= Collections.singletonList(bean);
        }
    

    其他特殊容器类

    
    public static <T> Set<T> singleton(T o);
    
    public static <T> List<T> singletonList(T o);
    
    public static <K,V> Map<K,V> singletonMap(K key, V value);
     
    // 或者直接调用常量 EMPTY_LIST
    public static final <T> List<T> emptyList();
    
    //或者直接调用常量 EMPTY_MAP
    public static final <K,V> Map<K,V> emptyMap();
    
    //或者直接调用常量 EMPTY_SET
    public static final <T> Set<T> emptySet()
    
    
    • 需要注意的是,以上6个方法返回的容器类均是immutable,即只读的,如果调用修改接口,将会抛出UnsupportedOperationException

    https://www.cnblogs.com/oreo/p/9761940.html

  • 相关阅读:
    Linux查看CPU信息
    sed总结
    angularjs学习笔记--service、$http、$scope、angularjs指令、事件、api
    angularjs学习笔记--ng-model、controller、DI、$scope、$provide
    angularjs学习笔记--$http、数据渲染到表格、路由、依赖注入、provider
    angularjs学习笔记--主html&template html&module&template js、模块、控制器、双向数据绑定、过滤器
    angularjs学习笔记--视图、模板、组件、$http、$q、module
    angular学习笔记---下载并运行nicefish项目
    angular学习笔记---通过angular/cli建一个新的项目
    json模拟数据交互
  • 原文地址:https://www.cnblogs.com/lzh1043060917/p/14212968.html
Copyright © 2011-2022 走看看