<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: neshamah0000</title>
    <description>The latest articles on DEV Community by neshamah0000 (@neshamah0000).</description>
    <link>https://dev.to/neshamah0000</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1006758%2Fcacb4fe7-8293-4bee-a7fa-91dc3b01c8b2.png</url>
      <title>DEV Community: neshamah0000</title>
      <link>https://dev.to/neshamah0000</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/neshamah0000"/>
    <language>en</language>
    <item>
      <title>Java 8 Stream</title>
      <dc:creator>neshamah0000</dc:creator>
      <pubDate>Thu, 12 Jan 2023 11:55:55 +0000</pubDate>
      <link>https://dev.to/neshamah0000/java-8-stream-3am1</link>
      <guid>https://dev.to/neshamah0000/java-8-stream-3am1</guid>
      <description>&lt;p&gt;&lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html"&gt;https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://cheatography.com/carlmig/cheat-sheets/java-8-streams/"&gt;https://cheatography.com/carlmig/cheat-sheets/java-8-streams/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;concat(Stream&amp;lt;? extends T&amp;gt; a, Stream&amp;lt;? extends T&amp;gt; b)&lt;br&gt;
Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Test
public void whenMergingStreams_thenResultStreamContainsElementsFromBoth() {
    Stream&amp;lt;Integer&amp;gt; stream1 = Stream.of(1, 3, 5);
    Stream&amp;lt;Integer&amp;gt; stream2 = Stream.of(2, 4, 6);

    Stream&amp;lt;Integer&amp;gt; resultingStream = Stream.concat(stream1, stream2);

    assertEquals(
      Arrays.asList(1, 3, 5, 2, 4, 6),
      resultingStream.collect(Collectors.toList()));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;count()&lt;br&gt;
Returns the count of elements in this stream.&lt;/p&gt;

&lt;p&gt;distinct()&lt;br&gt;
Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream.&lt;/p&gt;

&lt;p&gt;filter(Predicate&amp;lt;? super T&amp;gt; predicate)&lt;br&gt;
Returns a stream consisting of the elements of this stream that match the given predicate.&lt;/p&gt;

&lt;p&gt;flatMap(Function&amp;lt;? super T,? extends Stream&amp;lt;? extends R&amp;gt;&amp;gt; mapper)&lt;br&gt;
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class FlatMapExample1 {

    public static void main(String[] args) {

        Developer o1 = new Developer();
        o1.setName("mkyong");
        o1.addBook("Java 8 in Action");
        o1.addBook("Spring Boot in Action");
        o1.addBook("Effective Java (3nd Edition)");

        Developer o2 = new Developer();
        o2.setName("zilap");
        o2.addBook("Learning Python, 5th Edition");
        o2.addBook("Effective Java (3nd Edition)");

        List&amp;lt;Developer&amp;gt; list = new ArrayList&amp;lt;&amp;gt;();
        list.add(o1);
        list.add(o2);

        // hmm....Set of Set...how to process?
        /*Set&amp;lt;Set&amp;lt;String&amp;gt;&amp;gt; collect = list.stream()
                .map(x -&amp;gt; x.getBook())
                .collect(Collectors.toSet());*/

        Set&amp;lt;String&amp;gt; collect =
                list.stream()
                        .map(x -&amp;gt; x.getBook())                              //  Stream&amp;lt;Set&amp;lt;String&amp;gt;&amp;gt;
                        .flatMap(x -&amp;gt; x.stream())                           //  Stream&amp;lt;String&amp;gt;
                        .filter(x -&amp;gt; !x.toLowerCase().contains("python"))   //  filter python book
                        .collect(Collectors.toSet());                       //  remove duplicated

        collect.forEach(System.out::println);

    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;flatMapToDouble(Function&amp;lt;? super T,? extends DoubleStream&amp;gt; mapper)&lt;br&gt;
flatMapToInt(Function&amp;lt;? super T,? extends IntStream&amp;gt; mapper)&lt;br&gt;
flatMapToLong(Function&amp;lt;? super T,? extends LongStream&amp;gt; mapper)&lt;br&gt;
mapToDouble(ToDoubleFunction&amp;lt;? super T&amp;gt; mapper)&lt;br&gt;
mapToInt(ToIntFunction&amp;lt;? super T&amp;gt; mapper)&lt;br&gt;
mapToLong(ToLongFunction&amp;lt;? super T&amp;gt; mapper)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // Creating a List of Strings
        List&amp;lt;String&amp;gt; list = Arrays.asList("Geeks", "GFG",
                                          "GeeksforGeeks", "gfg");

        // Using Stream flatMapToInt(Function mapper)
        // to get length of all strings present in list
        list.stream().flatMapToInt(str -&amp;gt; IntStream.of(str.length())).
        forEach(System.out::println);
        // result: 5 3 13 3
        list.stream().mapToInt(str -&amp;gt; 
        str.length()).forEach(System.out::println);
        // result: 5 3 13 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;forEach(Consumer&amp;lt;? super T&amp;gt; action)&lt;br&gt;
Performs an action for each element of this stream.&lt;/p&gt;

&lt;p&gt;forEachOrdered(Consumer&amp;lt;? super T&amp;gt; action)&lt;br&gt;
Performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.&lt;/p&gt;

&lt;p&gt;generate(Supplier s)&lt;br&gt;
Returns an infinite sequential unordered stream where each element is generated by the provided Supplier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Stream.generate(new Random()::nextInt)
    .limit(5).forEach(System.out::println); 
//result: 697197501, 50139200, 321540264, 1042847655, -770409472
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;iterate(T seed, UnaryOperator f)&lt;br&gt;
Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    //Stream.iterate(initial value, next value)
    Stream.iterate(0, n -&amp;gt; n + 1)
                .limit(10)
                .forEach(x -&amp;gt; System.out.println(x));
       //0 1 2 3 4 5 6 7 8 9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;limit(long maxSize)&lt;br&gt;
Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.&lt;br&gt;
skip(long n)&lt;br&gt;
Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Stream.of(-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
              .skip(2)
              .forEach(i -&amp;gt; System.out.print(i + " "));
        // 1 2 3 4 5 6 7 8 9 10

    Stream.of(-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
              .limit(2)
              .forEach(i -&amp;gt; System.out.print(i + " "));
       // -1 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;peek(Consumer&amp;lt;? super T&amp;gt; action)&lt;br&gt;
This method exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     Stream.of("one", "two", "three", "four")
         .filter(e -&amp;gt; e.length() &amp;gt; 3)
         .peek(e -&amp;gt; System.out.println("Filtered value: " + e))
         .map(String::toUpperCase)
         .peek(e -&amp;gt; System.out.println("Mapped value: " + e))
         .collect(Collectors.toList());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;reduce(BinaryOperator accumulator)&lt;br&gt;
Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;List&amp;lt;Integer&amp;gt; numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
int result = numbers
  .stream()
  .reduce(0, (subtotal, element) -&amp;gt; subtotal + element);
assertThat(result).isEqualTo(21);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;sorted(Comparator&amp;lt;? super T&amp;gt; comparator)&lt;br&gt;
Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// sorted accept integer with negative and positive number. But if the stream is not integer, better use Compartor instead
Stream.of(6,9,3,0,1)
.sorted((x,y)-&amp;gt;x-y)
.forEach(i -&amp;gt; System.out.print(i + " "));
// result: 0 1 3 6 9

List&amp;lt;User&amp;gt; users = Arrays.asList(
            new User("C", 30),
            new User("D", 40),
            new User("A", 10),
            new User("B", 20),
            new User("E", 50));

List&amp;lt;User&amp;gt; sortedList = users.stream()          .sorted(Comparator.comparingInt(User::getAge))
            .collect(Collectors.toList());

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Comparator&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        String str = "welcome to code decode and code decode welcome you";
        Map &amp;lt;String, Long&amp;gt; map = Arrays.asList(str.split(" "))
                .stream()
                .collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
        System.out.println(map);
// result: {code=2, and=1, to=1, decode=2, welcome=2, you=1}

// Convert String to uppercase and join them with coma
list.stream().map(x-&amp;gt;x.toUpperCase()).collect(Collectors.joining(","));

// Statistics 
IntSummaryStatistics statistics stat = list.stream().mapToInt(x-&amp;gt;x) // IntStream
.summaryStatistics()

int min = statistics.getMin();
int max = statistics.getMax();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;IntStream&lt;br&gt;
range(int startInclusive, int endExclusive)&lt;br&gt;
Returns a sequential ordered IntStream from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1.&lt;br&gt;
rangeClosed(int startInclusive, int endInclusive)&lt;br&gt;
Returns a sequential ordered IntStream from startInclusive (inclusive) to endInclusive (inclusive) by an incremental step of 1.&lt;br&gt;
&lt;code&gt;IntStream.range(0,count)&lt;/code&gt; = &lt;code&gt;Stream.iterate(2, n -&amp;gt; n+1).limit(count)&lt;/code&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
