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 8950243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:24:41+00:00 2026-06-15T13:24:41+00:00

I got everything else to work except the sort method. I need to sort

  • 0

I got everything else to work except the sort method. I need to sort the Students in the HashMap based on the Student’s first attribute. I need the sort method to happen after I added all of the students in the HashMap, not while it’s being added.

package HashMap;

        import java.io.InputStream;
        import java.util.HashMap;
        import java.util.HashSet;
        import java.util.Scanner;

        public class clubMapping {
            HashMap<String, HashSet<Student>> map = new HashMap<String, HashSet<Student>>();

            public clubMapping(String clubName) {
                InputStream is = getClass().getClassLoader().getResourceAsStream(
                        "student.txt");
                Scanner scan = new Scanner(is);
                while (scan.hasNext())
                    add(scan.next(), scan.next(), Integer.parseInt(scan.next()),
                            scan.next());
                scan.close();
                System.out.println(map);
                System.out.println();
                System.out.println(map.get(clubName));
            }

            public void add(String last, String first, Integer id, String club) {
                HashSet<Student> set = new HashSet<Student>();
                if (!map.containsKey(club)) {
                    set.add(new Student(last, first, id));
                    map.put(club, set);
                } else {
                    set = map.get(club);
                    set.add(new Student(last, first, id));
                }
            }

            public static void main(String[] args) {
                new clubMapping("Math");
            }
        }

    package HashMap;

    public class Student {
        String last, first;
        Integer id;

        public Student(String l, String f, Integer i) {
            last = l;
            first = f;
            id = i;
        }

        public String toString() {
            return last + " " + first + " " + id;
        }
    }
  • 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-15T13:24:42+00:00Added an answer on June 15, 2026 at 1:24 pm

    You should use a TreeSet instead of a HashSet for the student set. A tree set is implicitly sorted, a hash set is not.

    public void add(String last, String first, Integer id, String club) {
        TreeSet<Student> set = new TreeSet<Student>(studentComp);
        if (!map.containsKey(club)) {
            set.add(new Student(last, first, id));
            map.put(club, set);
        } else {
            set = map.get(club);
            set.add(new Student(last, first, id));
        }
    }
    

    But you must define a comparator to use for the sorting, as shown below.

    private static Comparator<Student> studentComp = new Comparator<Student>() {
        @Override
        public int compare(Student s1, Student s2) {
            return (s1.last.compareTo(s2.last));
        }
    };
    

    Also, you should probably encapsulate your student attributes in getter/setter methods, rather than accessing them directly as I’ve done above (as your class currently allows).

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

Sidebar

Related Questions

I got EVERYTHING, but this working. I can't get the move_uploaded_file(); to work. $ups_path
I've setup a simple TCP file transfer. Everything appears to work OK, except for
I've got everything finished for the program, but it's telling me that the else
Everything pretends to work great, except when I Search for anything I get no
I've got everything setup and working for MQTT now. I am using the IBM
Boy, I've got everything down-pat save the word referencing. The game crashes because of
I'm following the Django-CMS introductory tutorial and have got everything working up to the
I got following code from net and it looks everything is proper but i'm
I've got a view in my app that does pretty much everything, and I
I installed gvim on windows vista and got really excited to edit EVERYTHING with

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.