查找未排序集合的不同元素的最大/最小值

时间:2013-01-26 13:36:40

标签: java list comparator

我有一个国家/地区代码值列表(可以复制)和相应的价格,如Class Main所示,我希望以这种方式找到最大/最小值:

如果国家代码= 0.1,我应该从0.90,0.91,0.92得到最大价格= 0.92。等所有其他国家代码 即我想找到每个不同国家代码的最高价格

我已经在下面显示的代码中成功完成了。但这是非常缓慢而且不是很好的方法。

我的方法:作为" Class Main"中的数据。是相关的(国家代码,有价格),我首先按国家代码使用Class" Telephone"与比较器,然后我扫描"的所有元素电话的ArrayList"然后我找到每个" Distinct"的最大值。国家代码与ArrayList元素的比较。

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());
        }
    }   
}

4 个答案:

答案 0 :(得分:2)

您可以在不预先进行任何排序的情况下执行此操作,因此您的唯一成本将是对集合的一次迭代以及哈希映射的插入时间。

Collection<Telephone> list = ...;

Map<Integer, Double> maximumPrices = new HashMap<Integer, Double>();

for(Telephone telephone : list) {
    Double checkPrice = maximumPrices.get(telephone.getCountryCode());
    if(checkPrice == null || checkPrice < telephone.getPrice()) {
        maximumPrices.put(telephone.getCountryCode(), telephone.getPrice());
    }
}

我会留下支持记录最低价格作为练习。

答案 1 :(得分:1)

您可以让Telephone实施Comparable,如果国家/地区代码相同,您可以在比较中计算您的价格。如果您对列表进行排序,则首先按contrycode排序,然后按价格排序。

然后,您可以遍历您的电话集合并保留每个交互的contrycode。您迭代的国家/地区代码的最后一个元素将是价格最高的元素。这样,您就可以一步完成国家/地区代码比较和价格比较。

 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;    
 }

答案 2 :(得分:1)

我认为最佳实施取决于您所追求的目标。

a)如果你经常调用一组或多或少的静态手机,你应该尝试在添加新的电话时设置最小值,最大值。

b)如果您经常添加电话,只有在需要时才能获得最大最大值。

表示a)您可以使用HashMap,其中key是国家/地区代码。

 HashMap<Integer,ArrayList<float>> telefonMap = new HashMap<Integer,ArrayList<float>();
 addtelephone(int coutrycode, float price ){
  if (telefonMap.contains(countrycode){
     telefonMap.get(contrycode).add(price);
     // if you have frequent min max requests add, else skip the next line
     Collections.sort(telefonMap.get(countrycode));
  }
  else {
     ArrayList<Float> tempList = new ArrayList<Float>();
     priceList.add(price);
     telefonMap.put(contrycode,tempList);
 }
}

现在你可以通过调用

获得最小的最大元素
telephoneMap.get(countrycode).get(0 or telephoneMap.get(countrycode).size()-1)

如果您在添加手机时没有对阵列进行排序,只需在此处调用阵列并对其进行排序

答案 3 :(得分:0)

如果我是你,我会使用以下地图。

// Mapping of counties and lis of prices
Map<String, List<Double>> telephones = new HashMap<String, List<Double>>();

//Searching for country code 0.1
List<Double> prices = telephones.get("0.1");

//Finding Max and Min
Double maximum = Collections.max(prices);
Double minimum = Collections.min(prices);