/* OlympicsFiltered.java Copyright (c) Kari Laitinen http://www.naturalprogramming.com 2015-01-26 File created. 2015-09-04 Last modification. This program is a kind of modernized version of Olympics.java. This version uses modern Java features that are available in Java 8 and later versions. This program demonstrates: - converting a traditional array into an ArrayList - searching for data in a Stream with a Lambda expression - use of an Optional object */ import java.util.* ; class Olympics { int olympic_year ; String olympic_city ; String olympic_country ; public Olympics( int given_olympic_year, String given_olympic_city, String given_olympic_country ) { olympic_year = given_olympic_year ; olympic_city = given_olympic_city ; olympic_country = given_olympic_country ; } public int get_year() { return olympic_year ; } public void print_olympics_data() { System.out.print( "\n In " + olympic_year + ", Olympic Games were held in " + olympic_city + ", " + olympic_country + ".\n" ) ; } } class OlympicsFiltered { public static void main( String[] not_in_use ) { Olympics[] olympics_table = { new Olympics( 1896, "Athens", "Greece" ), new Olympics( 1900, "Paris", "France" ), new Olympics( 1904, "St. Louis", "U.S.A." ), new Olympics( 1906, "Athens", "Greece" ), new Olympics( 1908, "London", "Great Britain" ), new Olympics( 1912, "Stockholm","Sweden" ), new Olympics( 1920, "Antwerp", "Belgium" ), new Olympics( 1924, "Paris", "France" ), new Olympics( 1928, "Amsterdam","Netherlands"), new Olympics( 1932, "Los Angeles", "U.S.A."), new Olympics( 1936, "Berlin", "Germany" ), new Olympics( 1948, "London", "Great Britain" ), new Olympics( 1952, "Helsinki","Finland" ), new Olympics( 1956, "Melbourne","Australia" ), new Olympics( 1960, "Rome", "Italy" ), new Olympics( 1964, "Tokyo", "Japan" ), new Olympics( 1968, "Mexico City","Mexico" ), new Olympics( 1972, "Munich", "West Germany" ), new Olympics( 1976, "Montreal", "Canada" ), new Olympics( 1980, "Moscow", "Soviet Union" ), new Olympics( 1984, "Los Angeles","U.S.A."), new Olympics( 1988, "Seoul", "South Korea"), new Olympics( 1992, "Barcelona","Spain" ), new Olympics( 1996, "Atlanta", "U.S.A." ), new Olympics( 2000, "Sydney", "Australia" ), new Olympics( 2004, "Athens", "Greece" ), new Olympics( 2008, "Beijing", "China" ), new Olympics( 2012, "London", "Great Britain" ) } ; // Next, we'll create an ArrayList object into which we'll add // all the Olympics object that were put into the convetional array. ArrayList olympics_list = new ArrayList() ; Collections.addAll( olympics_list, olympics_table ) ; System.out.print("\n This program can tell where the Olympic " + "\n Games were held in a given year. Give " + "\n a year by using four digits: " ) ; Scanner keyboard = new Scanner( System.in ) ; int given_year = keyboard.nextInt() ; // The ArrayList is converted to a Stream, which is searched for // an Olympics object that contains the year given by the user. // The Optional object may or may not contain the Olympics object // that we are interested in. Optional found_olympics = olympics_list.stream() .filter( olympics -> olympics.get_year() == given_year ) .findFirst() ; if ( found_olympics.isPresent() ) { found_olympics.get().print_olympics_data() ; } else { System.out.print( "\n Sorry, no Olympic Games were held in " + given_year + ".\n" ) ; } } } /* The following pages provide useful information related to this program: http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html http://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html http://docs.oracle.com/javase/8/docs/api/java/util/Optional.html */