-1
 List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
//        list.clear();
//        list.add(2);
 list = list.stream().
     mapToInt(integer -> 2).
     filter(integer -> integer ==2).
     distinct().
     boxed().collect(Collectors.toList());

How diff they are? Attach importance to reading or Use new tech technology?

Haven Lin
  • 176
  • 1
  • 16
  • 1
    What are you trying to achieve? – Eran Apr 26 '17 at 12:17
  • 1
    `mapToInt(integer -> 2)` will transform every given value of the list to `2`. The filter allows only `2`s, so 5 `2`'s are given to `distinct()`. As you have only `2`'s the result will be a `List` containing 1 `2`. If your question is how to solve `list.clear(); list.add(2)` with streams then the answer is: you probably shouldn't. – Roland Apr 26 '17 at 12:21
  • Maybe a simple `list = Arrays.asList(2);` will do? Or a stream way: `list = Stream.of(2).collect(Collectors.toList());`. – Flown Apr 26 '17 at 12:24
  • it is not my expected, thanks – Haven Lin Apr 26 '17 at 13:14

1 Answers1

2

The entire operation that you want to do is slightly wrong

 list = list.stream()
            .mapToInt(i -> 2) // every single element is transformed to "2"
            .filter(i -> i == 2) // since the previous step turns everything into 2, this one will take every element
            .distinct() // then you apply distinct, resulting in a single element
            .boxed()
            .collect(Collectors.toList());

The result is a List with a single element with value 2, but it's very inefficient.

If you really want to filter, then it would be easier with just:

 list = list.stream()
            .filter(i -> i == 2)
            .collect(Collectors.toList());

Besides that your initial code :

 list.clear();
 list.add(2);

will fail, because Arrays.asList will create an immutable list.

But even if you would have an ArrayList that is mutable, the difference is that with your stream approach you will not alter the source of the stream; while with you first approach you would delete everything and re-populate it with different values.

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • if use list = list.stream() .filter(i -> i == 2) .collect(Collectors.toList()); if list are not contain 2,or contain duplicate 2? then it will be not my expected. – Haven Lin Apr 26 '17 at 13:19
  • @HavenLin that is easy to try and you should try that. If it does not contain a `2` the resulting List is going to be empty. If there are multiple `2` in the initial list, there will be multiple results in the resulting List. If you want to remove duplicates you could collect to a `Set` : `.collect(Collectors.toSet());` – Eugene Apr 26 '17 at 13:23
  • but the list from request, and i expected the list only contain 2,not empty – Haven Lin Apr 26 '17 at 13:28
  • 1
    @HavenLin you are trying to say that you get that list from a Request and you will always get at least one element that is equal to `2`? Sorry but I can't really understand what you are saying – Eugene Apr 26 '17 at 13:30
  • hmmm, Maybe I was wrong, btw, Could i contact you by mail or chat tools? – Haven Lin Apr 26 '17 at 13:34
  • @HavenLin not really. You could post another question explaining *exactly* what you mean/want/or don't understand – Eugene Apr 26 '17 at 13:35
  • i mean my logic is inefficient, – Haven Lin Apr 26 '17 at 13:38
  • The `List` view that is created by `Arrays#asList` is **not** immutable, as you can modify elements that already exist in the `List`. – Jacob G. Apr 26 '17 at 15:16
  • @JacobG. in this case *lots of other* code is not immutable also. I've actually asked a question like this several years ago: http://stackoverflow.com/questions/12053945/is-guavas-immutablexxx-really-immutable – Eugene Apr 26 '17 at 15:22
  • I suppose, the point of @Jacob G’s comment is that the `List` returned by `Arrays.asList` still supports the `set` method, so it’s only “not resizable” rather than immutable. On the other hand, if you use `Collections.singletonList(2)` (or `List.of(2)`) you get a truly immutable `List` (which doesn’t say anything about the mutability of `2` itself)… – Holger Apr 27 '17 at 10:01
  • @Eugene Could you help me to answer another question? http://stackoverflow.com/questions/43657798/return-about-java-8-foreach – Haven Lin Apr 27 '17 at 12:28
  • hi guys, who has free to help answer question for http://stackoverflow.com/questions/43973596/how-to-convert-foreach-to-lambda – Haven Lin May 15 '17 at 07:32