Monday, September 25, 2023

Java Collection Framework

 What is fail fast and feel safe in Java collection framework?

  • Failsafe and FailFast In Java collection framework.
    • Failsafe, Iterator doesn’t throw Any exception contrary to fail fast Iterator, If collection is modified structurally, while one thread is iterating over it.
      • This is becauseFail safe work on clone of collection instead of original collection and that’s why they are called as fail safe Iterator.Iterator of CopyOnWriteArrayList Is an example of fail safe Iterator also Iterator Written by concurrent hash mapKey set is also fail safe Iterator And never throw ConcurrentModificationException.
    • Fail Fast Iterator
      • When we use fail fast Iterator It immediately throws ConcurrentModificationException When an element is added or removed from the collection, while the thread is iterating over the collection, example Iterator On hash map,Iterator On array list etc.

What are the some of the bases on which collections can be chosen?

  • ArrayList
    • When we want the insertion order to be maintained
  • Linked List
    • When the insert and update operations are more.
  • Tree Set
    • When we want the elements to be sorted

What is Unmodifiable/Immutable map In collection?

What is a difference between list and set?

  • List can contain multiple values with duplicates, whereas set contains multiple values where values cannot be duplicates.
  • Order is maintained in a list, whereas order is not maintained in a set.
How can we minimise hashing collision?
  • We can have our own implementation for hash code and equals method and Minimise hash collision.
What are the different techniques to resolve collusion in collections?
  • Chaining(open hashing)
  • Open Addressing
    • It has three parts
      • Linear probing
        • We add in the next available space if collision has occurred.
      • Quadratic probing
        • We use R+i pow 2 mod n
        • R Is the hashed value for Which collision has occurred.
        • I is the probed value That is how many times we have checked for hash collision value.
        • N is the length of map.
      • Double Hashing
        • We use two hash functions here one to resolve collision And other to calculate hash.
  • If we uniformly distribute elements over hash map, then time complexity on average will decrease.
    • We can use our own hash function for uniform distribution of elements per slot.
  • Perfect uniform distribution has only(n/m) Elements per slot.
    • Search time is equal to Big O n/m= Big O(1+n/m)
Tree Set
  • Complexity is O log n
  • Maintains natural order of elements
  • It is not thread safe, that is not synchronised.
  • We said directly implement,navigable set, but in directly implement, sorted, set and set interface.
  • The underlined data structure is Balanced tree.
  • It is not index base data structure.
  • Does not follow insertion order.
  • It follows, sorting order
  • Uses compare to method to compare and store values in Balanced tree.
  • Tree set stores Homogeneous elements that is same data type elements.
  • We cannot store duplicate elements.
  • We can’t add null values.
  • Was introduced in Java 1.2

No comments:

Post a Comment