[英]How to find a maximum value of an index in a list of lists
我有一個對象列表列表,每個內部列表都有 3 個對象元素,理論上是一個 String 和兩個雙打,比如a和b ,按順序排列。
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();
}
如何找出最大的在我的名單列表的價值? IE。 我想找到一個索引最大值。
1) 讓我們更好地封裝你的數據對象,稱之為 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) 填充 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)使用streams.max(帶比較器)
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) 或者使用 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)或者只是自己對列表進行排序並獲得第一項(如果排序降序;如果升序則獲得最后一項)
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());
列表列表中所有類型為 Double 的值的最大 double 值,如下所示
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;
}
如果您使用 Comparator 接口按升序對每個單獨的 Runner 對象進行排序,並且 Timings treeSet 中的最后一個 Runner 對象將始終是最大值的索引,該怎么辦
我們不能使用 ArrayList 因為它沒有任何支持 Comparator 作為參數的構造函數
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;
}
}
調用類
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));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.