Java 使用属性上的条件重新排列对象列表(不是升序/降序)

Java 使用属性上的条件重新排列对象列表(不是升序/降序),java,collections,Java,Collections,我有一个对象列表(比如说Office对象),它的属性比如说id、名称、位置、强度。现在,假设字段位置具有某个集合中的值(例如3个可能的值,纽约、加利福尼亚、犹他)。现在,我想在location属性上重新排列此列表,使具有location属性的Office对象(如纽约)排在列表的第一位,其次是加利福尼亚州的对象,然后是犹他州的对象。所以,基本上这不是升序或降序,而是我为这个属性设置的标准。在Java中实现这一点最有效的方法是什么 我第一次想到使用Comprator,但我不想简单地按升序/降序排序,

我有一个对象列表(比如说Office对象),它的属性比如说id、名称、位置、强度。现在,假设字段位置具有某个集合中的值(例如3个可能的值,纽约、加利福尼亚、犹他)。现在,我想在location属性上重新排列此列表,使具有location属性的Office对象(如纽约)排在列表的第一位,其次是加利福尼亚州的对象,然后是犹他州的对象。所以,基本上这不是升序或降序,而是我为这个属性设置的标准。在Java中实现这一点最有效的方法是什么


我第一次想到使用Comprator,但我不想简单地按升序/降序排序,而是按照上面提到的我指定的标准进行排序。不确定,我是否也可以使用比较器,如果可以,如何使用。如果没有,那么另一种最有效的方法是什么?

简单,不能排除,理想情况下需要包含所有位置:

public static void rearrangeSort(List<Office> source, String... locations) {
    List<String> locs = Arrays.asList(locations);
    Collections.sort(source, (o1, o2) -> locs.indexOf(o1.getLocation()) - locs.indexOf(o2.getLocation()));
}
公共静态排序(列出源、字符串…位置){
列表LOC=数组。asList(位置);
Collections.sort(source,(o1,o2)->locs.indexOf(o1.getLocation())-locs.indexOf(o2.getLocation());
}
更复杂的是:

public static List<Office> rearrange(List<Office> source, String... locations) {
    Map<String, List<Office>> map = new HashMap<>();
    for (Office office : source) {
        List<Office> lst = map.get(office.getLocation());
        if (lst == null) {
            lst = new ArrayList<>();
            map.put(office.getLocation(), lst);
        }
        lst.add(office);
    }
    List<Office> resultList = new ArrayList<>();
    for (String loc : locations) {
        List<Office> partial = map.get(loc);
        if (partial != null) {
            resultList.addAll(partial);
        }
    }
    return resultList;
}
公共静态列表重新排列(列表源、字符串…位置){
Map Map=newhashmap();
for(办公室:来源){
List lst=map.get(office.getLocation());
如果(lst==null){
lst=新的ArrayList();
map.put(office.getLocation(),lst);
}
第一地址(办公室);
}
List resultList=new ArrayList();
用于(字符串位置:位置){
列表部分=映射获取(loc);
如果(部分!=null){
结果列表addAll(部分);
}
}
返回结果列表;
}
lambdas的早期版本:

public static List<Office> rearrange2(List<Office> source, String... locations) {
    Map<String, List<Office>> map = source.stream().collect(Collectors.toMap(
            Office::getLocation,
            o -> {List<Office> lst = new ArrayList<>(); lst.add(o); return lst;},
            (offices, offices2) -> {offices.addAll(offices2); return offices;}));
    return Arrays.asList(locations).stream()
            .filter(map::containsKey)
            .map(map::get)
            .flatMap(Collection::stream)
            .collect(Collectors.toList());
}
公共静态列表2(列表源、字符串…位置){
Map Map=source.stream().collect(Collectors.toMap(
办公室:getLocation,
o->{List lst=new ArrayList();lst.add(o);返回lst;},
(offices,offices2)->{offices.addAll(offices2);returnoffices;});
返回数组.asList(locations).stream()
.filter(映射::containsKey)
.map(map::get)
.flatMap(集合::流)
.collect(Collectors.toList());
}

集合。sort
可以有一个额外的比较器,您可以对其进行多次或动态比较。Java8允许非常紧凑的表示法,甚至可以组合比较器。我用1。7@user1892775将lambda替换为:new Comparator(){public int compare(Office o1,Office o2){return locs.indexOf(o1.getLocation())-locs.indexOf(o2.getLocation();}}