Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7942067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:51:16+00:00 2026-06-03T23:51:16+00:00

Assume an application producing a number of HashMap<String, MyClass> data structures, each containing dozens

  • 0

Assume an application producing a number of HashMap<String, MyClass> data structures, each containing dozens to hundreds of Comparable objects of type MyClass, which need to end up in a single and sorted Collection.

Two possible implementations of this functionality return a SortedSet or a sorted List, as follows:

public static Set<MyClass> getSortedSet(HashMap<String, MyClass>... allMaps)
{
    SortedSet<MyClass> set = new TreeSet<MyClass>();

    Collection<MyClass> c;

    for (HashMap<String, MyClass> map:allMaps)
    {
        c = map.values();
        set.addAll(c);
    }

    return set;
}

public static List<MyClass> getSortedList(HashMap<String, MyClass>... allMaps)
{
    List<MyClass> list = new ArrayList<MyClass>();

    Collection<MyClass> c;

    for (HashMap<String, MyClass> map:allMaps)
    {
        c = map.values();
        list.addAll(c);
    }

    Collections.sort(list);

    return list;
}

Would there be any clear performance advantage to any of the above 2 methods?

Is there a faster way of implementing the same functionality?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:51:18+00:00Added an answer on June 3, 2026 at 11:51 pm

    Some problems with your sorted list method:

    ArrayLists are backed by arrays. Whenever you add a new element, it might have to grow the array behind the scenes. If you want to use this approach, you should create the ArrayList of the proper size before hand.

    The sorting after you add all elements seems non-optimal. Why not add elements into the list at their correct position? (Use a sorted collection then turn into a list) A good Sorted List for Java

    To actually answer you question, I would go with the approach that uses the TreeSet behind the scenes. Because, if the user wants, they can always do Set.toArray() and then have a list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have a application that stores data,gets data and processes data and stores
How will I let my c# program to get data from another application? assume
In Java, assume 3 java application running. a) Firstly, My application running b) Secondly,
assume that i start coding an application from scratch, is the best way to
Assume i have a custom Windows application written in C#. This application has only
Assume I have a singleton class in an external lib to my application. But
I am writing a web application on Asp.Net MVC 3. Assume that we have
I'm allowing authenticated users to upload image files with my PHP application. Assume I've
Assume a single application server instance that has two EARs deployed. The first EAR
Let's assume a simple application, which has at the top the following navigation (pseudo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.