Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

How can you maintain a Collection with elements in Sorted order?

Pragya Keshap answered on February 4, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers

  • how to sort collection in reverse order
  • how to sort a collection

  • How can you maintain a Collection with elements in Sorted order?

    0

    In Java, there are many ways to maintain a Collection with elements

    in sorted order.

    Some collections like TreeSet store elements in the natural

    ordering. In case of natural ordering we have to implement

    Comparable interface for comparing the elements.

    We can also maintain custom ordering by providing a custom

    Comparator to a Collection.

    Another option is to use the utility method Collections.sort() to sort

    a List. This sorting gives nlog(n) order of performance. But if we

    have to use this method multiple times then it will be costly on

    performance.

    Another option is to use a PriorityQueue that provides an ordered

    queue. The main difference between PriorityQueue and

    Collections.sort() is that PriorityQueue maintains a queue in Order

    all the time, but we can only retrieve head element from queue. We

    cannot access the elements of PriorityQueue in Random order.

    We can use TreeSet to maintain sorted order of elements in

    collection if there are no duplicate elements in collection.

    https://www.javatpoint.com/Sorting-in-collection-framework

    Popularity 1/10 Helpfulness 1/10 Language java
    Source: Grepper
    Tags: elements java
    Link to this answer
    Share Copy Link
    Contributed on Feb 04 2023
    Pragya Keshap
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.