“类型Set <person>”</person>的方法流()未定义

时间:2014-07-10 23:55:34

标签: java eclipse lambda java-8 java-stream

我正在尝试运行我在java必备路径课程中找到的示例java程序。我想使用eclipse逐步完成它,以便更好地了解正在发生的事情。 Person类很好没有错误但是MRTest类最后有一个错误,对于调用它的类型,stream()方法是未定义的。我不知道如何解决这个问题。

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

1 个答案:

答案 0 :(得分:6)

问题是您的类路径中有1.7个SE库,将其更改为1.8修复它

查看问题评论部分中的详细信息