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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:51:45+00:00 2026-05-13T21:51:45+00:00

Hi guys i’ve never written a comparator b4 and im having a real problem.

  • 0

Hi guys i’ve never written a comparator b4 and im having a real problem. I’ve created a hashtable.

Hashtable <String, Objects> ht;

Could someone show how you’d write a comparator for a Hashtable? the examples i’ve seen overide equals and everything but i simply dont have a clue. The code below is not mine but an example i found, the key thing in hashtables means i cant do it like this i guess.

 public class Comparator implements Comparable<Name> {
        private final String firstName, lastName;

        public void Name(String firstName, String lastName) {
            if (firstName == null || lastName == null)
                throw new NullPointerException();
        this.firstName = firstName;
            this.lastName = lastName;
        }

        public String firstName() { return firstName; }
        public String lastName()  { return lastName;  }

        public boolean equals(Object o) {
            if (!(o instanceof Name))
                return false;
            Name n = (Name)o;
            return n.firstName.equals(firstName) &&
                   n.lastName.equals(lastName);
        }

        public int hashCode() {
            return 31*firstName.hashCode() + lastName.hashCode();
        }

        public String toString() {
        return firstName + " " + lastName;
        }

        public int compareTo(Name n) {
            int lastCmp = lastName.compareTo(n.lastName);
            return (lastCmp != 0 ? lastCmp :
                    firstName.compareTo(n.firstName));
        }
    }
  • 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-05-13T21:51:45+00:00Added an answer on May 13, 2026 at 9:51 pm

    Comparators are used to sort a list. A Hashtable (note the case) is not ordered by its elements. You can order a table by iterating over its keys (in the case you’d want to order on its keys, I presume) and put them in a List. The next thing to do is to sort the List and iterate over the List, and use a get out of the Hashtable to get its associated value.

    Here is an example (using HashMap, since it’s more integrated with the rest of the Java Collections. A HashMap is essentially the same as Hashtable.):

    public static void main(String... arg) {
        HashMap<String, Object> x = new HashMap<String, Object>();
        x.put("second", " ordered!");
        x.put("first", "Correctly");
    
        LinkedList<String> keys = new LinkedList<String>();
        for(final String f : x.keySet()) {
            keys.add(f);
        }
        Collections.sort(keys, new Comparator<String>() {
            public int compare(String first, String second) {
                // return -1 is "first <  second"
                // return 1  is "first >  second"
                // return 0  is "first == second"
                return first.compareTo(second);
            }
        });
    
        for(final String f : keys) {
            System.out.print(x.get(f));
        }
        System.out.println();
    }
    

    The order of the list keys is sorted by the anonymous Comparator class. It will sort alphabetically, as is the default for Strings. You can use your own key object, like you mentioned. If you don’t implement Comparator in this key object, then you can supply, as in the above example. Else you can use the default Comparator by calling:

    Collections.sort(keys);
    

    Which will use the classes implementation of Comparator. If it does not implement Comparator, then it will throw an exception (since it will cast to a Comparator)

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

Sidebar

Related Questions

guys. I have a strange problem. I try to write unit-tests to web-app. I
guys am having some troubles with running out of memory when displaying an animation
Guys, I've came across this problem I can't resolve myselg, I'm pretty sure I
guys please post me an example for threading in MFC....it should show the progress
Guys I'm new to Android actually, i created an android UI with eclipse, its
Guys, I'm trying to convert something from C# to VB.NET and I'm having trouble
Guys i am facing a problem with wildcard character in jquery. Please help if
Guys, could you please describe TortoiseHg extensions which comes with the installation package? What
Guys, can someone give me a brief run through of how to change the
Guys i m having a table in which i have gender and date columns

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.