1 / 44

Java Collection Framework

Java Collection Framework. Introduction. Java Collection Framework Collection/Container Object/Collection Object We will be creating objects of appropriate class and using their in built functions. TestArrayList.java[DemoProgram] Package is java.util

Download Presentation

Java Collection Framework

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Collection Framework

  2. Introduction • Java Collection Framework • Collection/Container Object/Collection Object • We will be creating objects of appropriate class and using their in built functions. • TestArrayList.java[DemoProgram] • Package is java.util • Main Types of collections are set, lists and maps.

  3. Collections • Collections in java is a framework that provides an architecture to store and manipulate the group of objects. • All the operations that you perform on a data such as searching, sorting, insertion, manipulation, deletion etc. can be performed by Java Collections. • Java Collection simply means a single unit of objects. • Java Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSetetc).

  4. Hierarchy of Collection framework

  5. Methods of Collection interface

  6. Sets • The AbstractCollectionclass is a convenience class that provides partial implementation for the Collection interface. It implements all the methods in Collection except the size and iterator methods. • The AbstractSetclass is a convenience class that extends AbstractCollectionand implements Set. • The AbstractSetclass provides concrete implementations for the equalsmethod and the hashCodemethod. • Since the size method and iterator method are not implemented in the AbstractSetclass, AbstractSetis an abstract class.

  7. TreeSet • TreeSet provides an implementation of the Set interface that uses a tree for storage. Objects are stored in sorted, ascending order. • Access and retrieval times are quite fast, which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly. • The TreeSet class supports four constructors.

  8. Example

  9. Example

  10. TreeSet Example: Book • Let's see a TreeSet example where we are adding books to set and printing all the books. • The elements in TreeSet must be of Comparable type. • String and Wrapper classes are Comparable by default. • To add user-defined objects in TreeSet, you need to implement Comparable interface.

  11. Comparator • TreeSet(and some other classes) store elements in sorted order. However, it is the comparator that defines precisely what “sorted order” means. • By default, these classes store their elements by using what Java refers to as “natural ordering,” which is usually the ordering that you would expect (A before B, 1 before 2, and so forth). • If you want to order elements a different way, then specify a Comparator when you construct the set or map. Doing so gives you the ability to govern precisely how elements are stored within sorted collections and maps.

  12. Comparator Interface • Java Comparator interface is used to order the objects of user-defined class. • This interface is found in java.utilpackage. • It provides multiple sorting sequence i.e. you can sort the elements on the basis of any data member, for example rollno, name, age or anything else. • We can define a comparator to compare these elements. To do so, create a class that implements the java.util.Comparator interface. • The Comparator interface has two methods, compare and equals. • public int compare(Object element1, Object element2)  returns a negative value if element1 < element2, a positive value if element1 > element2, and zero if they are equal. • public boolean equals(Object element)  returns true if the specified object is also a comparator and imposes the same ordering as this comparator. Note: The equals method is also defined in the Object class. Therefore, you will not get a compile error even if you don’t implement the equals method in your custom comparator class.

  13. Example (sorting elements of TreeSet in reverse order)

  14. Example (user-defined objects in TreeSet)

  15. Example (sorting ArrayList)

  16. Lists • To allow duplicate elements to be stored in a collection, we need to use a list. • A list can not only store duplicate elements but also allow the user to specify where they are stored. The user can access elements by an index. • The List interface extends Collection to define an ordered collection with duplicates allowed. • The List interface adds position-oriented operations, as well as a new list iterator that enables the user to traverse the list bidirectionally.

  17. List The List interface extends Collection to define an ordered collection with duplicates allowed.

  18. ArraysLists

  19. Example

  20. The ListIterator interface extends the Iterator interface to add bidirectional traversal of the list.

  21. Example

  22. LinkedLists • LinkedListis a linked list implementation of the List interface. • In addition to implementing the List interface, this class provides the methods for retrieving, inserting, and removing elements from both ends of the list. • A LinkedListcan be constructed using its no-arg constructor or LinkedList(Collection).

  23. Example

  24. Deque • Usually pronounced as deck, a deque is a double-ended-queue. • Deque interface is a linear collection of elements that supports the insertion and removal of elements at both end points. • The Deque interface is a richer abstract data type than both Stack and Queue because it implements both stacks and queues at the same time. • The Deque interface, defines methods to access the elements at both ends of the Deque instance. Methods are provided to insert, remove, and examine the elements. • Predefined classes like ArrayDeque and LinkedList implement the Deque interface.

  25. Deque using LinkedList • The LinkedList class implements the Deque interface, which extends the Queue interface. So we can use LinkedList to create a Queue.

  26. This method is preferred for queues Throws exception is queue is empty. Throws exception is queue is empty.

  27. LinkedList methods and respective Queue methods

  28. Example

  29. Map • Optimizes Searching. • A map is a container that stores the elements along with the keys. • The keys are like indexes. • In Map, the keys can be any objects. A map cannot contain duplicate keys. Each key maps to one value. A key and its corresponding value form an entry, which is actually stored in a map.

  30. HashMap The HashMap class is efficient for locating a value, inserting an entry, and deleting an entry.

  31. Example As shown in the output, the entries in the HashMap are in random order.

  32. Example

More Related