0

Here is the stream "airportFilteredList" i am having trouble with, it should print the country name that matches the filter of the given "Turkey" string but it doesn't print anything to the console, using count() on the stream gives a value of 0

public void getFilteredAirportList(String filteredText){

     System.out.println(filteredText); //returns Turkey    
     Predicate<Airport> filterCountry = u -> u.getCountry() == filteredText;
     Stream<Airport> airportFilteredList = airportList.stream().filter(filterCountry);
     airportFilteredList.forEach(u -> System.out.println(u.getCountry()));

}

I made a general stream without the filter as well from the same list but it works well and returns all country names of the airports.

public void getAirportList(){
    Stream <Airport> airportStream = airportList.stream();
    airportStream.forEach(u -> u.getCountry()); //returns Turkey
}

The ArrayList from which the Airport objects are retrived

ArrayList<Airport> airportList = new ArrayList<Airport>();

public void createAirport(String airport, String country, String continent,
        String airfield_length) {
    airportList.add(new Airport(airport, country, continent,
            airfield_length));

}

The full source code is at https://github.com/Jointts/JavaExam/tree/master/JavaExam

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
Jointts
  • 121
  • 3
  • 11

1 Answers1

1

use equals instead of == in java to compare String as == compares references not value.

 Stream<Airport> airportFilteredList = airportList.stream().filter(
            u -> "Turkey".equals(u.getCountry()));
akash
  • 22,664
  • 11
  • 59
  • 87