0

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);
        }
    }
3
  • 3
    What exactly is the problem? Commented Jul 5, 2019 at 16:04
  • 1
    Compiles and runs with no errors in the console? There is nothing wrong with your code. You can see it in operation here: repl.it/@randycasburn/StreamEdit Commented Jul 5, 2019 at 16:51
  • 1
    i should have been more clear. This piece of code is running but gets me List<String> of Book Title. I want a List<Book> instead with Book Title in Uppercase. Commented Jul 5, 2019 at 17:22

3 Answers 3

1

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());
Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

Using .map(str2 -> {str2.setName(str2.getName().toUpperCase()); return str2;}) makes the whole thing more complicated than it is, as you can just use .forEach() to achieve that.
I'm only attempting to show the OP where the code went wrong. Providing a different solution doesn't help the OP see the error.
Jokes on both of you and the rest of the Java users, you can write this as var updateList = list.Select(b => new Book{Id = b.Id, Name = b.Name.ToUpper(), Something = b.Something} in C#
I missed the C# part of the question, but your example is not any better or more concise than: list.forEach(b -> b.setName(b.getName().toUpperCase()));
0

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());
  }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.