简体   繁体   English

Java:如何获得文件中的最高和最低价格

[英]Java: how to get highest & lowest vale in a file

Problem solved 问题解决了

I would like to find the highest and lowest value from a file. 我想从文件中找到最高和最低值。 I have converted the figures in a file from String to double. 我已经将文件中的数字从String转换为double。 How can I do if I would like to use Get method to find the highest and lowest amount? 如果我想使用Get方法查找最高和最低金额怎么办?

Edit: I complete made a mess of the original answer, as I didn't realise Strings would be compared lexicographically by using the standard Collections() class methods. 编辑:我完全弄乱了原来的答案,因为我没有意识到使用标准的Collections()类方法可以按字典顺序比较Strings

So the best way to do this using Collections() is to create a custom Comparator 因此,使用Collections()做到这一点的最佳方法是创建一个自定义的Comparator

Comparator<String> sort = (o1, o2) -> Double.valueOf(o1).compareTo(Double.valueOf(o2));
String max = Collections.max(numList, sort);
String min = Collections.min(numList, sort);

Try it online! 在线尝试!

you should put data into arraylist while reading from the file. 您应该在从文件读取数据时将数据放入arraylist。 and then start sorting the numbers using merge or quick. 然后开始使用merge或quick对数字进行排序。

i'm using inbuild Collections.sort() method. 我正在使用inbuild Collections.sort()方法。

package com.collections.java.basic;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortingDataWhileReading {
    public static void main(String[] args) throws IOException {
          BufferedReader br = new BufferedReader(new FileReader("E:\\BUILD\\numbers.txt"));//this file contains several double data.
            List<Double> numbers = new ArrayList<Double>();
            String line = null;

             //String line = br.readLine();

             while ((line = br.readLine()) != null) {
                 String []strNumbers = line.split(" ");
                 for(String strNumber : strNumbers){
                     numbers.add((double) Double.parseDouble(strNumber));
                 }

             }   

             br.close();

             Collections.sort(numbers);

             System.out.println("minimum value" + numbers.get(0));
             System.out.println("minimum value" + numbers.get(numbers.size() - 1));

             System.out.println(numbers);
    }
}

This should solve your problem. 这应该可以解决您的问题。

Double higher = 0;
Double lower = 0;
for (String item:numList)
        { 
            Double result = Double.parseDouble(item);
            if (result>higher) { higher = result;}
            if (result<lower) { lower = result;}
            //Calculate the total sales for each week
            total += result;
        }
public static double getMin(List<String> list) {
    double min = Double.MAX_VALUE;
    for (String s : list) {
        double d = Double.valueOf(s);
        min = min > d ? d : min;
    }
    return min;
}

public static double getMax(List<String> list) {
    double max = Double.MIN_VALUE;

    for (String s : list) {
        double d = Double.valueOf(s);
        max = max < d ? d : max;

    }
    return max;
}

Java8 example: Java8示例:

public static double getMinJ8(List<String> list) {
    return list.stream().map(s -> Double.valueOf(s)).min((d1, d2) -> d1.compareTo(d2)).get();
}
public static double getMaxJ8(List<String> list) {
    return list.stream().map(s -> Double.valueOf(s)).max((d1, d2) -> d1.compareTo(d2)).get();
}

store all the values in a treeset so all the elements will be sorted. 将所有值存储在树集中,以便对所有元素进行排序。 Now use first and last 现在使用第一个最后一个

sample code 样例代码

    TreeSet s=new TreeSet();
    //add elements to the set
    s.add(100);
    s.add(200);
    s.add(1);
   // use first() method to get the lowest
    System.out.println(s.first());
   //use last method to get the highest
    System.out.println(s.last());

output : 1 输出 1

200 200

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM