如何反转SortedSet的顺序

时间:2016-02-01 03:12:24

标签: java treeset sortedset

我想使用以下内容在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));
}

以上代码打印:

5.1 c
10.5 a
12.3 b    

但是如何以相反的顺序打印订单列表,如下所示:

12.3 b
10.5 a
5.1 c

4 个答案:

答案 0 :(得分:6)

如果您愿意以相反的顺序存储city = create(:city) invalid_city = City.new(country_code: city.country_code, name: city.name) expect(invalid_city).not_to be_valid expect(invalid_city.errors.full_messages).to include "This city has already been added" 中的元素,那么您需要进行的唯一更改是使用an appropriate constructor构建SortedSet,这需要自定义{ {3}}:

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)); } ,它与元素的自然顺序相反。

答案 1 :(得分:4)

你也可以试试这个:

    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));
    }

答案 2 :(得分:2)

尝试使用NavigableSet

 public NavigableSet<E> descendingSet()

像这样:

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

最后,您的treereverse顺序相反。

答案 3 :(得分:0)

您可以尝试此实现(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);
        }
    }
}

请注意,这是OrderSet,它保留插入顺序,而SortedSet则根据某些比较器(例如,按字母顺序)对集合进行排序。

我从here添加了Reverse()方法。