根据Java中列表中的最小属性值选择一个对象

时间:2015-04-17 04:22:02

标签: java arraylist

我有一个班级

public class LocationDistances {
    String locs;
    double distances;

}

和由另一个类中的LocationDistances对象组成的ArrayList

ArrayList<LocationDistances> locsDis = new ArrayList(Arrays.asList(
    new LocatoinDistances("abc",23.234556),
    new LocatoinDistances("xyz",3.3444566),
    . . . 
    . . .
    . . .

));

如何根据locsDis arrayList中LocationDistances.distance的最小值从数组列表中选择LocationDistances.locs?

6 个答案:

答案 0 :(得分:6)

如果您使用的是Java 8(应该:) :):

locsDis.stream().min((first, second) -> Double.compare(first.distance, second.distance)).get();

编辑:决定让这篇文章更加全面,以帮助那些仍然局限于Java < 8但可以使用真棒库番石榴的人:)

Java 6&amp; 7使用guava(Google util库):

Ordering<LocationDistances> ordering = new Ordering<LocationDistances>() {
    @Override
    public int compare(LocationDistances left, LocationDistances right) {
        return Double.compare(left.distance, right.distance);
    }
};
return ordering.max(list);

答案 1 :(得分:5)

如果没有重复的距离,这样的东西就会起作用。

LocationDistances min=null;
for(LocationDistances x:locsDis){
    min=(min==null||x.distances<min.distances)?x:min;
}
String minimumDistance=min.locs;

如果有重复的距离,请使用类似

的内容
ArrayList<LocationDistances> min=new  ArrayList<LocationDistances>();

for(LocationDistances x:locsDis){
    if(min.size()==0||x.distances==min.get(0).distances)
           min.add(x);
    else if(x.distances<min.get(0).distances){
           min.clear();
           min.add(x);
    }               
}

答案 2 :(得分:1)

将Priority Queue与自定义Comparator一起使用。 (使用poll / peek获取最小值)

public class MyComparator implements Comparator<LocationDistance>
{
  @Override
  public int compare(LocationDistance ld1, LocationDistance ld2)
  {
    return  Double.compare(ld1.distances, ld2.distances);
   }
}
......
......
PriorityQueue collection = new PriorityQueue<LocationDistance>(5, new MyComparator());

答案 3 :(得分:0)

@David Limkys是对的,这将是Java 8中的解决方案,适用于Java 6&amp; 7后面会这样做。

public class MainCLass {

    /**
     * 
     */
    public MainCLass() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) {

        LocationDistances[] arr = new LocationDistances[] {
                new LocationDistances("abc", 23.234556),
                new LocationDistances("xyz", 3.3444566), };

        ArrayList<LocationDistances> locsDis = new ArrayList(Arrays.asList(arr));

        Collections.sort(locsDis, new Comparator<LocationDistances>() {
            public int compare(LocationDistances o1, LocationDistances o2) {
                return (int)( o1.distances - o2.distances);
            }
        });

    }

}

class LocationDistances {
    String locs;
    double distances;

    /**
     * @param locs
     * @param distances
     */
    public LocationDistances(String locs, double distances) {
        super();
        this.locs = locs;
        this.distances = distances;
    }

    @Override
    public String toString() {
        return String.format("LocationDistances [locs=%s, distances=%s]", locs,
                distances);
    }

}

答案 4 :(得分:0)

您可以在LocationDistances上实现Comparable,并使用比较器按比例对ArrayList进行排序。

public class LocationDistances implements Comparable {
    String locs;
    double distances;

    @Override
    public int compareTo(LocationDistances anotherLocation) {
        double otherLocDistances = ((LocationDistances)anotherLocation).getDistances();

        /* ascending order */
        return this.distances - otherLocDistance;

        /* descending order */
        //return otherLocDistance - this.distances;
    }
}

然后致电:

ArrayList<LocationDistances> locsDis = new ArrayList(Arrays.asList(
    new LocationDistances("abc",23.234556),
    new LocationDistances("xyz",3.3444566),
    . . . 
    . . .
    . . .

));
Collections.sort(locsDis);

答案 5 :(得分:0)

这就是你要找的东西:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;


public class Run {
    public static void main(String[]args){
        ArrayList<LocationDistances> locsDis = new ArrayList(Arrays.asList(
                new LocationDistances("abc",23.234556),
                new LocationDistances("xyz",3.3444566)));

        ArrayList<Double> distances = new ArrayList<Double>();
        for (LocationDistances locationDistances : locsDis) {
            distances.add(locationDistances.getDistances());
        }
        double minimum = Collections.min(distances);
        System.out.println(minimum);
    }
}

class LocationDistances  {

    public LocationDistances(String locs, double distances) {
        this.locs = locs;
        this.distances = distances;
    }
    String locs;
    double distances;
    public String getLocs() {
        return locs;
    }
    public void setLocs(String locs) {
        this.locs = locs;
    }
    public double getDistances() {
        return distances;
    }
    public void setDistances(double distances) {
        this.distances = distances;
    }

}