如何根据多个条件从ArrayList中获取元素?

时间:2019-02-22 01:26:40

标签: java arrays javafx arraylist

我正在创建一个界面,用于管理不同周/剧院的电影票预订。

我有一个电影课:

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文件的Label上被调用和“设置”(并且因为我正在考虑实现用于在ArrayList中添加影片的接口)。

谢谢大家。

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

好的,所以在您的示例中,它将类似于以下内容:

   //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...");
       }

More about Optional can be found here.