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

  • Home
  • SEARCH
  • 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 9188539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:02:00+00:00 2026-06-17T20:02:00+00:00

I hope it is not repeating question, I made two Classes, Dog and Ser,

  • 0

I hope it is not repeating question,

I made two Classes, “Dog” and “Ser”,

“Dog” Class uses a comparator to sort the price first. (Done correctly)
“Ser” Class should add sorted price list (as values) + (unrepeated country code as “Keys” in the map . which i have correctly to me.

In my information, Map should sort the keys in Ascending order explicitly, but this is not the case, see output.

My Goal is now to have sorted country code in the Class Ser with corresponding values of price

class Dog implements Comparator<Dog>, Comparable<Dog> {
    private int CountryCode;
    private double Price;
    private String Operator;

    Dog() {
    }

    Dog(int c, double p, String o) {
        CountryCode = c;
        Price = p;
        Operator = o;
    }

    public int getCountryCode() {
        return CountryCode;
    }

    public double getPrice() {
        return Price;
    }

    public String getOperator() {
        return Operator;
    }

    // Overriding the compareTo method
    public int compareTo(Dog d) {

        double de = Price - d.getPrice();

        if (de > 0.00001)
            return 1;
        if (de < 0.00001)
            return -1;
        return 0;

    }

    // Overriding the compare method to sort by price
    public int compare(Dog d, Dog d1) {

        double de = d.getPrice() - d1.getPrice();

        if (de > 0.00001)
            return 1;
        if (de < 0.00001)
            return -1;
        return 0;
    }

    @Override
    public String toString() {
        return "  " + Price + ":" + Operator + "";
    }

}

public class Ser {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // Takes a list o Dog objects

        ArrayList<Dog> list1 = new ArrayList<Dog>();
        Map<Integer, Dog> mp = new HashMap<Integer, Dog>();

        list1.add(new Dog(1, 0.9, "A"));
        list1.add(new Dog(268, 5.1, "A"));
        list1.add(new Dog(46, 0.17, "A"));
        list1.add(new Dog(4620, 0.0, "A"));
        list1.add(new Dog(468, 0.15, "A"));
        list1.add(new Dog(4631, 0.15, "A"));
        list1.add(new Dog(4673, 0.9, "A"));
        list1.add(new Dog(46732, 1.1, "A"));

        list1.add(new Dog(1, 0.92, "B"));
        list1.add(new Dog(44, 0.5, "B"));
        list1.add(new Dog(46, 0.02, "B"));
        list1.add(new Dog(467, 1.0, "B"));
        list1.add(new Dog(48, 1.2, "B"));

        list1.add(new Dog(1, 0.9, "c"));
        list1.add(new Dog(44, 0.4, "c"));
        list1.add(new Dog(46, 0.01, "c"));
        list1.add(new Dog(467, 0.2, "c"));

        // Sorts the array list using comparator

        Collections.sort(list1, new Dog());

        System.out.println("Sort the Dog class with respect to Price");

        for (Dog a : list1)
            // printing the sorted list of ages
            System.out.println(a.getCountryCode() + "  : " + a.getPrice()
                    + "  : " + a.getOperator());

        // Add only those keys from sorted Price but if the key is added, don't
        // repeat it
        for (int i = 0; i < list1.size(); i++) {
            if (!mp.containsKey(list1.get(i).getCountryCode()))
                mp.put(list1.get(i).getCountryCode(), list1.get(i));

        }

        System.out.println("");

        System.out.println(" Get Sorted List of Keys and values");

        for (Map.Entry<Integer, Dog> e : mp.entrySet()) {

            System.out.println(e.getKey() + "" + e.getValue());

        }

    }
}

Output: Sort the Dog class with respect to Price

4620  : 0.0  : A,  46  : 0.01  : c,  46  : 0.02  : B,  4631  : 0.15  : A,  468  : 0.15  : A,  46  : 0.17  : A,  467  : 0.2  : c,  44  : 0.4  : c,  44  : 0.5  : B,  1  : 0.9  : c,  4673  : 0.9  : A,  1  : 0.9  : A,  1  : 0.92  : B,  467  : 1.0  : B, 46732  : 1.1  : A, 48  : 1.2  : B, 268  : 5.1  : A

Get Sorted List of Keys and corresponding values

4673  0.9:A, 1  0.9:c, 46732  1.1:A, 48  1.2:B, 4631  0.15:A, 4620  0.0:A, 468  0.15:A, 46  0.01:c, 467  0.2:c, 268  5.1:A, 44  0.4:c
  • 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-17T20:02:02+00:00Added an answer on June 17, 2026 at 8:02 pm

    Here’s the problem:

    Map <Integer, Dog> mp=  new HashMap  <Integer, Dog> ();
    

    HashMap does not sort its elements into any order.
    Try it with TreeMap instead. TreeMap has a constructor which takes a Comparator.

    http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html

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

Sidebar

Related Questions

I hope I am not repeating any previously asked question. Anyway, so Google Wave
First and foremost, apologies for any cross-posting. Hope I'm not repeating an issue here,
I hope I'm not repeating an existing question but I'm having some issues retaining
I really hope this is not a question posed by millions of newbies, but
Hope this question is not stupid since I am an amateur web designer. I
I hope this question is not 'controversial' - I'm just basically asking - has
I hope I am not repeating anyone here, but I have been searching google
I hope I'm not repeating - it doesn't seem like there are any questions
Hope this is not a dupe. I would like to be able to do
I hope I'm not duplicating here :-) I'm wondering what's the most effective way

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.