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

The Archive Base Latest Questions

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

I have a list of Country code values (which can be duplicated) and corresponding

  • 0

I have a list of Country code values (which can be duplicated) and corresponding prices as shown in the Class Main, i want to find max/min in this way:

if Country Code = 0.1 , i should get it max price= 0.92 from 0.90, 0.91, 0.92. and so on for all other country codes
i.e. i want to Find max price for each distinct Country codes

i have done it successfully in the code shown below. But it is very slow and not good approach.

My Method: As the data in “Class Main” is related (country code, with price), i first sort the data by country code using Class “Telephone” with Comparator, and then i scan all the elements of the ” Telephone-ArrayList” and then i find the max value for each “Distinct” Country Code with comparison of ArrayList elements.

class Telephone implements Comparator<Telephone>{
    private int countryCode; 
    private double price;

    Telephone(){
    }

    Telephone( int c, double p){
        countryCode= c;
        price= p;

    }

    public int getCountryCode(){
        return countryCode;
    }

    public double getPrice(){
        return price;
    }


    // Overriding the compare method to sort
    public int compare(Telephone d, Telephone d1){

        return d.getCountryCode() - d1.getCountryCode();    
    }

}


public class Main {                          
    /**                                     
    * @param args
    */
    public static void main(String[] args) {
        // Takes a list o Telephone objects
        ArrayList <Telephone> list = new ArrayList<Telephone>(); 
        ArrayList <Double> arr = new ArrayList<Double>(); 
        list.add(new Telephone(1, 0.9));
        list.add(new Telephone(268, 5.1 ));
        list.add(new Telephone(46, 0.17 ));
        list.add(new Telephone(46, 0.01));
        list.add(new Telephone(4631, 0.15 ));
        list.add(new Telephone(4620, 0.0 ));
        list.add(new Telephone(468, 0.15 ));
        list.add(new Telephone(46, 0.02));
        list.add(new Telephone(4673, 0.9));
        list.add(new Telephone(46732,1.1));



        list.add(new Telephone(1, 0.91 ));
        list.add(new Telephone(44, 0.4 ));
        list.add(new Telephone(92, 0.4 ));
        list.add(new Telephone(467, 0.2 ));
        list.add(new Telephone(4, 0.0001 ));

        list.add(new Telephone(1, 0.92 ));
        list.add(new Telephone(44, 0.5 ));

        list.add(new Telephone(467, 1.0 ));
        list.add(new Telephone(48, 1.2 ));
        list.add(new Telephone(4, 0.1));

        Collections.sort(list, new Telephone());

        for ( int i=0; i < list.size()-1; i++)
        {

            arr.clear();

            while ( list.get(i).getCountryCode()== list.get(i+1).getCountryCode())
            {

              arr.add(list.get(i).getPrice()) ;
              i=i+1;

            }
            arr.add(list.get(i).getPrice());

            arr.trimToSize();

            System.out.println( " Max value is " + Collections.max(arr).toString() + " for " +list.get(i).getCountryCode());
        }
    }   
}
  • 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:47:52+00:00Added an answer on June 17, 2026 at 8:47 pm

    You could let Telephone implement Comparable and if the counrycodes are equal you could calculate your price into the comparison. If you sort your list then, you will have it sorted by contrycode first and then by price.

    You can then iterate over your Telephone colection and keep the contrycode for each interation. The last element of a countrycode you iterated over will be the one with the highest price. This way you can do the country code comparison and price comparison in one step.

     public int compareTo(Telephone d){
        int cc1 = this.getCountryCode();
        int cc2 = d.getCountryCode();
    
        if(cc1 == cc2){
          double price1 = this.getPrice();
          double price2 = d.getPrice();          
          if(price1 < price2)
             return -1;
          if(price1 > price2)
             return 1;
          return 0;
        }
    
        return cc1 - cc2;    
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have html drop down list,which has country list. Now I want to set
I have List I want to sort Desc by Priority, which is int and
I have the following code to parse the country when the autocomplete list is
Let's say we have a code list of all the countries including their country
I have a list which looks like the following: <ul id=offers> <li class=unique-id-here> <span
I have a drop-down list containing classifications of currencies which can be instantiated as
I have been struggling to create a Dropdown list which will display Country names
I have a piece of code which creates dropdown list of the countries <div
I have tried to serialize the class which contains the list of objects but
I have a list called Countries, and each country has a list of Towns,

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.