首页 > 代码库 > DefaultHashOperations multiget的一个坑

DefaultHashOperations multiget的一个坑

 

DefaultHashOperations的multiget如果没有数据会返回java.util.Collections.EmptyList,这个List没有重写add方法。

 

List<Long> resultIds=redisTemplate.boundHashOps(“key”).multiGet(ids)

 

resultIds在add一个新元素时,报了一下的错:
java.lang.UnsupportedOperationException        at java.util.AbstractList.add(AbstractList.java:148)        at java.util.AbstractList.add(AbstractList.java:108)

 

原因:

org.springframework.data.redis.core.RedisTemplate#boundHashOps

    public List<HV> multiGet(Collection<HK> hashKeys) {        return ops.multiGet(getKey(), hashKeys);    }

org.springframework.data.redis.core.DefaultHashOperations#multiGet

    public List<HV> multiGet(K key, Collection<HK> fields) {        if (fields.isEmpty()) {            return Collections.emptyList();//这个list是java.util.Collections#emptyList中的java.util.Collections.EmptyList        }        final byte[] rawKey = rawKey(key);        final byte[][] rawHashKeys = new byte[fields.size()][];        int counter = 0;        for (HK hashKey : fields) {            rawHashKeys[counter++] = rawHashKey(hashKey);        }        List<byte[]> rawValues = execute(new RedisCallback<List<byte[]>>() {            public List<byte[]> doInRedis(RedisConnection connection) {                return connection.hMGet(rawKey, rawHashKeys);            }        }, true);        return deserializeHashValues(rawValues);    }

 

这个java.util.Collections.EmptyList没有重写add方法:

    private static class EmptyList<E>        extends AbstractList<E>        implements RandomAccess, Serializable {        private static final long serialVersionUID = 8842843931221139166L;        public Iterator<E> iterator() {            return emptyIterator();        }        public ListIterator<E> listIterator() {            return emptyListIterator();        }        public int size() {return 0;}        public boolean isEmpty() {return true;}        public boolean contains(Object obj) {return false;}        public boolean containsAll(Collection<?> c) { return c.isEmpty(); }        public Object[] toArray() { return new Object[0]; }        public <T> T[] toArray(T[] a) {            if (a.length > 0)                a[0] = null;            return a;        }        public E get(int index) {            throw new IndexOutOfBoundsException("Index: "+index);        }        public boolean equals(Object o) {            return (o instanceof List) && ((List<?>)o).isEmpty();        }        public int hashCode() { return 1; }        @Override        public boolean removeIf(Predicate<? super E> filter) {            Objects.requireNonNull(filter);            return false;        }        @Override        public void replaceAll(UnaryOperator<E> operator) {            Objects.requireNonNull(operator);        }        @Override        public void sort(Comparator<? super E> c) {        }        // Override default methods in Collection        @Override        public void forEach(Consumer<? super E> action) {            Objects.requireNonNull(action);        }        @Override        public Spliterator<E> spliterator() { return Spliterators.emptySpliterator(); }        // Preserves singleton property        private Object readResolve() {            return EMPTY_LIST;        }    }

 

DefaultHashOperations multiget的一个坑