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
        • ThreadPoolExecutor
        • RecursiveTask
        • 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
    • Dynamic loading of agents
  • Your Views

Find the count of city names in the given array

7/27/2022

0 Comments

 
You are given a list of city names & you need to find the number of times each name is appearing in the given list.
e.g. : List<String> cities = List.of("a","b","c","b","d","a");
 o/p should be like : ​{a=2, b=2, c=1, d=1}

    
I think we can also use reduce(), but haven't tried that yet. And will be again giving some
complex solution.
0 Comments

Find the duplicates in the List of Objects using Stream

4/24/2022

0 Comments

 
In the other post we saw how to find duplicates in the List of String & the same approach we can use for this problem also.
But there is a catch here...What is that?
In case of primitives & String, Java knows how to compare or how to establish the equivalency
but in case of custom object, Java needs help for this, we all know this.
 So, here if the objects in your collections are Entity ones, then I expect that those will be
having hashCode() & equals() overriden. Then you will not see any surprise but if these
methods are not overriden then Java doesn't know how to process & every object will be
treated as unique one & you can see the unexpected result.
 One can follow the below example & can try by commenting out the hashCode() & equals().

    
0 Comments

Finding the duplicates given in the list of String

4/23/2022

0 Comments

 
 Many are getting quite excited to see this functional programming & mostly have just got the
exposure & that too still working with Java8, they like to ask the questions on this & such
questions which possibily they came across recently over the internet or in their interviewes.
 Well the question was asked by some P. Jain from Xebia in one interview -
You are given a list of city names & you need to print those names only, which are getting repeated in the given list, i.e. you need to provide the duplicate names only.
 e.g. : List<String> names = Arrays.asList("delhi", "pune", "pune", "jaipur", "delhi");
In above list, ans should be "pune" & "delhi".
 As suggested on - How to find duplicate elements in a Stream in Java - GeeksforGeeks
​
There can be different ways to get the result but 2 of those are mentioned below which seem clean-

    
0 Comments

Interview: Given array/list of numbers, separate out even and odd numbers.

4/22/2022

0 Comments

 
 First check if it is an Array or List of numbers/integers, as you are supposed to provide the
​code for that.
 Check if you are supposed to solve this problem using Functional way or Imperative style.

Though chances are very high, as you are supposed to write the code for such a simple
problem, then the interviewer is looking your functional way.

 It is to be written using Functional way & if interviewer says you can take anything List or
Array. Then go with List & solution will be like shown below-
Using List

    
 Now if interviewer says that it is array of numbers, then surely that interviewer
has recently worked on such scenario or seen on internet while preparing for the
nterviews.

 As if you are supposed to give the sample using an array, then be careful while declaring
​such array. Why?
 If you declare your array as int[] array, then it needs to be handled in different way.
 So you will need to convert this array to Stream.
 We have 2 ways for this-
 a) Use Arrays.stream()
 b) Use Stream.of();

What's the issue then?
Lets have such array like : int[] premNums = {1,2,3,4,5,6,7,8,9,10};
 Using (a) : IntStream intStream = Arrays.stream(premNums);
Here we get IntStream, & looking this seems no issue. I also thought the same but I didn't
get the way to solve the problem using this IntStream, if anyone knows please comment.

 Using (b) : Stream<int[]> arrayStream = Stream.of(premNums);
Here we see the problem now directly, as we need the stream of numbers but here we
are getting stream of int[].
 Here it becomes bit tricky to get this stream like the way we need.
 Below is the way & note the use of boxed() in flatMap()
Using array of primitive integers

    
 Now if we declare our array of numbers as : Integer[] nums = {1,2,3,4,5,6,7,8,9,10};
 So here this makes the life bit easy & like List of numbers. Why?
Because now if we convert this array to Stream using either (a) or (b) as suggested above, then in both the cases we get stream of Integer, not stream of int[], like shown in below code-
Using array of objects or wrapper types

    
0 Comments

Collectors in Java

4/7/2022

1 Comment

 
Collectors in Java, it is huge. The world of collectors is very huge & one can get lost while trying its features, my own experience.

 So below I am giving a glimpse about its power in the new Java world. But I also think that it is too tricky to get the things done which we can do easily with Imperative style & that will be quite expressive to, but again many people think that Functional way is the better one.
 So check below example & you decide & please also suggest the improvements.
​Collections

    
 Another example about Collectors,
what if toMap() get duplicate keys?
How to collect data in Map of Map
ToMaps

    
1 Comment

    Author

    Nitin Agrawal

    Archives

    July 2022
    April 2022

    Categories

    All

Powered by Create your own unique website with customizable templates.