The Collection Classes Java Help

The Collection Classes 

Now that you are familiar with the collection interfaces, you arc ready to examine the standard classes that implement them. Some of the classes provide full implementations that can be used as-is. Others are abstract, providing skeletal irn plcmc atlons that I Used (‘IS starting points for creating concrete collections. None . of the collection classes arc synchronized, but as you will see later in this chapter, it is possible to obtain synchronized versions, The standard collection classes are summarized in the following table The following sections examine the concrete collection classes and illustrate their use .

The ArrayList Class

The ArrayList class extends AbstractList and implements the List interface. Arraylist supports dynamic arrays that can grow as needed. In Java, standard arrays arc of a fixed length. After arrays arc created, they cannot grow or shrink, which means  you must know in advance how many clements an array will hold. But, sometimes, you may not know until run time precisely how large of an array you need. To handle this situation, the collections framework defines Arraylist essence.xm ArrayList is a variable-length array of object references. That is, an Arraylist can dynamically increase or decrease in size. Array lists arc created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk. Arraylist has the constructors shown here:

Arraylist( )

Array List(Collection 1 c)
ArrayList(int capacity)
The first constructor builds an empty an.l), list. The second constructor  array list that is initialized with the dements of the collection l’. third constructor builds an array list that has till’ specified initial (policy. The capacity ilhe SIDe the underlying array that is used to store the clements. The capacity grows automatically as elements are added to an. array list. The following program shows a simple use of Arraylist. An array list is created, and then objects of type String arc added to it. (Recall that a quoted string is translated into a String object.) TI1 list is then displayed. Some of the elements are removed and the list is displayed again. The output from this program is shown here: Initial size of al: 0 I

Size of aI, after additions: 7
‘Contents of al: (C, A2, A, E, 13, D, FJ
Size of al af~er deletions: 5
Contents of hI: [C, A2, E. IT, DJ

Notice that a1 starts out empty and grows as clements are added to it. When elements are removed, its ‘size is reduced. . In the preceding example, the contents of a collection are displayed using the default conversion provided by toString( ), which was inherited from AbstractCoLlection. Although it is sufficient for short, sample programs, you seldom use this method to display the contents of a real-world collection. Usually, you provide your own output routines. But, for the next few examples, the default output created by toString( ) will continue to be used. Although the capacity of an ArrayList object increases automatically as objects arc stored in it, you can increase: the capacity of an ArrayList object manually by calling ensure Capacity By. You might want to do this if yeu know in advance that you will be storing many more items in the collection that it can currently hold. By increasing its capacity once, at the start, you can prevent several reallocations later. Because reallocations are costly in terms of time, preventing unnecessary ones improves performance. The signature for eusuref’apacltyt ) is shown here: void ensurcf.apacityunt cap)

Here, cap is the new capacity.
Conversely, if you want to reduce the size of the raLlY that underlies an Arraylist
object so that it is precisely as large as the number of items that it is currently holding,
call trim To Size( ), shown here
void trim To Size( )

Obtaining an Array from an Array list When working with Array lisl. you will sometimes want to obtain an actual are contains the contents of tho list. As explained earlier, YOU can do this by calling,li toArray] ). , Several reasons exist why you might want to convert a collection into an array Such as To obtain faster processing for certain operations, To pi1SS an array to a method that L nul overloaded to accept a  To autocratic you’ll never code code that do not understand collections. The output from the program is shown here Contents of al (I, 2, 3, 41 Sum is: 10 The program begins by creating a collection of integers. As explained, you cannot store primitive types in a collection, so objects of type Integer are created and stored. Next, . to Array( ) is called and it obtains an array of Objects. The contents of this array are cast to Integer, and then the values are summed

  • Feel free to send us an inquiry, we will reply back in hours.


Posted on September 17, 2014 in java util Part 1 The Collections Framework

Share the Story

About the Author

Back to Top