我正在创建一个界面来管理不同周/剧院的电影票预订。
我有一个电影课:
public class Film {
private String title;
private Double price;
private String ageRestriction;
private double rating;
private String genre;
private String location;
private String screenDay;
}
一个 FilmList 类,用于在 ArrayList 中创建和存储所有影片:
public class FilmList {
private ArrayList <Film> filmArrayList;
public FilmList (){
this.filmArrayList = new ArrayList<>();
}
public void addFilm(Film films){
this.filmArrayList.add(films);
}
这就是图形单元界面。我想做的是根据两个条件在 ArrayList 中捕获一个元素: 从用户中选择的周和剧院,还添加了一种方法来检查所选参数中列表中是否只有一个元素。这很重要,因为每个影片实例都将被调用并在 FXML 文件的标签上“设置”(并且因为我正在考虑实现一个用于在 ArrayList 中添加影片的接口(interface))。
谢谢大家。
最佳答案
好的,所以在您的示例中,它将类似于以下内容:
//try to find any movie that suits two predicates, 1st - that it's price is greater than 30( this is random number, you can put a value from your textfield here ) and 2nd - that it's title contains Spiderman ( again, put a value from your textfield title search here for your need )
Optional<Film> movie = listOfMovies.stream().filter(i -> i.getPrice() > 30 && i.getTitle.contains("Spiderman")).findAny();
// if any movie has been found to suits provided criterias
if(movie.isPresent())
{
//print it on screen, note method get()
//again this is just for example here, in your code,
// you can do with this result whatever you like
// for example show all data about that movie on screen
System.out.println("---->" +movie.get());
}
else
{
// if not found do nothing
System.out.println("Nothing found...");
}
关于java - 如何根据多个条件从ArrayList中获取元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54818723/