简体   繁体   中英

How to find a maximum value of an index in a list of lists

I have a list of lists of objects, with each single innerlist having 3 Object elements, theoretically a String and two doubles,say a and b , in that order.

ArrayList<ArrayList<Object>> timings = new ArrayList<ArrayList<String>>()
        for (int runnerno = 0; runnerno < runners; runnerno++) {
            ArrayList<Object> thisrunner = new ArrayList<Object>();
            thisrunner.add(sc.next()); //string
            thisrunner.add(sc.nextDouble()); //double a 
            thisrunner.add(sc.nextDouble()); //double b
            timings.add(thisrunner);
            sc.nextLine();
        }

How do I find out the maximum a value in my list of lists? ie. I want to find an indexed maximum.

1) Lets make a better encapsulation of your data object, call it FooBar

public class FooBar {
    private String text;
    private Double x;
    private Double y;

    public FooBar() {

    }

    public FooBar(String text,Double x,Double y) {
        this.text = text;
        this.x = x;
        this.y = y;
    }

    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    public Double getX() {
        return x;
    }
    public void setX(Double x) {
        this.x = x;
    }
    public Double getY() {
        return y;
    }
    public void setY(Double y) {
        this.y = y;
    }
}

2) Populate a list of FooBars

    List<FooBar> points = new ArrayList<FooBar>();

    for( int i = 0; i < 1000; i++ ) {
        FooBar f = new FooBar("Text" + i,
        ThreadLocalRandom.current().nextDouble(0, 100),
        ThreadLocalRandom.current().nextDouble(0, 100));
        points.add(f);
    }

3) Use streams.max (with Comparator)

    Optional<FooBar> maxFooBar = points.stream().max(new Comparator<FooBar>() {

        @Override
        public int compare(FooBar o1, FooBar o2) {
            return o1.getX().compareTo(o2.getX());
        }
    });

    System.out.println("Max Point: " + maxFooBar.get().getX());

4) Or use Collections.max

    FooBar maxFooBar = Collections.max(points, new Comparator<FooBar>() {

        @Override
        public int compare(FooBar o1, FooBar o2) {
            return o1.getX().compareTo(o2.getX());
        }
    });
    System.out.println("Max Point: " + maxFooBar.getX());

5) Or just sort the list yourself and get the first item (if sorted Descending; get last if Ascending)

    points.sort(new Comparator<FooBar>() {

        @Override
        public int compare(FooBar o1, FooBar o2) {
            return -1 * o1.getX().compareTo(o2.getX());
        }
    });
    System.out.println("Max Point: " + points.get(0).getX());

The maximum double value of all values that are of type Double in the list of lists, like so

public static double findMax(List<List> lists) {
    double max = Double.MIN_VALUE;
    for (List list : lists)
        for (Object o : list)
            if (o instanceof Double)
                if ((Double) o).doubleValue() > max)
                   max = ((Double) o).doubleValue();
    return max;
}

what if you use Comparator interface for sorting your each individual Runner object in ascending order and the last Runner object inside timings treeSet would be always the index of maximum value

we cannot use the ArrayList since it does not have any constructor which support Comparator as an argument

public class Runner {
     private String s;
     public double a;
     public double b;

     public Runner() {}
     public Runner(String s, double a, double b) {
         this.s = s;
         this.a = a;
         this.b = b;
     }

     @Override
     public String toString() {
         return s + " " + a + " " + b;
     }
}

calling class

 import java.util.Comparator;
 import java.util.Scanner;
 import java.util.TreeSet;

 public class Calling {

      public static void main(String[] args) {
           Scanner sc = new Scanner(System.in);
           int runners = 3;
           TreeSet<Runner> timings = new TreeSet<>(new MyComparator());

           for (int runnerno = 0; runnerno < runners; runnerno++) {
                timings.add(new Runner(sc.nextLine(), Double.parseDouble(sc.nextLine()), Double.parseDouble(sc.nextLine())));               
           }
      System.out.println(timings);
      System.out.println("max value " + timings.last());
      }

 }

class MyComparator implements Comparator<Runner> {
    @Override
    public int compare(Runner o1, Runner o2) {
        return Double.valueOf(o1.a).compareTo(Double.valueOf(o2.a));
   }
}

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