Sunday 15 February 2015

Best Of Collection Question In Java.


Differences between HashSet and HashMap in Java
HashSet internally uses HashMap to store objects.when add(String) method called it calls HahsMap put(key,value) method where key=String object & value=new Object(Dummy).so it maintain no duplicates because keys are nothing but Value Object.
the Objects which are stored as key in Hashset/HashMap should override hashcode & equals contract.
Keys which are used to access/store value objects in HashMap should declared as Final because when it is modified Value object can't be located & returns null.

How HashSet Internally Works in Java:-

http://java67.blogspot.in/2014/01/how-hashset-is-implemented-or-works-internally-java.html

------------------------------------------------------------------------------------------------------------------

How HashMap works in java

http://www.javacodegeeks.com/2014/03/how-hashmap-works-in-java.html
 or
http://javahungry.blogspot.com/2013/08/hashing-how-hash-map-works-in-java-or.html?_sm_au_=iVVq5Q1F10JH7NHN

http://javapapers.com/core-java/java-hashtable/
------------------------------------------------------------------------------------------------------------------

http://www.java2blog.com/2014/02/how-hashmap-works-in-java.html

http://javarevisited.blogspot.in/2011/02/how-hashmap-works-in-java.html

Watching vedio must help you:-
https://www.youtube.com/watch?v=iinE6ZBNBjw
------------------------------------------------------------------------------------------------------------------

HashMap – Single Key and Multiple Values Example


http://java.dzone.com/articles/hashmap-%E2%80%93-single-key-and
-----------------------------------------------------------------------------------------------------------------

How to add duplicate key in map:-

http://stackoverflow.com/questions/18922165/how-to-include-duplicate-keys-in-hashmap

if you want to do manually we can use this trick

 if ( ! map.containsKey( key ) ) {
            List list = new ArrayList( );
            list.add( value);
            map.put( key, list);
        }
        else {
            List list = (List) map.get(key);
            list.add( value );
        }

Or if you want to have a single list.add():
        if ( ! map.containsKey( key ) ) {
            List list = new ArrayList( );
            map.put( key, list);
        }
        
        List list = (List) map.get(key);
        list.add( value );
---------------------------------------------------------------------------------------

0r

http://java.dzone.com/articles/allowing-duplicate-keys-java

---------------------------------------------------------------------------------------------------

Why Use Inner Classes?

There are several compelling reasons for using nested classes, among them:
  • It is a way of logically grouping classes that are only used in one place.
  • It increases encapsulation.
  • Nested classes can lead to more readable and maintainable code.
  • ---------------------------------------------------------------------
  • -One good usage of inner classes that comes into my mind is in java.util.ArrayList that hides its iterators implementations into private inner classes. You can't create them except by invoking iterator() or listIterator() on the list object.
    This way the Iterator and ListIterator implementations for ArrayList are grouped with their related class and methods for enhanced readability (the implementations are pretty short), but hidden from others.
----------------------------------------------------------------------------------------------------------------
Best of hash Code by Eclipse
http://eclipsesource.com/blogs/2012/09/04/the-3-things-you-should-know-about-hashcode/

-------------------------------------------------------------------------------------------------------------
ArrayList vs Linked List(when to use linked list)
http://beginnersbook.com/2013/12/difference-between-arraylist-and-linkedlist-in-java/
--------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment