Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中不可修改的NavigableSet/NavigableMap?_Java_Collections_Guava - Fatal编程技术网

Java中不可修改的NavigableSet/NavigableMap?

Java中不可修改的NavigableSet/NavigableMap?,java,collections,guava,Java,Collections,Guava,java.util.Collections有几个不可修改的方法,这些方法通过在禁止变异操作的装饰器中包装集合来提供不可修改的集合视图 Java6增加了对Java.util.NavigableSet和Java.util.NavigableMap的支持 我希望能够拥有不可修改的NavigableSets和NavigableMaps,但是java.util.Collections#不可修改的SortedSet(SortedSet)和java.util.Collections#不可修改的SortedMa

java.util.Collections
有几个
不可修改的
方法,这些方法通过在禁止变异操作的装饰器中包装集合来提供不可修改的集合视图

Java6增加了对
Java.util.NavigableSet
Java.util.NavigableMap
的支持

我希望能够拥有不可修改的
NavigableSet
s和
NavigableMap
s,但是
java.util.Collections#不可修改的SortedSet(SortedSet)
java.util.Collections#不可修改的SortedMap(SortedMap)
是不够的,因为它们不支持特定于
NavigableSet
NavigableMap
的操作

Christian Semrau提供的
不可修改NavigableSet
不可修改NavigableMap

是否有实际的实现(在问题评论中):


如中所述,JDK中没有此类方法。

这只是猜测,但未提供不可修改的实现的部分原因可能是因为NavigableSet/Map接口公开了未标记为可选的变异方法:

  • 纳维加布勒赛特。波尔第一
  • 纳维加布勒赛特酒店
  • NavigableMap.pollFirstEntry
  • NavigableMap.pollLastEntry
也就是说,当提供一个不可修改的实现时,抛出一个UnsupportedOperationException似乎是合理的。这就是这些实现(假设您使用的是GoogleGuava)所做的:

NavigableSet:

导入静态java.util.Collections.unmodifiableSortedSet;
导入java.util.Collections;
导入java.util.Iterator;
导入java.util.NavigableSet;
导入java.util.SortedSet;
导入com.google.common.collect.ForwardingSortedSet;
/**
*{@link NavigableSet}等价于{@link集合#不可修改的SortedSet(SortedSet)}。
*不幸的是,这是必需的,因为{@link Collections}缺少“不可修改的NavigableSet”
*(见http://stackoverflow.com/questions/2577706/unmodifiable-navigableset-navigablemap-in-java
*及http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6907251).
* 
*这只是一个猜测,但没有提供不可修改的实现的部分原因可能是因为
*{@link NavigableSet}公开未标记为可选的变异方法:
*-{@link NavigableSet#pollFirst()}
*-{@link NavigableSet#pollLast()}
*  
*此外,我们不能走不变的路线,因为谷歌番石榴没有提供不变的变体
*(见http://code.google.com/p/guava-libraries/issues/detail?id=664).
*
*@param见{@link NavigableSet}
*/
公共最终类不可修改NavigableSet扩展ForwardingSortedSet实现NavigableSet{
非公开最终NavigableSet代表;
私有最终分类集不可修改委托;
/**
*@param delegate请参阅{@link ForwardingSortedSet#delegate()}。
*/
公共不可修改的NavigableSet(NavigableSet代理){
this.delegate=委托;
unmodifiableDelegate=不可修改的SortedSet(委派);
}
/**
*@param委托
*@return{@link#不可修改的NavigableSet(NavigableSet)}
*@see EffectiveJava#Static _factories(而非_构造函数)
*/
公共静态不可修改NavigableSet新建不可修改NavigableSet(NavigableSet委托){
返回新的不可修改的NavigableSet(委托);
}
@凌驾
受保护的SortedSet委托(){
返回不可修改的委托;
}
@凌驾
公共电能上限(E){
返回代表。上限(e);
}
@凌驾
公共迭代器下降迭代器(){
//NavigableSet.DegendingIterator显式声明此行为。
//通过这样做,我们不需要做任何额外的事情来确保迭代器是不可修改的。
返回下降集().iterator();
}
@凌驾
公共导航墙集下降集(){
返回newUnmodifiableNavigableSet(delegate.degendingset());
}
@凌驾
公共东楼(东){
返回代表。楼层(e);
}
@凌驾
公共导航耳机(E-toElement,含布尔值){
返回新的不可修改的NavigableSet(代理耳机(包括toElement));
}
@凌驾
公共教育及高等教育(E){
返回代表。更高级别(e);
}
@凌驾
公共工程(E)较低(E){
返回代表。下(e);
}
@凌驾
公共E pollFirst(){
抛出新的UnsupportedOperationException();
}
@凌驾
公共E pollast(){
抛出新的UnsupportedOperationException();
}
@凌驾
公共NavigableSet子集(E fromElement、boolean fromInclusive、E toElement、boolean toInclusive){
返回新的不可修改的NavigableSet(delegate.subSet(fromElement、fromInclusive、toElement、toInclusive));
}
@凌驾
公共NavigableSet尾集(E fromElement,含布尔值){
返回新的不可修改的NavigableSet(delegate.tailSet(fromElement,包括在内));
}
}
导航地图:

导入静态不可修改的NavigableSet.newUnmodifiableNavigableSet;
导入静态java.util.Collections.unmodifiableSortedMap;
导入java.util.AbstractMap;
导入java.util.Map;
导入java.util.NavigableMap;
导入java.util.NavigableSet;
导入java.util.SortedMap;
导入com.google.common.collect.ForwardingSortedMap;
/**
*此类与{@link UnmodifiableNavigableSet}具有相同的有理数。
*@param见{@link NavigableMap}
*@param见{@link NavigableMap}
*/
公共最终类不可修改NavigableMap扩展ForwardingSortedMap实现NavigableMap{
非公开最终导航地图代表;
私人最终分类地图不可修改代表;
/**
*@param delegate请参阅{@link ForwardingSortedMap#delegate()}。
*/
公共不可修改NavigableMap(NavigableMap代理){
th
import static java.util.Collections.unmodifiableSortedSet;

import java.util.Collections;
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.SortedSet;

import com.google.common.collect.ForwardingSortedSet;

/**
 * {@link NavigableSet} equivalent of {@link Collections#unmodifiableSortedSet(SortedSet)}.
 * This is unfortunately needed because {@link Collections} lacks "UnmodifiableNavigableSet"
 * (see http://stackoverflow.com/questions/2577706/unmodifiable-navigableset-navigablemap-in-java
 * and http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6907251).
 * 
 * This is just a guess, but part of the reason why an unmodifiable implementation was not provided may be due to the fact that 
 * {@link NavigableSet} exposes mutating methods that aren't marked as optional:
 * - {@link NavigableSet#pollFirst()}
 * - {@link NavigableSet#pollLast()}
 *  
 * In addition, one can't go the immutable route, as Google Guava doesn't provide an Immutable variant 
 * (see http://code.google.com/p/guava-libraries/issues/detail?id=664).
 *
 * @param <E> See {@link NavigableSet}
 */
public final class UnmodifiableNavigableSet<E> extends ForwardingSortedSet<E> implements NavigableSet<E> {

    private final NavigableSet<E> delegate;
    private final SortedSet<E> unmodifiableDelegate;

    /**
     * @param delegate See {@link ForwardingSortedSet#delegate()}.
     */
    public UnmodifiableNavigableSet(NavigableSet<E> delegate) {
        this.delegate = delegate;
        unmodifiableDelegate = unmodifiableSortedSet(delegate);
    }

    /**
     * @param delegate
     * @return {@link #UnmodifiableNavigableSet(NavigableSet)}
     * @see EffectiveJava#Static_factories_instead_of_constructors
     */
    public static <E> UnmodifiableNavigableSet<E> newUnmodifiableNavigableSet(NavigableSet<E> delegate) {
        return new UnmodifiableNavigableSet<E>(delegate);
    }

    @Override
    protected SortedSet<E> delegate() {
        return unmodifiableDelegate;
    }

    @Override
    public E ceiling(E e) {
        return delegate.ceiling(e);
    }

    @Override
    public Iterator<E> descendingIterator() {
        // NavigableSet.descendingIterator explicitly states this behavior.
        // By doing this, we don't need to do anything extra to ensure the iterator is unmodifiable.
        return descendingSet().iterator();
    }

    @Override
    public NavigableSet<E> descendingSet() {
        return newUnmodifiableNavigableSet(delegate.descendingSet());
    }

    @Override
    public E floor(E e) {
        return delegate.floor(e);
    }

    @Override
    public NavigableSet<E> headSet(E toElement, boolean inclusive) {
        return newUnmodifiableNavigableSet(delegate.headSet(toElement, inclusive));
    }

    @Override
    public E higher(E e) {
        return delegate.higher(e);
    }

    @Override
    public E lower(E e) {
        return delegate.lower(e);
    }

    @Override
    public E pollFirst() {
        throw new UnsupportedOperationException();
    }

    @Override
    public E pollLast() {
        throw new UnsupportedOperationException();
    }

    @Override
    public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
        return newUnmodifiableNavigableSet(delegate.subSet(fromElement, fromInclusive, toElement, toInclusive));
    }

    @Override
    public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
        return newUnmodifiableNavigableSet(delegate.tailSet(fromElement, inclusive));
    }
}
import static UnmodifiableNavigableSet.newUnmodifiableNavigableSet;
import static java.util.Collections.unmodifiableSortedMap;

import java.util.AbstractMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.SortedMap;

import com.google.common.collect.ForwardingSortedMap;

/**
 * This class has the same rational as {@link UnmodifiableNavigableSet}.
 * @param <K> See {@link NavigableMap}
 * @param <V> See {@link NavigableMap}
 */
public final class UnmodifiableNavigableMap<K,V> extends ForwardingSortedMap<K,V> implements NavigableMap<K,V> {

    private final NavigableMap<K,V> delegate;
    private final SortedMap<K,V> unmodifiableDelegate;

    /**
     * @param delegate See {@link ForwardingSortedMap#delegate()}.
     */
    public UnmodifiableNavigableMap(NavigableMap<K,V> delegate) {
        this.delegate = delegate;
        unmodifiableDelegate = unmodifiableSortedMap(delegate);
    }

    /**
     * @param delegate
     * @return {@link #UnmodifiableNavigableMap(NavigableMap)}
     * @see EffectiveJava#Static_factories_instead_of_constructors
     */
    public static <K,V> UnmodifiableNavigableMap<K,V> newUnmodifiableNavigableMap(NavigableMap<K,V> delegate) {
        return new UnmodifiableNavigableMap<K,V>(delegate);
    }

    @Override
    protected SortedMap<K,V> delegate() {
        return unmodifiableDelegate;
    }

    private Map.Entry<K,V> newImmutableEntry(Map.Entry<K,V> entry) {
        return entry == null ? null : new AbstractMap.SimpleImmutableEntry<K,V>(entry);
    }

    @Override
    public Map.Entry<K,V> ceilingEntry(K key) {
        return newImmutableEntry(delegate.ceilingEntry(key));
    }

    @Override
    public K ceilingKey(K key) {
        return delegate.ceilingKey(key);
    }

    @Override
    public NavigableSet<K> descendingKeySet() {
        return newUnmodifiableNavigableSet(delegate.descendingKeySet());
    }

    @Override
    public NavigableMap<K,V> descendingMap() {
        return newUnmodifiableNavigableMap(delegate.descendingMap());
    }

    @Override
    public Map.Entry<K,V> firstEntry() {
        return newImmutableEntry(delegate.firstEntry());
    }

    @Override
    public Map.Entry<K,V> floorEntry(K key) {
        return newImmutableEntry(delegate.floorEntry(key));
    }

    @Override
    public K floorKey(K key) {
        return delegate.floorKey(key);
    }

    @Override
    public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
        return newUnmodifiableNavigableMap(delegate.headMap(toKey, inclusive));
    }

    @Override
    public Map.Entry<K,V> higherEntry(K key) {
        return newImmutableEntry(delegate.higherEntry(key));
    }

    @Override
    public K higherKey(K key) {
        return delegate.higherKey(key);
    }

    @Override
    public Map.Entry<K,V> lastEntry() {
        return newImmutableEntry(delegate.lastEntry());
    }

    @Override
    public Map.Entry<K,V> lowerEntry(K key) {
        return newImmutableEntry(delegate.lowerEntry(key));
    }

    @Override
    public K lowerKey(K key) {
        return delegate.lowerKey(key);
    }

    @Override
    public NavigableSet<K> navigableKeySet() {
        return newUnmodifiableNavigableSet(delegate.navigableKeySet());
    }

    @Override
    public Map.Entry<K,V> pollFirstEntry() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Map.Entry<K,V> pollLastEntry() {
        throw new UnsupportedOperationException();
    }

    @Override
    public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
        return newUnmodifiableNavigableMap(delegate.subMap(fromKey, fromInclusive, toKey, toInclusive));
    }

    @Override
    public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
        return newUnmodifiableNavigableMap(delegate.tailMap(fromKey, inclusive));
    }
}