[英]I am getting unexpected behaviour when using Java Streams and Scanners
我最近看到一個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;
}
現在整個程序運行正常,但是在搜索書籍時,我遇到了Scanner的意外行為。 這是我遇到的直接輸入/輸出行為:
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次。 我該如何緩解這種情況? 是什么導致這種情況發生?
這是因為在您的Stream
調用next()
,因此對於Stream
中的每個Book
對象,將對調用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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.