简体   繁体   English

如何反转SortedSet的顺序

[英]How to reverse the order of SortedSet

I want to print an ordered list in Map using the following: 我想使用以下内容在Map中打印一个有序列表:

Map<Float, String> mylist = new HashMap<>();

mylist.put(10.5, a);
mylist.put(12.3, b);
mylist.put(5.1, c);

SortedSet<Float> orderlist = new TreeSet<Float>(mylist.keySet());

for (Float i : orderlist) {
    System.out.println(i+" "+mylist.get(i));
}

The above code prints: 上面的代码打印:

5.1 c
10.5 a
12.3 b    

But how do I the print the orderlist in reverse order like below: 但是如何以相反的顺序打印订单列表,如下所示:

12.3 b
10.5 a
5.1 c

If you're willing to store the elements in the SortedSet in reverse order, the only change you need to make is to construct the TreeSet with an appropriate constructor which takes a custom Comparator : 如果您愿意以相反的顺序将元素存储在SortedSet中,那么您需要做的唯一更改是使用适当的构造函数构造 TreeSet该构造函数采用自定义Comparator

Map<Float, String> mylist = new HashMap<>();

mylist.put(10.5, a);
mylist.put(12.3, b);
mylist.put(5.1, c);

SortedSet<Float> orderlist = new TreeSet<Float>(Collections.reverseOrder());
orderList.addAll(mylist.keySet());

for (Float i : orderlist) {
    System.out.println(i+" "+mylist.get(i));
}

Note the neat method here is Collections.reverseOrder() which returns a Comparator which compares in the opposite of the natural ordering of elements. 注意这里的整洁方法是Collections.reverseOrder() ,它返回一个Comparator ,它与元素的自然顺序相反。

You can also try this : 你也可以试试这个:

    Map<Float, String> mylist = new HashMap<Float, String>();
    mylist.put(10.5, a);
    mylist.put(12.3, b);
    mylist.put(5.1, c);

    SortedSet<Float> orderlist = new TreeSet<Float>(mylist.keySet()).descendingSet();

    for (Float i : orderlist) {
        System.out.println(i+" "+mylist.get(i));
    }

Try to use NavigableSet : 尝试使用NavigableSet

 public NavigableSet<E> descendingSet()

Like this: 像这样:

  SortedSet<Float> orderlist = new TreeSet<Float>(mylist.keySet());
  SortedSet<Float> treereverse = new TreeSet<Float>();
  // creating reverse set
  treereverse=(TreeSet)orderlist.descendingSet();

Finally you have treereverse with the reverse order. 最后你有反向顺序的treereverse反转。

You can try this implementation ( source ): 您可以尝试此实现( ):

public sealed class OrderedSet<T> : ICollection<T>
{
    private readonly IDictionary<T, LinkedListNode<T>> _dictionary;
    private readonly LinkedList<T> _linkedList;

    public OrderedSet()
        : this(EqualityComparer<T>.Default)
    {
    }

    private OrderedSet(IEqualityComparer<T> comparer)
    {
        _dictionary = new Dictionary<T, LinkedListNode<T>>(comparer);
        _linkedList = new LinkedList<T>();
    }

    public int Count => _dictionary.Count;

    public bool IsReadOnly => _dictionary.IsReadOnly;

    void ICollection<T>.Add(T item)
    {
        Add(item);
    }

    public void Clear()
    {
        _linkedList.Clear();
        _dictionary.Clear();
    }

    public bool Remove(T item)
    {
        var found = _dictionary.TryGetValue(item, out var node);
        if (!found)
        {
            return false;
        }

        _dictionary.Remove(item);
        _linkedList.Remove(node);
        return true;
    }

    public IEnumerator<T> GetEnumerator()
    {
        return _linkedList.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public bool Contains(T item)
    {
        return _dictionary.ContainsKey(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        _linkedList.CopyTo(array, arrayIndex);
    }

    public void Add(T item)
    {
        if (_dictionary.ContainsKey(item))
        {
            return;
        }

        var node = _linkedList.AddLast(item);
        _dictionary.Add(item, node);
    }

    public void Reverse()
    {
        var head = _linkedList.First;
        while (head.Next != null)
        {
            var next = head.Next;
            _linkedList.Remove(next);
            _linkedList.AddFirst(next.Value);
        }
    }
}

Please note that this is an OrderSet which preserves the insertion order unlike SortedSet which keeps the set sorted according to some comparator (eg alphabetically). 请注意,这是一个OrderSet ,它保留了插入顺序,这与SortedSet不同,后者根据某个比较器(例如按字母顺序)对集合进行排序。

I added the Reverse() method from here . 我从这里添加了Reverse()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM