Nitin Agrawal
Contact -
  • Home
  • Interviews
    • Secret Receipe
    • InterviewFacts
    • Resume Thoughts
    • Daily Coding Problems
    • BigShyft
    • Companies
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems >
        • Problem10
        • Problem300
      • AnagramsSet
    • Core Java >
      • Codility
      • Program Arguments OR VM arguments & Environment variables
      • Java Releases >
        • Java8 >
          • Performance
          • NasHorn
          • WordCount
          • Thoughts
        • Java9 >
          • ServiceLoaders
          • Lambdas
          • List Of Objects
          • Code Snippets
        • Java14 >
          • Teeing
          • Pattern
          • Semaphores
        • Java17 >
          • Switches
          • FunctionalStreams
          • Predicate
          • Consumer_Supplier
          • Collectors in Java
        • Java21 >
          • Un-named Class
          • Virtual Threads
          • Structured Concurrency
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
        • Future Or CompletableFuture
      • Important Points
      • Immutability
      • Dictionary
      • Sample Code Part 1 >
        • PatternLength
        • Serialization >
          • Kryo2
          • JAXB/XSD
          • XStream
        • MongoDB
        • Strings >
          • Reverse the String
          • Reverse the String in n/2 complexity
          • StringEditor
          • Reversing String
          • String Puzzle
          • Knuth Morris Pratt
          • Unique characters
          • Top N most occurring characters
          • Longest Common Subsequence
          • Longest Common Substring
        • New methods in Collections
        • MethodReferences
        • Complex Objects Comparator >
          • Performance
        • NIO >
          • NIO 2nd Sample
        • Date Converter
        • Minimum cost path
        • Find File
      • URL Validator
    • Julia
    • Python >
      • Decorators
      • String Formatting
      • Generators_Threads
      • JustLikeThat
    • Go >
      • Tutorial
      • CodeSnippet
      • Go Routine_Channel
      • Suggestions
    • Methodologies & Design Patterns >
      • Design Principles
      • Design Patterns >
        • TemplatePattern
        • Adapter Design Pattern
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • Singleton >
          • Singletons
        • Strategy
  • Frameworks
    • Apache Velocity
    • React Library >
      • Tutorial
    • Spring >
      • Spring Boot >
        • CustomProperties
        • ExceptionHandling
        • Custom Beans
        • Issues
      • Quick View
    • Rest WebServices >
      • Interviews
      • Swagger
    • Cloudera BigData >
      • Ques_Ans
      • Hive
      • Apache Spark >
        • ApacheSpark Installation
        • SparkCode
        • Sample1
        • DataFrames
        • RDDs
        • SparkStreaming
        • SparkFiles
    • Integration >
      • Apache Camel
    • Testing Frameworks >
      • JUnit >
        • JUnit Runners
      • EasyMock
      • Mockito >
        • Page 2
      • TestNG
    • Blockchain >
      • Ethereum Smart Contract
      • Blockchain Java Example
    • Microservices >
      • Messaging Formats
      • Design Patterns
    • AWS >
      • Honeycode
    • Dockers >
      • GitBash
      • Issues
      • Kubernetes
  • Databases
    • MySql
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search
  • Random issues
    • TOAD issue
    • Architect's suggestions
  • Your Views

MapMulti

1/2/2022

0 Comments

 
 Remember?
 Once we got methods like limit() & skip() which took concrete long type value and people were struggling about the conditions where they don't have such concrete long values but their situation was conditional, means they needed some method which can take the predicate to decide on the number of elements to accept or skip.
  So?
 We got takeWhile() & dropWhile() methods which take predicate & the above constraint was resolved now.
  Why am I saying this?
 Till date we were having flatMap() only if we need to go from Stream of Stream of T to Stream of T.
                                                                    OR
 If we need to go from Stream of T to...ummm...may be to Stream of R or may be we need to move to Stream of T but after some calculations.
 So what we were doing for this?
  flatMap() ?
 Naaaaaaa....we did use of map() or may be filter() also
                                                                  OR
 One wants to move from one to many i.e. you are getting a stream of T type elements & you want to process each element to generate more elements from each element received from the given stream.
  So what we did earlier?
  We used map() to generate the collection of those new elements, then we did flattening using flatMap(). Well it worked but it was also getting performance hit due to intermediate Streams.
  Whats the solution now?
​ Use mapMulti(), as shown below in oneToMany()

 I am not sure, but may be some people had issues due to intermediate streams being created by intermediate actions above, which can hit the performance badly if your stream is huge.
 So, we got mapMulti() now in Java16, which as per my below way is quite efficient but check by your way also before you hit this road....

Note :- Feel free to comment any of your concern regarding the thoughts shared here.

  Check the below code I wrote to guage the performance -

    
0 Comments



Leave a Reply.

    Author

    Nitin Agrawal.

    Archives

    January 2022
    December 2021

    Categories

    All

Powered by Create your own unique website with customizable templates.