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

Ask A Question

Stats

  • Questions 315k
  • Answers 315k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To center an absolutely positioned element you set its CSS… May 13, 2026 at 11:15 pm
  • Editorial Team
    Editorial Team added an answer The road to success will involve doing what the assignment… May 13, 2026 at 11:15 pm
  • Editorial Team
    Editorial Team added an answer Never mind, found the answer: area.Axes[n].MajorGrid.LineColor = Color.Whatever; May 13, 2026 at 11:15 pm

Related Questions

Hi guys I wrote this code and i have two errors. Invalid rank specifier:
hi guys i have an error with sqldatasource. When i click sqldatasource's updatequery property
Hi guys I'm trying to add a css footer but can't it to work
Hi guys I have this in PowerShell: I have a collection that have a
Hi guys I wish to get information for entries I have in my database

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.