We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3
Now I'm Using A Method Called Print By Passing The Arguments For Map
Means It Should Contain The Map Of Same Mappings. So If It Is Empty Then
Print Map Is Empty Else Return The Elements And That Is The Key Value Pairs Present In The Map. So Let's Run And Check The Output. So First What It Said Comma It Said Map Is Empty Because I'm Printing The Map Without Entering Anything In That. So It Will Go To The Print Method And Check Whether The Map Is Empty Or Not. Yes It Is Empty So It Will Retrieve The Result. So Next This Statement Got Executed And It's Telling The Size Of The Map History Because It Has The Key Value For Three Different Categories. Again It Will Print The Map. So This Will Again Go To The Function Definition And Print This Statement Because Now The Map Is Not Empty. So There Is A Function Call Over Here And Now Size Of The Map Is Map . Size. What Is The Size? Three So It Is Displaying The Size Of The Map As Three. And It's Printing All These Elements Now Value For Key ABC Is Ten. So That's What I'm Checking Over Here. That Is Comma Get The Key For ABC And Print Its Value. So If I Do It Again Comma Like You Know Comma MNO. Again Comma Change It Over Here As Well Comma And Here As Well. OK Comma So It Will Print The Value Will Be Thirty. So Finally After Clearing The Map Again It's Telling Map Is Empty. Now Let's Run And Check The Output. It's MNO. You Can See That The Value For The Key MNO Is Thirty Right? I Hope You Understood This. So This Is How You Can Configure Your Hash Map And Map It Accordingly. So Now Talking About The Complexity Of Hash Map Comma It Provides A Constant Time Complexity For Basic Operations That Is Get And Put If Hash Function Is Properly Written And It Disperses The Elements Properly Among The Buckets. Iteration Over Hashmap Depends On The Capacity Of Hashmap And The Number Of Key Value Pairs. Basically It Is Directly Proportional To The Capacity Plus Size And Capacity Is The Number Of Buckets In The Hash Map. So It Is Not A Good Idea To Keep A Higher Number Of Buckets And Hashmap Initially. So This Is All About The Time Complexity. Now Let's Have A Look At The Various Methods That Are Widely Being Used In A Hash Map. First Void Clear. This Is Used To Remove All The Mappings From A Map. Next Boolean Contains Key That Is Parameter Will Be Object Key. This Is Used To Return True If For A Specified Key Mapping Is Present In That Map. And Again Boolean Contains Value Of Object Value. This Is Yours Return True If One Or More Key Is Mapped To A Specified Value. Next Method Is Object Clone. This Is Used To Return A Shallow Copy Of The Object Of The Mentioned Hash Map. Next Bullen Is Empty. Use To Check Whether The Map Is Empty. Returns True If It Is Empty. Next Object Get Object Key. This Is Used To Retrieve Or Fetch The Value Map By A Particular Key. Set Key Set. This Is Used To Return A Set Views Of The Key. In Size You Should Return The Size Of The Map. Next Object Put Parameters Will Be Key And Value. This Is Used To Return A Particular Mapping Of Key Value Pair Into A Map. Next Comma Put All Of Map M. This Is Used To Copy All Of The Elements From One Map To Other. Next Object Remove Object Key. This Is Used To Remove The Values For Any Particular Key In The Map And The Last One Is Collection Values Versus Used To Return A Collection View Of The Values In The Hash Map. So These Are The Various Methods That Are Widely Being Used In The Hash Map. Let's Understand What Is Generics In Java. Generics Is A Term That Denotes A Set Of Language Features That Are Related To The Definition And The Use Of Generic Types And Methods. Java Generic Methods Differ From Regular Data Types And Methods. So Before Generic We Use The Collections To Store Any Type Of Objects. It Can Be Either A Generic Or Non Generic Object. Now Generics Forced The Java Programmer To Store A Specific Types Of Objects. So Basically This Is All About What Is Generics. Now Let's Move Further And See Why Do You Need Java Generics. If You Look At The Java Collections Framework Then You Will Observe That Most Of The Classes Take Parameter Or Argument Of Type Object. So Basically What Happens In This Form? They Can Take Any Java Type As Argument And Return The Same Object Or Argument. It Can Be Either A Homogeneous Or Heterogeneous That Is Not Of A Similar Type. So Sometimes In The Java Application Comma The Data Type Of The Input Is Not Fixed. The Input Can Be An Integer Comma A Float Comma Or A Java String. In Order To Assign The Input To The Variable Of The Right Data Type Comma Prior Checks Had To Be Conducted. In The Traditional Approach Comma After Taking The Input Comma The Data Type Of The Input Was Checked And Then It Was Assigned To The Variable Of The Right Data Type. When This Logic Was Used Comma The Length Of The Code And Execution Time Was Increased. So To Avoid This Comma Generics Were Introduced. So On Using Generics Comma The Parameters In The Code Is Checked At Compile Time Automatically. And It Sets The Data Type By Default. So This Is Where You Need The Concept Of Generics In Java As The Execution Time And The Time That You Invest In Writing The Code Will Also Be Decreased. So Now That You Know What Is Generics In Java And Why Do You Need It Comma Let's Move Further And See Some Of The Types Of Generics. So Basically There Are Four Types. First Generic Type Class And You Have Interface Comma Method And Constructor. First Let's Understand What Is Generic Type Class. A Class Is Set To Be Generic If It Declares One Or More Types Of Variables. So These Variable Types Are Known As The Parameters Of The Java Class. Let's Understand This With The Help Of An Example. So Here You Can See I Have Created The Class With One Property X And The Type Of The Property Is Object Comma Correct? So What Happens Once You Initialize The Class With The Certain Type Comma The Class Should Be Used. With That Particular Type Only. For Example Comma If You Want One Instance Of A Class To Hold The Value Of Type String Comma Then Programmer Should Set And Get Only String Type. Since I Have Declared The Property Type To Object Comma There Is No Way To Enforce This Restriction. A Programmer Can Set Any Object And Expect. Any Return Value From The Get Method Since All Java Types Are Subtypes Of Object Class. So It's Very Simple Right? So Here I'm Creating A Method For Set And Get And I'm Using The This Keyword For Reference. And Then When I Return The Value Of The X It Can Be Any Type But All The Java Types Are Subtypes Of Object Class. So I Can Set A Property For Integer And I Can Return String Anything It Can Be. So This Is How It Works. The Next Type Is Generic Type Interface And Interface In Java Refers To The Abstract Data Types. They Allow Java Collections To Be Manipulated Independently From The Details Of The Representation. Also The Former Hierarchy In Object Oriented Programming Languages. So Let's Take An Example And Understand This. So Here I Have Created A Interface For Generic And I'm Doing Two Operations For T One And T Two. So For Two What I'm Doing I'm Performing The Execution. I'm Passing Two Variables That Are T One And X And Next What I'm Doing I'm Performing A Reverse Execution Of Whatever I Have Performed In The Upper Statement. So When I Create A Class For Generic And Implement The Interface. Whatever I Perform The Execution Will Be Reversed In The Next Execution. Suppose Say While Performing The Execution Comma I'm Passing A String Variable And That Can Be Easily Converted To An Integer Variable When I'm Reversing The Execution. So Basically All That Matters Is The Type Of The Object And There Is No Way That I Have To Enforce Restriction Or Anything Like That. It Can Be A String Comma Integer Or Double Comma Float Comma Anything. So I Hope You Was Able To Understand How Generics Can Be Applied To Interfaces. Now Let's See What's Next. The Next Type Is Generic Type Method. So Generic Methods Are Much Similar To Generic Classes Comma But They Differ From Each Other In Only One Aspect Comma That Is Comma The Scope Or Type Of Information Is Available Inside The Method Only. Generic Methods Introduce Their Own Type Parameters. So Let's Take An Example And Understand This. So In This Case What Happens If You Pass A List Of String To Search In The Method It Will Work Fine But If You Try To Find Number In The List Of First String It Will Give Compile Time Error. Here's Right Because See First I'm Using Advanced For Loop. I'm Trying To Check The Element Present In The List. If The List Isn't String Then It Will Match Because Always List Is A String. Like List Can Be A String Comma It Can't Be A Integer.