Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get elements from an ArrayList based by multiple conditions?

I'm creating an interface that manage the booking of Cinema tickets in different weeks/theatres.

I have a Film Class:

public class Film {

  private String title;
  private Double price;
  private String ageRestriction;
  private double rating;
  private String genre;
  private String location;
  private String screenDay;
}

A FilmList class that create and store all the Films in an ArrayList:

public class FilmList {

  private ArrayList <Film> filmArrayList;

  public FilmList (){
    this.filmArrayList = new ArrayList<>();
  }

  public void addFilm(Film films){
    this.filmArrayList.add(films);
  }

And this is the graphic unit interface. What I'm trying to do is to catch in the ArrayList just one element based on two condition: The Week and the Theatre selected from the user and also add a way for check that there's just one element on the list from the chosen parameters. This is important because each film instance will be called and "setted" on the Label of the FXML file (and because I'm thinking to implement an interface for add films in the ArrayList).

Thank's everyone.

like image 802
AAA999 Avatar asked Dec 03 '25 03:12

AAA999


1 Answers

OK, so in your example it would be something like following:

   //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.

like image 79
MS90 Avatar answered Dec 05 '25 18:12

MS90