"stream () method is undefined for type Set <Person>"
I am trying to run an example java program that I found in java essential trails tutorials. I want to go through it using eclipse to better understand what is going on. The Person class works fine without errors, but the MRTest class has one error at the end, that the stream () method is undefined for the type that calls it. I do not 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.
}
}
+4