Collections
Chapter 12
12
Java Collection Frameworks
The Java collection framework is a set
of utility classes and interfaces.
Designed for worki...
12
Types of Collections
Simple Collections
Sets have no duplicate elements.
Lists are ordered collections that can have
du...
12
The Collection Interface
Root interface of
collection classes
Common methods
defined in Collection:
add()
contains()
is...
12
The ArrayList
ArrayList is a concrete
implementation of the List interface.
A concrete implementation is a class that
c...
12
ArrayList vs. an Array
An ArrayList has several advantages:
Can be resized dynamically as more
objects are added
Elemen...
12
Controlling ArrayList size
Important methods:
size() returns the number of elements
ensureCapacity() sets the capacity ...
12
Using Collections
Although objects in a collection do not
have to be of the same type:
It is not good practice to mix o...
12
Adding Elements to a Collection
add() method is overloaded
myList.add(myObject) adds myObject
to end of collection
myLi...
12
Replacing and Removing Elements
To replace and remove elements in an
ArrayList:
set() method replaces an element at a
s...
Upcoming SlideShare
Loading in …5
×

Chapter 12

247 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
247
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter 12

  1. 1. Collections Chapter 12
  2. 2. 12 Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with collections of objects A collection is a group of objects, each of which is known as an element in the collection.
  3. 3. 12 Types of Collections Simple Collections Sets have no duplicate elements. Lists are ordered collections that can have duplicate elements. Maps Map uses key/value pairs to associate an object (the value) with a key. Both sets and maps can be sorted or unsorted.
  4. 4. 12 The Collection Interface Root interface of collection classes Common methods defined in Collection: add() contains() isEmpty() iterator() remove() size()
  5. 5. 12 The ArrayList ArrayList is a concrete implementation of the List interface. A concrete implementation is a class that can be instantiated.
  6. 6. 12 ArrayList vs. an Array An ArrayList has several advantages: Can be resized dynamically as more objects are added Elements can be removed from an ArrayList Simpler to use than an array
  7. 7. 12 Controlling ArrayList size Important methods: size() returns the number of elements ensureCapacity() sets the capacity of the ArrayList to a certain number of elements trimToSize() trims an ArrayList to only the number of elements it currently holds to reduce memory requirements
  8. 8. 12 Using Collections Although objects in a collection do not have to be of the same type: It is not good practice to mix object types in a collection. You must check object type and cast to appropriate class if heterogeneous collections are used.
  9. 9. 12 Adding Elements to a Collection add() method is overloaded myList.add(myObject) adds myObject to end of collection myList.add(3, myObject) inserts myObject at offset 3 in a collection
  10. 10. 12 Replacing and Removing Elements To replace and remove elements in an ArrayList: set() method replaces an element at a specified position remove() method removes an element at a specified position

×