Nitin Agrawal
Contact -
  • Home
  • Interviews
    • Secret Receipe
    • InterviewFacts
    • Resume Thoughts
    • Daily Coding Problems
    • BigShyft
    • CompanyInterviews >
      • InvestmentBanks >
        • ECS
        • Bank Of America
        • WesternUnion
        • WellsFargo
      • ProductBasedCompanies >
        • CA Technologies
        • Model N India
        • Verizon Media
        • Oracle & GoJek
        • IVY Computec
        • Nvidia
        • ClearWaterAnalytics
        • ADP
        • ServiceNow
        • Pubmatic
        • Expedia
        • Amphora
        • CDK Global
        • CDK Global
        • Epic
        • Sincro-Pune
        • Whiz.AI
        • ChargePoint
      • ServiceBasedCompanies >
        • Altimetrik
        • ASG World Wide Pvt Ltd
        • Paraxel International & Pramati Technologies Pvt Ltd
        • MitraTech
        • Intelizest Coding Round
        • EPAM
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems
      • AnagramsSet
    • Core Java >
      • Codility
      • Program Arguments OR VM arguments & Environment variables
      • Java Releases
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
        • Future Or CompletableFuture
      • Important Points
      • Immutability
      • Dictionary
      • 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
        • Decorator
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • RequestChaining
        • Singleton >
          • Singletons
  • Frameworks
    • Apache Velocity
    • Spring >
      • Spring Boot >
        • CustomProperties
        • ExceptionHandling
        • 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
  • Databases
    • MySql
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search
  • Random issues
    • TOAD issue
    • Architect's suggestions
  • Your Views

Create your own HashMap

4/24/2018

1 Comment

 
I have observed that in many interviews this question is asked - How will you create your own HashMap class in Java? last time I was asked this question in Globant interview.
​I always wonder & want to say that why do I need to create my HashMap if I am using Java.
​Rather this question should be - Do you know the working of HashMap in Java? - This right question was asked to me in ​Globallogic interview.

​So one can get lot of pages on this working on the internet, so not going in that.
​But will tell that working, also will explain the scenario when its performance will get hit, like if all the keys are returning the same hashcode, causing a long linked list & again the searching in that will be O(n) & not O(1).
​So hashcode() must be efficient enough to return unique hashcode for different keys. And need to look thing when using custom objects as keys. Also in hashcode() you can look to use hashcode() of String class if you have String properties in your objects. Also take care of the immutability of such objects.
​Also tell the improvements done in new Java versions i.e. since jdk1.8 where it is also using balanced binary tree for certain threshold.
​Can discuss about the concurrency in your hashmap.
Can tell about why ordering in HashMap is not decided, because this ordering is dependent on the memory location of the keys & that location decided at the runtime depending on the address of the available memory to store that key. But once HashMap is populated & you are not making any change its data, then every time you iterate through this HashMap, you will get the elements in the same order everytime.
In newer versions of Java, check for the version mabe it is 9+, this ordering will not be present.

​And during the interview with Globant on this, I used method set() instead of put() in actual HashMap class, then I was questioned that when put() is used in actual class then why I have used set(), & this is more like to receive one parameter & we need to send inputs i.e. key & its value.
I was wondering on this question that -
​1) Why I am creating HashMap class when such class is in Java?
​2) set() takes one input only, it is not a rule, it is just a convention being followed during coding
​    in general.
​3) Its my class, I can give any relevant names to methods here, why you are comparing with
    any original class.

1 Comment
Lee link
8/28/2021 12:13:13 pm

Thanks for writting this

Reply



Leave a Reply.

    Author

    Nitin Agrawal

    Archives

    April 2018

Powered by Create your own unique website with customizable templates.