Java对特定索引进行排序

huangapple go评论56阅读模式
英文:

Java Sort particular index

问题

以下是您提供的代码的翻译部分:

我有一个模型

public class HotelSort {

    public static void main(String args[]){
        List<Hotel> hotels = new ArrayList<>();
        hotels.add(new Hotel("A", 2, 50));
        hotels.add(new Hotel("B", 4, 30));
        hotels.add(new Hotel("C", 1, 60));
        hotels.add(new Hotel("D", 8, 10));
        hotels.sort(Comparator.comparing(Hotel::getNumberOfBookings).reversed());

        if(hotels.size() < 3){
            System.out.println(hotels);
        } else {
            var customSortedList = new ArrayList<>(hotels.subList(0, 2));
            var secondList = new ArrayList<>(hotels.subList(2, hotels.size()));
            var mostReviewedHotel = secondList.stream().max(Comparator.comparing(Hotel::getNumberOfReviews)).orElseThrow();
            secondList.remove(mostReviewedHotel);
            customSortedList.add(mostReviewedHotel);
            customSortedList.addAll(secondList);
            System.out.println(customSortedList);
        }
    }
}

在这个代码中,酒店根据预订数量进行排序,并按特定要求对酒店列表进行排序。如果酒店列表的大小小于3,就直接打印酒店列表。否则,将酒店列表分为两部分:前两个酒店和剩余的酒店。然后,从剩余酒店中找出评论数最多的酒店,并将其添加到新创建的列表中,然后按照预订数量排序。最终,打印出排序后的酒店列表。

希望这有助于您理解代码的功能。如果您有任何其他问题或需要进一步的帮助,请随时提出。

英文:

I have a model

Hotel
  name
  bookings
  reviews

I have a strange sorting requirement and wanted to check with you if there is a better way
I need to sort list of books based on bookings and just the third item in the list with max number of reviews

What I have done so far and it works

  • Created a default comparator on bookings to sort the list
  • If the list is less than 3 do nothing
  • If the list is greater than or equal to 3 split list in two 0 to 2 and 3 to end of list
  • Create a new list and add first sublist to it.
  • Find the max recommended item from second sublist (3 to end of list) and add it to the newly created list and remove from the second sublist
  • Finally add the 2nd sublist which does not have max recommended but is sorted by number of bookings

Is there any performant efficient way of doing this?

Edit: Sorry here is the current code which works fine
public class HotelSort {

        public static void main(String args[]){
            List&lt;Hotel&gt; hotels = new ArrayList&lt;&gt;();
            hotels.add(new Hotel(&quot;A&quot;, 2, 50));
            hotels.add(new Hotel(&quot;B&quot;, 4, 30));
            hotels.add(new Hotel(&quot;C&quot;, 1, 60));
            hotels.add(new Hotel(&quot;D&quot;, 8, 10));
            hotels.sort(Comparator.comparing(Hotel::getNumberOfBookings).reversed());
    
            if(hotels.size() &lt; 3){
                System.out.println(hotels);
            } else {
                var customSortedList = new ArrayList&lt;&gt;(hotels.subList(0, 2));
                var secondList = new ArrayList&lt;&gt;(hotels.subList(2, hotels.size()));
                var mostReviewedHotel = secondList.stream().max(Comparator.comparing(Hotel::getNumberOfReviews)).orElseThrow();
                secondList.remove(mostReviewedHotel);
                customSortedList.add(mostReviewedHotel);
                customSortedList.addAll(secondList);
                System.out.println(customSortedList);
            }
        }
    }

The output in this case is which is correct but just wanted to see if there is a better performant way

Hotel{name=&#39;C&#39;, numberOfReviews=1, numberOfBookings=60},
 Hotel{name=&#39;A&#39;, numberOfReviews=2, numberOfBookings=50}, 
Hotel{name=&#39;D&#39;, numberOfReviews=8, numberOfBookings=10}, 
Hotel{name=&#39;B&#39;, numberOfReviews=4, numberOfBookings=30}

答案1

得分: 1

根据评论中@daniu指出的,您可以删除最受评论的元素并将其添加回所需的索引。您可以简化在特定索引之后查找最受评论元素的逻辑,方法是使用Collections.max并传递一个子列表。类似下面的代码应该是等效的:

List<Hotel> hotels = new ArrayList<>();
hotels.add(new Hotel("A", 2, 50));
hotels.add(new Hotel("B", 4, 30));
hotels add(new Hotel("C", 1, 60));
hotels.add(new Hotel("D", 8, 10));
hotels.sort(Comparator.comparing(Hotel::getNumberOfBookings).reversed());

if (hotels.size() > 3) {
    var mostReviewedHotel =
        Collections.max(hotels.subList(2, hotels.size()), Comparator.comparing(Hotel::getNumberOfReviews));
    hotels.remove(mostReviewedHotel);
    hotels.add(2, mostReviewedHotel);
}
hotels.forEach(System.out::println);
英文:

As pointed out by @daniu in the comments you can remove the most reviewed element and add it back at the desired index. You can simplify the logic of finding the most reviewed element after a certain index by using Collections.max and passing a sublist. Something like below should be equivalent:

List&lt;Hotel&gt; hotels = new ArrayList&lt;&gt;();
hotels.add(new Hotel(&quot;A&quot;, 2, 50));
hotels.add(new Hotel(&quot;B&quot;, 4, 30));
hotels.add(new Hotel(&quot;C&quot;, 1, 60));
hotels.add(new Hotel(&quot;D&quot;, 8, 10));
hotels.sort(Comparator.comparing(Hotel::getNumberOfBookings).reversed());

if(hotels.size() &gt; 3){
    var mostReviewedHotel =
            Collections.max(hotels.subList(2, hotels.size()), Comparator.comparing(Hotel::getNumberOfReviews));
    hotels.remove(mostReviewedHotel);
    hotels.add(2, mostReviewedHotel);
}
hotels.forEach(System.out::println);

huangapple
  • 本文由 发表于 2023年3月3日 20:35:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定