• No results found

2786) Collection Frame Work

In document Core_java (Page 90-103)

 Frame work is a collection of many utilities which are present in a class or an interface.  Collection frame work defines several class and interfaces which can be used to represent a

group of objects as a single entiry

 Collection frame work gives many readymade utilities, has a developers it’s our responsibility to use the readymade things as per the program requirement

All the class and interface of collection frame work is present in java.util package 2787)

2788) Difference between array and collection 2789)

2790) Array 2791) Collection

2792) Stores primitive has well has non primitive.

2793) Store only non primitive (Objects)

2794) It’s a fixed size 2795) Its dynamically growing

2796) We don’t have any methods to deal with any data hence may not give object oriented approach

2797) We have many utility methods and also many methods that deals with data hence gives object oriented approach

2798) Memory point of view array is not recommended

2799) It is recommended, because memory increases has per the load 2800)

2801)

2802) Collection

2803) A group of individual objects as a single entity is called has collection.  If we want to represent a group of individual objects into one entity than we should go for

Collection

 The Collection is a interface present in the java.util package

 The Collection interface contains lot of methods which can be applicable for any Collection type Objects

2805)

 Collection and Map interfaces are the part of collection framework but they are two separate vertical.

 Map is not a child of collection interface

 Collection and Map both can be used with generics or can be used independently without generics

 We cant use any primitives(int,float,double….etc) with generics

 Generic should be any object type not any other type (Integer, Double, Student…… etc)

2806)

2807) List

 It’s a child interface of Collection, such that List interface extends Collection interface  If you want to represent the group of objects in the order of insertion and if you want to

allow the duplicate values than you can go for List.  Index based

 Ordered (insertion order )  Accepts duplicate

 Multiple null values are accepted  List has many child class

o ArrayList Class o LinkedList Class o Vector Class

Stack sub Class of Vector 2808) ArrayList

2809) The object of ArrayList can be created in three ways has shown below

2810) ArrayList al1 = new ArrayList();

2811) ArrayList al2 = new ArrayList(int inetialCapacity); 2812) ArrayList al3 = new ArrayList(Collection ref);

2814) LinkedList

2815) The object of LinkedList can be created in two ways has shown below

2816) LinkedList li1 = new LinkedList();

2817) LinkedList li2 = new LinkedList(Collection); 2818) Collection means any Collection type object

2819) Vector

2820) The object of Vector can be created in four ways 2821) Vector v1 = new Vector();

2822) Vector v2 = new Vector(Collection);

2823) Vector v3 = new Vector(int inetialCapacity);

2824) Vector v4 = new Vector(int inetialCapacity, int growthRate); 2825) Growth rate means the rate at which the vector grows after filling the allotted memory.

2826) Stack

2827) The object of Stack can be created only in one way

2828) Stack s1 = new Stack();

2829) Special methods of stack class

2830) Object push(Object o) push the object into stack

2831) Object pop( ) remove the top most Object from stack

2832) Object peek( ) gives you the top most Object of stack 2833) boolean empty( ) gives true if stack is empty else false

2834) int search(Object o) return position or the index of the object if found or else it return -1

2835)

2836)

2837) Set

 It’s a child interface of Collection, such that List interface extends Collection interface  Non-index based

 No duplicates are allowed  Un-ordered

 Accepts only one NULL

 Set has some child class and interfaces o HashSet Class

LinkedHashSet sub Class of HashSet o SortedSet child interface of Set

NavigableSet child interface of SortedSetTreeSet Class

2838) SortedSet

2839) It’s a child interface of Set

2840) If we want to represent a group of individual objects according to some sorting order than we should go for SortedSet

2841) NavigableSet

2842) It is the child interface of SortedSet to provide several methods for navigation purpose

2843) It was introduced in 1.6 2844)

2845) HashSet

2846) The object of HashSet class can be created in the four ways by calling any one of the below constructor

2847) HashSet hs1 = new HashSet();

2848) HashSet hs2 = new HashSet(Collection);

2850) HashSet hs4 = new HashSet(int inetialCapacity , float incrementRatio);

2851)

2852) incrementRatio it can be give in the form of floating values , like 0.25 which is nothing but the HashSet need to grow by 25% of its size whenever it is filled full.

2853)

2854) LinkedHashSet

2855) The object of LinkedHashSet class can be created in the four ways by calling any one of the below constructor

2856) LinkedHashSet hs1 = new LinkedHashSet ();

2857) LinkedHashSet hs2 = new LinkedHashSet (Collection);

2858) LinkedHashSet hs3 = new LinkedHashSet (int inetialCapacity); 2859) LinkedHashSet hs4 = new LinkedHashSet (int inetialCapacity , float

incrementRatio); 2860)

2861) incrementRatio it can be give in the form of floating values , like 0.25 which is nothing but the HashSet need to grow by 25% of its size whenever it is filled full.

2862)

2863) TreeSet

2864) The object of TreeSet can be created in four ways has shown below 2865) TreeSet ts1 = new TreeSet();

2866) TreeSet ts2 = new TreeSet(Collection); 2867) TreeSet ts3 = new TreeSet(Comparator); 2868) TreeSet ts4 = new TreeSet(SortedSet);

2869) Comparator ,means any Object of the class which implements the Comparator interface

2870) SortedSet , means any Object of the Class which implements the SortedSet interface

2871)

2872) Queue

2873) Is a collection for holding elements prior to processing. Besides basic Collection operations, queues provide additional insertion, removal, and inspection operations.

2874)

2875) Priority queue

 This is the data structure to hold a group of individual objects prior to processing according to some priority

 The priority can be either default natural sorting order or customized sorting order

 If we want to sort user created objects than they should be Comparable otherwise we will get a ClassCastException

 Duplicate objects are not allowed  Insertion order is not preserved  Null cannot be inserted

2876)

2877) import java.util.PriorityQueue; 2878) public class QueExample {

2879) public static void main(String[] args) {

2880) PriorityQueue p = new PriorityQueue(); 2881)

2882) p.add("vikas"); 2883) p.add("anamika"); 2884) p.add("sindrela"); 2885) p.add("santhu");

2886) System.out.println("Elements of queue are "); 2887) System.out.println(p);

2888)

2889) System.out.println("Use of peek()"); 2890) System.out.println(p.peek()); 2891)

2892) System.out.println("Queue after using peek()"); 2893) System.out.println(p);

2894)

2895) System.out.println("Use of pool()"); 2896) System.out.println(p.poll());

2897)

2898) System.out.println("Queue after using pool()"); 2899) System.out.println(p);

2900) }

2901) }

2902) OUTPUT

2903) Elements of queue are

2904) [anamika, santhu, sindrela, vikas] 2905) Use of peek()

2906) anamika

2907) Queue after using peek()

2908) [anamika, santhu, sindrela, vikas] 2909) Use of pool()

2910) anamika

2911) Queue after using pool() 2912) [santhu, vikas, sindrela] 2913)

2914)

2915) boolean offer(Object obj) to add the objects into the queue

2916) Object element() to return head element of the Queue. If the Queue is empty than we will get RuntimeException saying NoSuchElementException

2917) Object peek() it gives the top most element present in the queue, but it will not remove the topmost element. If the Queue is empty than this method will retun null

2918) Object pool() it give u top most element present in the queue, but it will also remove that element.

2919) Object remove() to remove and return the head element of the queue. If the Queue is empty than we will get RuntimeException saying NoSuchElementException

2920) All elements in the queue will be getting arranged in their values order

2921)

2922) 2923)

2924) Legacy class

2925) There was no collection framework in the early version of java. There were several classes and interfaces that provided the methods for storing the objects. These classes where re constructed in 1.2, and these classes are called has legacy class.

2926) They are 1. Dictonary 2. HashTable 3. Properties 4. Stack 5. Vector

2927) There is one Legacy interface that is Enumeration. 2928)

2929) Cursors

2930) If we want to get the Objects one-by-one from the collection we should go for cursors

2931) There are three types of cursors

2932) Enumeration

2933) Iterator

2934) ListIterator

2935)

2936) Enumeration

2937) It is applicable only for the legacy class

2938) We can create Enumeration object by using elements( ) method 2939) It has two more methods

2940) boolean hasMoreElements( )

2941) Object nextElement( )

2942)

2943) Difference between LIST and SET 2944)

2945) List 2946) Set

2947) List allow duplicate elements 2948) No duplicates allowed 2949) List maintains insertion order 2950) No order

2951) All multiple null values 2952) Allow only one null value

2953) List is index based 2954) Not index based

2955)

2956) Difference between ArrayList and vector 2957)

2958) ArrayList 2959) Vector

2962) ArrayList is fast 2963) Vector is slow 2964) ArrayList increases by half of its

size when its size is increased.

2965) Vector doubles the size of its array when its size is increased.

2966) ArrayList came after JDK 1.2 has a part of collecyion frame work

2967) Vector came along with JDK 1.0, later it became a part of collection frame work

2968) iterator and listIterator returned by ArrayList are fail-fast

2969) Enumeration returned by Vector is not fail-fast.

2970) 2971) 2972)

2973) Difference between ArrayList and LinkedList 2974)

2975) ArralyList 2976) LinkedList

2977) Search operation is pretty fast 2978) search operation is slow when compared to arraylist

2979) Adding and removing the elements is slow

2980) Adding and removing the elements is fast

2981) Iterating the elements is fast 2982) Iterating the elements is slow 2983) Memory consumption is less 2984) Memory consumption is more 2985)

2986) Difference between HashMap and HashSet 2987)

2988) Hash Map 2989) Hash Set

2990) HashMap is a implementation of Map interface

2991) HashSet is an

implementation of Set Interface

2992) HashMap Stores data in form of key value pair

2993) HashSet Store only objects

2994) Put method is used to add element in map

2995) Add method is used to add element is Set

2996) In hash map hashcode value is calculated using key object

2997) Here member object is used for calculating hashcode value which can be same for two objects so equal () method is used to check for equality if it returns false that means two objects are different.

2998) HashMap is faster than hashset because unique key is used to access object

2999) HashSet is slower than Hashmap

3000)

3001) Difference between hashmap and hashtable 3002)

3003) HashMap 3004) HashTable

3005) is non-synchronized 3006) is synchronized

3007) HashMap allows one null key and any number of null values

3008) Hashtable does not allow null keys or values.

3009) HashMap is not synchronized it perform better than Hashtable.

3010) Less performance

3011) 3012)

3013)

3014) Difference between Iterator and ListIterator

3015) Iterator 3016) ListIterator

3017) Iterator to traverse Set and List and also Map type of Objects.

3018) List Iterator can be used to traverse for List type Objects, but not for Set type of Objects. 3019) Methods in Iterator : 1. hasNext() 2. next() 3. remove() 3020) Methods in ListIterator 1. hasNext() 2. next() 3. previous() 4.hasPrevious() 4. remove()

5. nextIndex() 6. previousIndex() 3021) iterator you can move only

forward

3022) List Iterator you can move back word also while reading the elements. 3023) Not possible with iterator 3024) list iterator you can add new element at any

point of time

3025) not possible with iterator 3026) list iterator you can modify an element while traversing

3027)

3028) Comparable 3029) Comparator

3030) We can use Comparable to define default natural sorting order

3031) We can use Comparator to define customized sorting order

3032) This interface present in java.lang package

3033) This interface present in java.util package

3034) Defines only one method that is compareTo( )

3035) Defines 2 methods 3036) compare( ) equals( ) 3037) Class implements Comparable

interface

3038) Implements Comparator interface 3039)

3040) The Map Interface

3041) A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value.

3042)

 When ever we need to represent a group of Objects has pair of key and value we should go with map

 Both the key and the value should be of Object type

 Duplicate key is not allowed but the values can be duplicated  Each key-value pair is called Entry

 There is no relation ship between Collection and Map  Methods

3043) Object put(Object key , Object value) 3044) Object putAll(Map map)

3045) Object get(Object key) 3046) Object remove(Object key) 3047) boolean isEmpty( )

3048) boolean containsKey( Object key ) 3049) boolean containsValue( Object value )

3050) int size( ) 3051) 3052) Set keySet( ) 3053) Set entrySet( ) 3054) Collection values( ) 3055) 3056) Entry (interface)

 Key-value pair is called has Entry

 Without existing map object there is no chance of Entry hence, interface entry is defined inside Map Interface

3057) 3058)

3059) HashMap 3060) HashTable

3061) Is not synchronized 3062) It is synchronized

3063) Any number of threads can operate simultaneously on the HashMap methods

3064) Only one thread can act on the methods of HashTabel, hence it is thread safe

3065) It accepts null for both key and value

3066) No null is allowed for both key and values

3067) The performance is high 3068) Comparatively performance is low 3069) Introduced from 1.2 version 3070) It from 1.1 version

3071)

3072) HashMap 3073) LinkedHashMap

3074) HashMap is parent class of LinkedHashmap

3075) It is a child class of HashMap

3076) Does not maintain the insertion order

3077) Maintains the insertion order

3080)

3081) TreeMap

 The insertion order in not maintained, all elements are inserted in some sorting order  If we want to sort our objects than need to make it has comparable type

 No duplicate key but values can be duplicated.

 We can also sort based on the value only with Comparable 3082)

3083) Note – refer class notes for examples 3084)

In document Core_java (Page 90-103)

Related documents