Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 查找未排序集合的不同元素的最大/最小值_Java_List_Comparator - Fatal编程技术网

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

Java 查找未排序集合的不同元素的最大/最小值,java,list,comparator,Java,List,Comparator,我有一个国家代码值(可以复制)和相应价格的列表,如主类中所示,我希望通过以下方式找到最大值/最小值: 如果国家代码=0.1,我应该从0.90,0.91,0.92得到它的最大价格=0.92。依此类推,适用于所有其他国家/地区代码 i、 我想找出每个不同国家代码的最高价格 我已经用下面显示的代码成功地完成了它。但这是一种非常缓慢且不好的方法 我的方法是:由于“类Main”中的数据是相关的(国家代码,价格),我首先使用带有比较器的类“phone”按国家代码对数据进行排序,然后扫描“phone Arra

我有一个国家代码值(可以复制)和相应价格的列表,如主类中所示,我希望通过以下方式找到最大值/最小值:

如果国家代码=0.1,我应该从0.90,0.91,0.92得到它的最大价格=0.92。依此类推,适用于所有其他国家/地区代码 i、 我想找出每个不同国家代码的最高价格

我已经用下面显示的代码成功地完成了它。但这是一种非常缓慢且不好的方法

我的方法是:由于“类Main”中的数据是相关的(国家代码,价格),我首先使用带有比较器的类“phone”按国家代码对数据进行排序,然后扫描“phone ArrayList”的所有元素,然后通过比较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());
        }
    }   
}
类电话机实现比较器{
私人国际编码;
私人双价;
电话(){
}
电话(INTC,双p){
countryCode=c;
价格=p;
}
public int getCountryCode(){
返回国家代码;
}
公开双价{
退货价格;
}
//重写compare方法以进行排序
公共int比较(电话d、电话d1){
返回d.getCountryCode()-d1.getCountryCode();
}
}
公共类主{
/**                                     
*@param args
*/
公共静态void main(字符串[]args){
//获取电话对象的列表
ArrayList=新建ArrayList();
ArrayList arr=新的ArrayList();
增加(新电话(1,0.9));
增加(新电话(268,5.1));
增加(新电话(46,0.17));
增加(新电话(46,0.01));
增加(新电话(4631,0.15));
增加(新电话(4620,0.0));
增加(新电话(468,0.15));
增加(新电话(46,0.02));
增加(新电话(4673,0.9));
增加(新电话(46732,1.1));
增加(新电话(1,0.91));
增加(新电话(44,0.4));
增加(新电话(92,0.4));
增加(新电话(467,0.2));
增加(新电话(4,0.0001));
增加(新电话(1,0.92));
增加(新电话(44,0.5));
增加(新电话(467,1.0));
增加(新电话(48,1.2));
增加(新电话(4,0.1));
Collections.sort(列表,新电话());
对于(int i=0;i
您无需事先进行任何排序即可完成此操作,因此您只需在集合中进行一次迭代,并为哈希映射插入时间

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());
    }
}
集合列表=。。。;
Map maximumPrices=新HashMap();
(电话:列表){
Double checkPrice=maximumPrices.get(telephone.getCountryCode());
if(checkPrice==null | | checkPrice

我将为您添加记录最低价格的支持作为练习。

您可以让电话实施,如果国家代码相等,您可以在比较中计算您的价格。如果你对列表进行排序,你会先按contrycode排序,然后按价格排序

然后,您可以重复您的电话通话,并保留每次通话的控制码。您迭代的countrycode的最后一个元素将是价格最高的元素。这样,您可以一步完成国家代码比较和价格比较

 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;    
 }
公共内部比较(电话d){
int cc1=this.getCountryCode();
int cc2=d.getCountryCode();
如果(cc1==cc2){
double price 1=this.getPrice();
双倍价格2=d.getPrice();
如果(价格1<价格2)
返回-1;
如果(价格1>价格2)
返回1;
返回0;
}
返回cc1-cc2;
}

如果我是你,我会像下面那样处理地图

// 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);
//县和物价指数的映射
Map telephones=newhashmap();
//正在搜索国家/地区代码0.1
标价=电话。获取(“0.1”);
//查找最大值和最小值
双倍最大值=集合。最大值(价格);
双倍最小值=集合.min(价格);

我认为最好的实施取决于您的目标

a) 如果你有一个或多或少是静态的一套电话,这是经常调用,你应该尝试设置最小值,最大值时,添加一个新的电话

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

对于a)您可以使用HashMap,其中键是国家代码

 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)