gpt4 book ai didi

java - 列表的列表,在父列表上有谓词过滤器,在子列表上有另一个谓词过滤器

转载 作者:行者123 更新时间:2023-12-02 18:00:16 27 4
gpt4 key购买 nike

我希望根据谓词过滤列表,并过滤她的子列表

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

class Hotel {
private final String city;
private final int numberOfStart;
private final List<Room> rooms = new ArrayList<>();

public String getCity(){return city;}
public int getNumberOfStart(){return numberOfStart;}
public List<Room> getRooms(){return rooms;}
public Hotel(String city, int numberOfStart) {
this.city = city;
this.numberOfStart = numberOfStart;
}

public void creatRooms(String roomNumber ,int numberOfbed,Double price) {
Room room = new Room(roomNumber,numberOfbed,price);
this.rooms.add(room);
}

@Override
public String toString() {
return "Hotel{\n\t" +
"city='" + city + '\'' +
", numberOfStart=" + numberOfStart +
", \n\trooms=" + rooms +
"}\n\n";
}
}

class Room {
private final double price;
private final int numberOfBed;
private final String roomNumber;

Room (String roomNumber,int numberOfBed, Double price){
this.price=price;
this.roomNumber=roomNumber;
this.numberOfBed=numberOfBed;

}

public double getPrice() {return price;}
public int getNumberOfBed(){return numberOfBed;}

@Override
public String toString() {
return "\n\t\tRoom{" +
"price=" + price +'\'' +
", numberOfBed=" + numberOfBed +
", roomNumber='" + roomNumber +
'}';
}
}

public class Main {

/**
* @param hotelList List search on a list of hotels.
* @param city relates to the location of the hotel, if empty "", then the predicate will be true, and ignore the city parameter.
* @param start concerns the quality of the hotel, if set to 0 then the predicate will be true, and ignore the start parameter.
* @param priceMax
* @param nbBed concerns the amount beds, ignored if set to 0
* @return
*/
public static List<Hotel> searchHotelRoom(List<Hotel> hotelList, String city, int start, Double priceMax, int nbBed) {

//condition about city location and price on hotel list
Predicate<Hotel> byCity = !city.isEmpty()? hotel -> hotel.getCity().equalsIgnoreCase(city) : hotel -> true;
Predicate<Hotel> byStart =!(start==0)? hotel -> hotel.getNumberOfStart() == start:hotel -> true;

//condition on room list
Predicate<Room> byNbBed =!(nbBed==0)? room -> (room.getNumberOfBed()== nbBed) :room -> false;
Predicate<Room> byPrice = room -> room.getPrice()<=priceMax;

return hotelList.stream()
.filter(byStart)
.filter(byCity)
.filter(room -> room.getRooms().stream().anyMatch(byPrice))
.filter(room -> room.getRooms().stream().anyMatch(byNbBed))
.collect(Collectors.toList());
}

public static void main(String[] args) {

List<Hotel> hotelList = new ArrayList<>();

//Dummy hotel data
Hotel hotelA = new Hotel("Paris",4);
hotelA.creatRooms("p12", 2, 150.);
hotelA.creatRooms("p17", 1, 200.);
hotelA.creatRooms("p15", 3, 50.);
hotelList.add(hotelA);

Hotel hotelB = new Hotel("Montpellier",4);
hotelB.creatRooms("b12", 2, 20.);
hotelB.creatRooms("b17", 1, 200.);
hotelB.creatRooms("b15", 1, 40.);
hotelB.creatRooms("b15", 1, 1.);
hotelList.add(hotelB);

Hotel hotelC = new Hotel("Toulouse",4);
hotelC.creatRooms("c12", 21, 200.);
hotelC.creatRooms("c17", 11, 100.);
hotelC.creatRooms("c15", 21, 50.);
hotelC.creatRooms("c16", 30, 25.);
hotelList.add(hotelC);

//System.out.println("Hotels List\n");
//hotelList.forEach(System.out::println);

List<Hotel> result= searchHotelRoom(hotelList,"",0,200.,2);
System.out.println("Result of search");
result.forEach(System.out::println);
}

}

搜索功能不起作用,因为我希望有一些不一致例如对于

List<Hotel> result= searchHotelRoom(hotelList,"paris",0,200.,1);

我有这个结果

Result of search
Hotel{
city='Paris', numberOfStart=4,
rooms=[
Room{price=150.0', numberOfBed=2, roomNumber='p12},
Room{price=200.0', numberOfBed=1, roomNumber='p17},
Room{price=50.0', numberOfBed=3, roomNumber='p15}]}

但我想要类似的东西

Result of search
Hotel{
city='Paris', numberOfStart=4,
rooms=[
Room{price=200.0', numberOfBed=1, roomNumber='p17}}

过滤器之间似乎没有和逻辑

List<Hotel> result= searchHotelRoom(hotelList,"paris",0,200.,2);

必须返回任何内容,但我有结果

以及许多酒店

List<Hotel> result= searchHotelRoom(hotelList,"",0,200.,1);

我有

Result of search
Hotel{
city='Paris', numberOfStart=4,
rooms=[
Room{price=150.0', numberOfBed=2, roomNumber='p12},
Room{price=200.0', numberOfBed=1, roomNumber='p17},
Room{price=50.0', numberOfBed=3, roomNumber='p15}]}


Hotel{
city='Montpellier', numberOfStart=4,
rooms=[

Room{price=200.0', numberOfBed=1, roomNumber='b17},

但我正在寻找类似的东西

Result of search
Hotel{
city='Paris', numberOfStart=4,
rooms=[
Room{price=200.0', numberOfBed=1, roomNumber='p17}
}


Hotel{
city='Montpellier', numberOfStart=4,
rooms=[

Room{price=200.0', numberOfBed=1, roomNumber='b17}
}

在搜索方法中,anyMatch 返回一个 boolean 值,但我想要房间列表,
所以我在我的搜索方法上尝试了一些东西,例如,但不起作用

 .filter(room -> room.getRooms().stream().filter(byPrice))

有谁能帮帮我吗?

最佳答案

您需要使用 map (或平面 map )以及过滤器来执行此类操作。粗略的形式类似于:

hotelsList.stream()
.filter(hotelFilter)
.flatMap(hotel -> hotel.rooms.stream()
.filter(roomFilter))
.collect(whatever)

一般来说,map操作需要 A<B>和一个 B -> C并给你一个A<C> 。平面 map 采用 A<B>和一个 B -> A<C>并给你一个A<C> (例如,组合列表,而且还对 Optional 执行操作,即使原始值存在,该操作本身也可能返回 None)

关于java - 列表的列表,在父列表上有谓词过滤器,在子列表上有另一个谓词过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74583003/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com