[英]copying list object to another list object with modified content using Streams
I want to use streams to Uppercase Book name in my List 我想使用流到我的列表中的大写书名
List<Book> list = new ArrayList<Book>();
list.add(new Book("1", "Java", 10));
list.add(new Book("2", "Spring", 20));
list.add(new Book("3", "Webservices", 30));
I want to convert case of my book name. 我要转换我的书名的大小写。 My current approach is giving me List<String> instead of List<Book> --------- [JAVA, SPRING, WEBSERVICES]
我当前的方法是给我List <String>而不是List <Book> --------- [JAVA,SPRING,WEBSERVICES]
package java8;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;;
public class StreamTest {
public static void main(String s[]) {
List<Book> list = new ArrayList<Book>();
list.add(new Book("1", "Java", 10));
list.add(new Book("2", "Spring", 20));
list.add(new Book("3", "Webservices", 30));
List<String> updateList = list.stream().map(str2 -> str2.getName().toUpperCase()).collect(Collectors.toList());
System.out.println("--------- "+updateList);
}
}
You can just use .forEach()
if you just want to update your existing list: 如果只想更新现有列表,则可以使用
.forEach()
:
list.forEach(b -> b.setName(b.getName().toUpperCase()));
If you want to create a new list you can use this: 如果要创建新列表,可以使用以下方法:
List<Book> result = list.stream()
.map(b -> new Book(b.getId(), b.getName().toUpperCase(), b.getSomething()))
.collect(Collectors.toList());
Your comment clarified your need: 您的评论明确了您的需求:
i should have been more clear.
我应该更清楚了。 This piece of code is running but gets me List of Book Title.
这段代码正在运行,但可以获取书名列表。 I want a List instead with Book Title in Uppercase
我想要一个清单,而不是大写的书名
You even state your desired return type as List<Book>
instead of List<String>
. 您甚至将所需的返回类型声明为
List<Book>
而不是List<String>
。
So lets start there: 因此,让我们从这里开始:
Change 更改
List<String> updateList
To 至
List<Book> updateList
Then, to actually return a List of Books, each iteration in the .map()
method must return the Book object of that iteration. 然后,要实际返回书籍列表,
.map()
方法中的每个迭代都必须返回该迭代的Book对象。 But, before that the operation must set the Name member to uppercase on that Book. 但是,在此之前,该操作必须在该Book上将Name成员设置为大写。
Change .map()
更改
.map()
.map(str2 -> str2.getName().toUpperCase())
To 至
.map(str2 -> {str2.setName(str2.getName().toUpperCase()); return str2;})
You will then be provided with a List<Book>
with uppercased titles. 然后,您将获得带有大写标题的
List<Book>
。
You can find a working version here: https://repl.it/@randycasburn/StreamEdit 您可以在这里找到可用的版本: https : //repl.it/@randycasburn/StreamEdit
The correct answers have already been given so just to point that the code looks much prettier (a matter of taste) if used streams in conjunction with method reference feature. 已经给出了正确的答案,以至于指出如果将流与方法引用功能结合使用,则代码看起来会更漂亮(很有趣)。
let me illustrate: 让我说明一下:
class Foo {
public List<Book> convertToBooksWithTitleInUpperCase(List<Book> books) {
return books.stream()
// the means: "map" book to another object (another book in this case) by calling the conversion function "createBookWithTitleInUpperCase")
.map(this::createBookWithTitleInUpperCase)
.collect(Collectors.toList());
}
private Book createBookWithTitleInUpperCase(Book book) {
return new Book(book.getId(), book.getTitle().toUpperCase());
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.