Thursday, 6 July 2023

Set (Interface) & Child Classes of Set Interface

Set

  • Set is Child Interface of Collection.
  • If we want to represent a group of Individual Object as a Single entity, Where Duplicates are not allowed & Insertion Order Not Preserved.
  • Set Interface doesn't have Contain any New Method and we have to use Only Collection Interface Method.



HashSet(Interface)

  • The Underlying Data Structure is HashTable.
  • Duplicates objects are not allowed.
  • Insertion Order is not Preserved & It is based on Hash Code of Objects.
  • Null Insertion is Possible.
  • Heterogeneous Objects are allowed.
  • Implements Serializable & Cloneable but Not Random Access Interface.
  • Hash Set is the Best choice If Our Frequent Operation is Search Operation.
Note:
  • In HashSet Duplicates are Not allowed, If we are trying to Insert Duplicates than we won't get any Compile/Run Time errors & add Method Simply Returns False.
Constructor
  1. HashSet h = new HashSet();
  • Creates an Empty HashSet Objects with Default Intial Capacity is 16 & Default Fill Ratio 0.75.
  2.HashSet h = new HashSet(int intialCapacity);
  • Creates an Empty HashSet Objects with Specified Intial Capacity & Default Fill Ratio 0.75.
  3.HashSet h = new HashSet(int intialCapacity,float fillRatio);
  4.HashSet h = new HashSet(Collection c);
  • Creates an Equivalent HashSet for the given Collection.
  • This Constructor meant for Inter Conversion B/W Collection Objects.
LinkedHashSet
  • It is Child Class of HashSet.
  • It is Exactly Same as HashSet {Including Constructor & Method}.
 Except Some Differences B/W HashSet & LinkedHashSet.


SortedSet
  • It is a Child Interface of Set.
  • If we want to Represent A group of Individual Objects according to Some Sorting Order without Duplication than we should go for SorteSet.
Methods of SortedSet
  • first()-> (1oo)[Return First element].
  • last()->   (110)[Return Last element].
  • headSet()->(100,101,102)[Return less than object].
  • tailSet()-> (106,108,110)[Return greater than object].
  • subSet()-> [Return greater & equal(>=) than object & less than & equal(<=) than object].
  • comparator()-> Returns Describe underlying Sorting Technique. if we are using Default Natural Sorting Order than we will get Null. 
  • for eg:
100
101
102
104
106
108
110
Note:-
  • Numbers-> Default Natural Sortiong Order [Ascending Order]
  • String ->Default Natural Sorting Order [Ascending Order]

NavigableSet
  • NavigableSet is Child class of Sorted Interface.
  • Duplication are Not allowed.
  • Insertion Order Not Preserved.
  • Heterogeneous Objects are allowed.
  • Null Insertion is Possible.
  • NavigableSet implements Serializable & Cloneable but not Random Access Interface.
  • NavigableSet interface provides navigation methods and descending iterator that allows the elements in the set can be traversed in descending order.

TreeSet
  • The Underlying Data Structure is Balanced Tree.
  • Duplication are Not allowed.
  • Insertion Order Not Preserved.
  • Heterogeneous Objects are Not allowed.
  • Null Insertion is Possible.
  • TreeSet implements Serializable & Cloneable but Not Random Access Interface.
  • All Objects will be Inserted based On some Sorting Order.
  • It may be Default Natural Sorting Order .
Constructor
  1. TreeSet t = new TreeSet();
  • Create an Empty TreeSet Object where, the elements will be Inserted according to Default Natural Sorting Order.
  2.TreeSet t = new TreeSet(Comparable c);
  • Create an Empty TreeSet Objects where, the elements will be Inserted according to Customizer Sorting Order Specified by Comparator Object.
  3.TreeSet t = new TreeSet (Collection c);
  4.TreeSet t = new TreeSet (SortedSet s);




No comments:

Post a Comment

Servlet in AEM/Sling Servlet

  What is Servlet? A servlet is a Java class that runs on the server side where the application resides. It acts as a middle layer between t...