简体   繁体   中英

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. How can I do if I would like to use Get method to find the highest and lowest amount?

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.

So the best way to do this using Collections() is to create a custom 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. and then start sorting the numbers using merge or quick.

i'm using inbuild Collections.sort() method.

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:

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

200

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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