简体   繁体   中英

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]

    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:

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

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. But, before that the operation must set the Name member to uppercase on that Book.

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

You can find a working version here: 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());
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM