使用Java Streams和Scanners时出现意外行为

时间:2019-02-06 21:57:30

标签: java java-stream

我最近看到一个关于Uni大学课程的主题,该课程由一位朋友指导,以某种方式进行。我以为我会借此机会参加这项任务的。

我这样创建了一个Book类:

class Book
{
    private String author, title;

    public Book setAuthor(String a)
    {
        author = a;
        return this;
    }

    public Book setTitle(String t)
    {
        title = t;
        return this;
    }

    public String getAuthor() 
    {
        return author;
    }

    public String getTitle()
    {
        return title;
    }
}

概念是,用户可以在程序开始时创建多本书,然后搜索作者:

private final static int BOOK_NO = 3;
private final static SO instance = new SO(); // This is whatever you called the class

public static void main(String[] args)
{
    Book[] books = new Book[BOOK_NO];
    Scanner kybd = new Scanner(System.in);

    for(int i = 0; i < BOOK_NO; i++)
    {
        books[i] = instance.addBook(kybd, new Book());
    }

    Arrays.stream(instance.findBook(kybd, books)).forEach(o -> {
        System.out.println(o.getTitle() + " by " + o.getAuthor());
    });
}

public Book addBook(Scanner s, Book b)
{

    System.out.println("Enter the Author of this book:");
    b.setAuthor(s.next());

    System.out.println("Enter the Title of this book:");
    b.setTitle(s.next());

    return b;
}

public Book[] findBook(Scanner s, Book[] bs)
{
    System.out.println("Search a book by author:");

    List<Book> finding = Arrays .stream(bs)
            .filter(o -> o.getAuthor().equalsIgnoreCase(s.next()))
            .collect(Collectors.toList());

    System.out.println("Found " + finding.size() + " matches.");

    Book[] output = new Book[finding.size()];
    output = finding.toArray(output);
    return output;
}

现在,整个程序运行正常,但是在搜索书籍时,我遇到了扫描仪意外的行为。这是我遇到的直接输入/输出行为:

Enter the Author of this book:
Foo
Enter the Title of this book:
Bar
Enter the Author of this book:
Foo
Enter the Title of this book:
FooBar
Enter the Author of this book:
Bar
Enter the Title of this book:
Foo
Search a book by author:
Foo
Foo
Foo
Found 2 matches.
Bar by Foo
FooBar by Foo

如您所见,在获得任何结果之前,我必须将书的作者输入扫描仪3次。我该如何缓解呢?是什么导致这种情况发生?

1 个答案:

答案 0 :(得分:3)

这是因为在您的Stream中调用了next(),因此对于Book中的每个Stream对象,filter调用中的Predicate为应用于它,然后将调用next()。将其解析为变量,这样就不会被多次调用:

String book = s.next();
List<Book> finding = Arrays.stream(bs)
                           .filter(o -> o.getAuthor().equalsIgnoreCase(book))
                           .collect(Collectors.toList());

filter()接受一个Predicate,在这种情况下,它将类似于:

Predicate<String> pred = str -> str.equalsIgnoreCase(s.next());

因此,每次应用next()都会被调用