COLLECTION



Most frequent collection question:
http://javadeveloperchoiceno1.blogspot.in/p/collection.html

for experience interview must see the below link:-
http://javadeveloperchoiceno1.blogspot.in/p/experience-interview.html

--------------------------------------------------------------------------------
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/
--------------------------------------------------------------------------------------------------------------

equals vs comareTo:--

http://javadeveloperchoiceno1.blogspot.in/2015/02/what-does-comparison-being-consistent.html
-----------------------------------------------------------------------------------------------------------------

Difference between Java Comparator and Comparable interfaceshttp://javadeveloperchoiceno1.blogspot.in/2015/02/difference-between-java-comparator-and.html

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

Java Collections Class:--
he java.util.Collections class consists exclusively of static methods that operate on or return collections.Following are the important points about Collections:
  • It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection.
  • The methods of this class all throw a NullPointerException if the collections or class objects provided to them are null.
Most Important methods are given below:-

---------------------
List emptylst = Collections.emptyList();
return empty imutable list:
 //   emptylst.add("A"); adding this will lead an  error.

-----------------------------
List<String> synlist = Collections.synchronizedList(arlst);
 Map<String,String> synmap = Collections.synchronizedMap(new HashMap());
Set<String> synset = Collections.synchronizedSet(new TreeSet());

these are used for synchronization purpose safety in Multithradiding:-
   
-----------------------------------------
Collections.sort(arlst); for natural sorting
 Collections.sort(arlst,null); Null can reaplace with object for comparator :-customize sorting
System.out.println("now arlst is "+arlst);

-----------------------------------------------------------
 int freq = Collections.frequency(arlst, "PP");

it is used to find the freq of the word pp in list:-

------------------------------------------------------
 Collections.reverse(arlst);
------------------------------------------------
System.out.println("value is"+Collections.max(arlst));
System.out.println("value is"+Collections.min(arlst));
---------------------------------------------------------------
 int index=Collections.binarySearch(arlst, "QUALITY");
Collections.copy(arlst,destlst);
------------------------------------------------------------------
 Collection<String> immutablelist = Collections.unmodifiableCollection(arlst);
 Map m = Collections.unmodifiableMap(new HashMap());
  Set unmodset = Collections.unmodifiableSet(new HashSet());
  immutablelist.add("raj");

it's kind of imutable once create the object fill the value once. want  to unmodifable just call this method:-

---------------------------
Collections.checkedList() method.
List myList = new ArrayList();
        myList.add("one");
        myList.add("two");
        myList.add("three");
        myList.add("four");
        List chkList = Collections.checkedList(myList, String.class);
        System.out.println("Checked list content: "+chkList);
        //you can add any type of elements to myList object
        myList.add(10);
        //you cannot add any type of elements to chkList object, doing so
        //throws ClassCastException
        chkList.add(10); //throws ClassCastException

Basic fund behind is .nongenric to genric ...


mostly we use list and ArrayList in collections of java.
------------------------------------------------------------------------------------------------------------------

Arrays:-

List and array are same thing..difference only array is fixed size where is arraylist dynamic,with any kind of value but if putting genrice again  it will similar to array....that's the reasone the thing you want to do ..using array can convert it to arralylist .just use array list method ,,and do your work again convert it to array...using list.toArray() method. and vice versa also possible,.
This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.

1:
int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };
      Arrays.sort(array);
      printArray("Sorted array", array);
      int index = Arrays.binarySearch(array, 2);
      System.out.println("Found 2 @ " + index);
adding two array together:- String a[] = { "A", "E", "I" };
      String b[] = { "O", "U" };
      List list = new ArrayList(Arrays.asList(a));
      list.addAll(Arrays.asList(b));
      Object[] c = list.toArray();
list=Arrays.asList(a); inside list we have two array method to get array from list
ArrayList vs Array

two array equals:-
   int[] ary = {1,2,3,4,5,6};
      int[] ary1 = {1,2,3,4,5,6};
      int[] ary2 = {1,2,3,4};
      System.out.println("Is array 1 equal to array 2?? "
      +Arrays.equals(ary, ary1))
 Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};
      int min = (int) Collections.min(Arrays.asList(numbers));
Conclusion:-
you can convert array to arraylist and use Collections class and arrayList to array and use Arrays Class:--
---------------------------------------------------------------------------------------
Multithreading:-
http://javarevisited.blogspot.in/2011/04/synchronization-in-java-synchronized.html


No comments:

Post a Comment