"method stream() is undefined for the type Set<Person>"

11,503

Problem was you had 1.7 SE library in your classpath, Changing it to 1.8 fixed it

See detail in question comments section

Share:
11,503
Jpaji Rajnish
Author by

Jpaji Rajnish

Updated on June 04, 2022

Comments

  • Jpaji Rajnish
    Jpaji Rajnish almost 2 years

    I'm trying to run an example java program I found in the java essential trails lessons. I want to step through it using eclipse to better understand what's going on. The Person class is fine with no errors but the MRTest class has one error at the end, that the stream() method is undefined for the type that's calling it. I don't know how to fix this.

    import java.util.List;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.Collection;
    import java.util.function.Supplier;
    import java.util.Set;
    import java.util.HashSet;
    import java.time.chrono.IsoChronology;
    
    public class MRTest {
    
        // The method transferElements copies elements from one collection to
        // another
    
        public static <T, SOURCE extends Collection<T>, DEST extends Collection<T>>
            DEST transferElements(
                SOURCE sourceCollection,
                Supplier<DEST> collectionFactory) {
    
                DEST result = collectionFactory.get();
                for (T t : sourceCollection) {
                    result.add(t);
                }
                return result;
        }  
    
        public static void main(String... args) {
    
            List<Person> roster = Person.createRoster();        
    
            for (Person p : roster) {
                p.printPerson();
            }
    
    
            Person[] rosterAsArray = 
                roster.toArray(new Person[roster.size()]);
    
            class PersonAgeComparator
                implements Comparator<Person> {
                public int compare(Person a, Person b) {
                    return a.getBirthday().compareTo(b.getBirthday());
                }
            }
    
            // Without method reference
            Arrays.sort(rosterAsArray, new PersonAgeComparator());
    
            // With lambda expression
            Arrays.sort(rosterAsArray,
                (Person a, Person b) -> {
                    return a.getBirthday().compareTo(b.getBirthday());
                }
            );
    
            // With method reference
            Arrays.sort(rosterAsArray, Person::compareByAge);
    
            // Reference to an instance method of a particular object
            class ComparisonProvider {
                public int compareByName(Person a,
                    Person b) {
                    return a.getName().compareTo(b.getName());
                }
    
                public int compareByAge(Person a,
                    Person b) {
                    return a.getBirthday().compareTo(b.getBirthday());
                }
            }
            ComparisonProvider myComparisonProvider = new ComparisonProvider();
            Arrays.sort(rosterAsArray, myComparisonProvider::compareByName);
    
            // Reference to an instance method
            // of an arbitrary object of a particular type
    
            String[] stringArray = { "Barbara", "James", "Mary", "John",
                "Patricia", "Robert", "Michael", "Linda" };
            Arrays.sort(stringArray, String::compareToIgnoreCase);
    
            Set<Person> rosterSetLambda =
                transferElements(roster, () -> { return new HashSet<>(); });
    
            Set<Person> rosterSet = transferElements(
                roster, HashSet::new);
            System.out.println("Printing rosterSet:");
            rosterSet.stream().forEach(p -> p.printPerson()); // This is the line that is the problem.
        }
    }