COLLECTION IN JAVA PDF

adminComment(0)

OOP: Collections. 1. Collections in Java. • Arrays. ▫ Has special language support. • Iterators. ▫ Iterator (i). • Collections (also called containers). ▫ Collection (i). The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet. The Collections Framework. ○ Java has a variety of collections classes for holding groups of data. ○ The three major ways of organizing data are. ○ Sets.


Collection In Java Pdf

Author:TARAH FAMILIA
Language:English, Indonesian, Japanese
Country:Grenada
Genre:Personal Growth
Pages:358
Published (Last):27.12.2015
ISBN:899-8-41102-236-7
ePub File Size:27.50 MB
PDF File Size:20.62 MB
Distribution:Free* [*Registration Required]
Downloads:50655
Uploaded by: OZIE

kaz-news.info*. ▫ Java 5 released! ◇ Lots of changes about collections Collection. ▫ Group of elements (references to objects). ▫ It is not specified whether they are. collection classes that the standard Java library supplies. In this chapter, you figure 1 interfaces and Classes in the Java Collections Framework. ‹‹interface››. JAVA COLLECTIONS FRAMEWORK. Prior to Java 2, Java provided ad hoc classes such as Dictionary, Vector, Stack, and Properties to store and manipulate .

Collections allow adding an element, deleting an element and host of other operations. There are a number of Collections in Java allowing to choose the right Collection for the right context. What are the important methods that are declared in the Collection Interface? Most important methods declared in the collection interface are the methods to add and remove an element.

Other important methods defined as part of collection interface are shown below. List interface extends Collection interface. So, it contains all methods defined in the Collection interface. In addition, List interface allows operation specifying the position of the element in the Collection.

Most important thing to remember about a List interface - any implementation of the List interface would maintain the insertion order. When an element A is inserted into a List without specifying position and then another element B is inserted, A is stored before B in the List. When a new element is inserted without specifying a position, it is inserted at the end of the list of elements.

Java Collection Interview Questions – Edition

However, We can also use the void add int position, E paramE ; method to insert an element at a specific position. First and foremost, Map interface does not extend Collection interface.

So, it does not inherit any of the methods from the Collection interface. A Map interface supports Collections that use a key value pair. A key-value pair is a set of linked data items: a key, which is a unique identifier for some item of data, and the value, which is either the data or a pointer to the data.

Key-value pairs are used in lookup tables, hash tables and configuration files. A key value pair in a Map interface is called an Entry. Put method allows to add a key, value pair to the Map.

SortedSet Interface extends the Set Interface. Both Set and SortedSet do not allow duplicate elements. Main difference between Set and SortedSet is - an implementation of SortedSet interface maintains its elements in a sorted order. Set interface does not guarantee any Order.

Throws a NoSuchElementException if the beginning of the list has been reached. E getFirst returns the element at the beginning or the end of the list. E removeFirst removes and returns the element at the beginning or the end of the list. Array Lists In the preceding section, you saw the List interface and the LinkedList class that implements it. The List interface describes an ordered collection in which the position of elements matters. There are two protocols for visiting the elements: through an iterator and by random access with methods get and set.

The latter is not appropriate for linked lists, but of course get and set make a lot of sense for arrays. The collections library supplies the familiar ArrayList class that also implements the List interface. An ArrayList encapsulates a dynamically reallocated array of objects. Why use an ArrayList instead of a Vector?

For one simple reason: All methods of the Vector class are synchronized. It is safe to access a Vector object from two threads. But if you access a vector from only a single thread by far the more common case your code wastes quite a bit of time with synchronization. In contrast, the Array- List methods are not synchronized.

Hash Sets Linked lists and arrays let you specify the order in which you want to arrange the elements. That can be time consuming if the collection contains many elements.

The drawback is that those data structures give you no control over the order in which the elements appear. The data structures organize the elements in an order that is convenient for their own purposes.

A well-known data structure for finding objects quickly is the hash table. A hash table computes an integer, called the hash code, for each object.

A hash code is an integer that is somehow derived from the instance fields of an object, preferably such that objects with different data yield different codes. Table below lists a few examples of hash codes that result from the hashCode method of the String class.

Hash Codes Resulting from the hashCode Function If you define your own classes, you are responsible for implementing your own hashCode method with the equals method: If a.

You might also like: HARNESSING JAVA 7 PDF

In Java, hash tables are implemented as arrays of linked lists. Each list is called a bucket see Figure below. To find the place of an object in the table, compute its hash code and reduce it modulo the total number of buckets. The resulting number is the index of the bucket that holds the element.

Perhaps you are lucky and there is no other element in that bucket. Then, you simply insert the element into that bucket. Of course, it is inevitable that you sometimes hit a bucket that is already filled. This is called a hash A hash table collision. Then, you compare the new object with all objects in that bucket to see if it is already present.

Join our Groups>

Provided that the hash codes are reasonably randomly distributed and the number of buckets is large enough, only a few comparisons should be necessary. If you want more control over the performance of the hash table, you can specify the initial bucket count. The bucket count gives the number of buckets that are used to collect objects with identical hash values. If too many elements are inserted into a hash table, the number of collisions increases and retrieval performance suffers.

Collections in Java

If you know approximately how many elements will eventually be in the table, then you can set the bucket count. Some researchers believe that it is a good idea to make the bucket count a prime number to prevent a clustering of keys.

The standard library uses bucket counts that are a power of 2, with a default of Any value you supply for the table size is automatically rounded to the next power of 2. Of course, you do not always know how many elements you need to store, or your initial guess may be too low. If the hash table gets too full, it needs to be rehashed. To rehash the table, a table with more buckets is created, all elements are inserted into the new table, and the original table is discarded.

The load factor determines when a hash table is rehashed. For example, if the load factor is 0.

Related Post: JAVA site APP

For most applications, it is reasonable to leave the load factor at 0. Hash tables can be used to implement several important data structures. The simplest among them is the set type. A set is a collection of elements without duplicates. The add method of a set first tries to find the object to be added, and adds it only if it is not yet present. The Java collections library supplies a HashSet class that implements a set based on a hash table.

You add elements with the add method. The contains method is redefined to make a fast lookup to find if an element is already present in the set. It checks only the elements in one bucket and not all elements in the collection. The hash set iterator visits all buckets in turn.

Because the hashing scatters the elements around in the table, they are visited in seemingly random order. The program reads all words from the input and adds them to the hash set. It then iterates through the unique words in the set and finally prints out a count. Alice in Wonderland has 5, unique words, including the copyright notice at the beginning. The words appear in random order. If the hash code of an element were to change, then the element would no longer be in the correct position in the data structure.

HashSet int initialCapacity constructs an empty hash set with the specified capacity number of buckets.Adding an element to a tree is slower than adding it to a hash table, but it is still much faster than adding it into the right place in an array or linked list.

You can refer the below example for help. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.

You can have two different rectangles with different coordinates but the same area. Thus, adding elements into a TreeSet is somewhat slower than adding into a HashSet. The Collection interface extends the Iterable interface and therefore all the subclasses of Collection interface also implement the Iterable interface. Set Interface in Java is present in java.

Like HashSet, It also contains unique elements.