简体   繁体   English

使用Streams将列表对象复制到具有修改内容的另一个列表对象

[英]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.

相关问题 如何使用流将对象列表转换为另一个列表对象? - How to convert list of object to another list object using streams? 使用 Streams,查看列表是否包含来自另一个列表的 object 的属性 - Using Streams, see if a list contains a property of an object from another list 使用 Java 8 Streams 从另一个创建对象列表 - Create list of object from another using Java 8 Streams 我可以使用流创建一个列表,其中标准是检查一个列表,然后将 object 添加到另一个列表? - Can I create a list using streams where the critera are to check one list then add an object to another list? 使用Java 8流将2个不同对象的列表连接到第三个对象的列表 - Join list of 2 different object to a list of a third object using Java 8 streams 使用 java 8 流过滤和修改列表对象 - Filter and modify list object using java 8 streams Java:使用流转换列表 <Object> 到另一个列表 <anotherObject> - Java : use Streams to convert List<Object> to another List<anotherObject> Java 流设置 object 列表的每个属性与另一个列表 - Java streams set each attribute of object List with another List 合并与 List 中每个唯一对象相关的数据,并使用 Streams 将它们转换为另一种类型的 List - Merge the data relating to each unique Object in a List and transform them into a List of another Type using Streams 使用Java Streams按属性将对象列表组合在一起,并将它们减少为具有另一个属性平均值的新对象列表 - Using Java Streams to group together a List of objects by an attribute and reduce them to a new list of object with the average of another attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM