Chapter One-Javacollections
Chapter One-Javacollections
1
Chapter Topics
04/23/2025 2 2
Introduction to the Java collections Framework
The Java Collections Framework is a library of
classes and interfaces for working with collections
of objects.
04/23/2025 3 3
The Main Types of Collections
•Lists
•Sets
•Map
s
04/23/2025 4 4
Lists
04/23/2025 5 5
Sets
6
Map
s
04/23/2025 7 7
04/23/2025 8 8
The Collection
Interface
04/23/2025 9 9
Some Methods in the Collection Interface
Method Description
add(o : E) : boolean Adds an object o to the Collection. The method
returns true if o is successfully added to the
collection, false otherwise.
clear() : void Removes all elements from the collection.
04/23/2025 1 11
1
Iterators
04/23/2025 1 12
2
The Iterator Interface
04/23/2025 1 13
3
The List
Interface
1
4
List Interface
Methods
04/23/2025 1 15
5
The List Interface
Methods
add(index:int,el:E) Adds the element el to the collection at the given
: void index. Throws IndexOutOfBoundsException if
index is negative, or greater than the size of the
list.
get(index:int):E Returns the element at the given index, or throws
IndexOutBoundsException if index is negative or
greater than or equal to the size of the list.
indexOf(o:Object):int Returns the least (first) index at which the
object o is found; returns -1 if o is not in the
list.
lastIndexOf(o:Object):i Returns the greatest (last) index at which the
nt object o is found; returns -1 if o is not in the list.
listIterator():ListItera Returns an iterator specialized to work
tor< E> with List collections.
remove(index:int):E Removes and returns the element at the given
index; throws IndexOutOfBoundsException if
index is negative, or greater than or equal to the
size of the list.
04/23/2025
set(index:int, el:E):E Replaces the element at index with the new 16 16
Other List Interface Method
o Add(index/value);
o addAll(collection c);
o Remove(object/index);
o RemoveAll(collection c);
o Get(index/value);
o Set(index,new value)
o Contains(object)
o isEmpty();
o Size();
04/23/2025 17
ArrayList and Vector
04/23/2025 1 18
8
AbstractSequentialList and LinkedList
04/23/2025 1 19
9
Example:
ArrayList
import java.util.*;
public class Test
{
public static void main(String [ ] args)
{
List<String> nameList = new ArrayList<String> ();
String [ ] names = {"Ann", "Bob", "Carol"};
// Add to arrayList
for (int k = 0; k < names.length; k++)
nameList.add(names[k]);
04/23/2025 2 22
2
Example:
LinkedList
import java.util.*;
public class Test
{
public static void main(String [ ] args)
{
List<String> nameList = new LinkedList<String> ();
String [ ] names = {"Ann", "Bob", "Carol"};
// Add to arrayList
for (int k = 0; k < names.length; k++)
nameList.add(names[k]);
04/23/2025 2 25
5
Example Use of Iterator
public static void main(String [ ] args)
{
List<String> nameList = new ArrayList<String>();
String [ ] names = {"Ann", "Bob", "Carol"};
04/23/2025 26
Class work 3
Get the out put of the code below?
public static void main(String[] args) {
System.out.println("well come to second java project");
LinkedList ll=new LinkedList();//declaring linked list
Integer [] a={1,2,4,5,6,7};
//adding element to linked list using for loop
for(int i=0;i<a.length;i++)
ll.add(a[i]);
//print elements of linked list
for(int j=0;j<ll.size();j++)
System.out.println("iteration \t\t"+(j+1)+"\t"+ll.get(j));
04/23/2025 27
Sets
04/23/2025 28
The Set Part of the JCF
Hierarchy
AbstractCollecti
on
AbstractS
et
HashSe TreeSe
t t
LinkedHashS
et
04/23/2025 29
The Set Part of the
JCF
04/23/2025 30
HashSet
04/23/2025 31
Examples of Hashing
Functions
•For Integer objects, you can use the integer value of
the object (or its absolute value).
04/23/2025 32
HashSet Capacity and Load Factor
04/23/2025 33
Some HashSet
Constructors
04/23/2025 34
HashSet Example 1
// Create a HashSet to hold String objects.
Set<String> fruitSet = new HashSet<String>();
04/23/2025 36
get the output
HashSet set=new HashSet();
Integer[] arr= {1,2,3,4,5,6};
for(int i=0;i<arr.length;i++)
set.add(arr[i]);
04/23/2025 38
HashSet Example 2
// Display the elements in the set.
System.out.println("Here are the names in the set.");
while (it.hasNext())
System.out.println(it.next());
System.out.println();
// Search for "Katherine". We should find this
// name in the set.
if (nameSet.contains("Katherine"))
System.out.println("Katherine is in the set.");
else
System.out.println("Katherine is NOT in the
set.");
// Search for "Bethany". We should not find
// this name in the set.
if (nameSet.contains("Bethany"))
System.out.println("Bethany is in the set.");
else
} System.out.println("Bethany is NOT in the
} set.");
04/23/2025 39
LinkedHashSet
04/23/2025 41
Sorting Strings Using a TreeSet
import java.util.*;
public class Test
{
public static
void
main(String [ ]
args)
{
// Create
TreeSet
Set<String>
mySet = new
TreeSet<Strin
g>();
// Add Strings
mySet.add("Alan");
mySet.add("Carol")
;
mySet.add("Bob");
// Get Iterator
Iterator it = mySet.iterator();
04/23/2025 42
Using a TreeSet to Sort Strings in
Reverse Alphabetic Order
public class Test
{
public static void main(String [ ] args)
{ // Create Comparator
RevStrComparator comp = new RevStrComparator();
Set<String> mySet = new TreeSet<String>(comp);
// Add strings
mySet.add("Alan");
mySet.add("Carol")
;
mySet.add("Bob");
// Get Iterator
Iterator it = mySet.iterator();
while (it.hasNext())
{
System.out.println(it.next(
));
}
}
}
04/23/2025 43
TreeSet Example 1
public static void main(String[] args)
{
// Create a TreeSet and store some values in it.
SortedSet<String> mySet = new TreeSet<String>();
mySet.add("Pear");
mySet.add("Apple");
mySet.add("Strawberry"); mySet.add("Banana");
System.out.println("Here are the TreeSet elements " +
"in ascending order:");
for (String str : mySet)
System.out.println(str);
The map stores the mappings based on the key part of the
mapping, in a way similar to how a Set collection stores its
elements.
04/23/2025 45
The Map Part of the JCF
Hierarchy
Ma
p
AbstractMa
p
HashMa TreeMa
p p
LinkedHashMa
p
04/23/2025 46
The Map
Interface
04/23/2025 47
Some Methods of the
Map Interface
clear() : void Removes all elements from the map.
keySet() : Set<K> Returns the set of all keys stored in the map.
04/23/2025 48
Some Methods of the Map
Interface
04/23/2025 49
Concrete Map
Classes
Maps store keys with attached values. The keys are
stored as sets.
•HashMap stores keys according to their hash codes, just like
HashSet stores its elements.
•LinkedHashMap is a HashMap that can iterate over the keys
in insertion order (order in which mappings were inserted)
or in access order (order of last access).
•TreeMap stores mappings according to the natural order of
the keys, or according to an order specified by a
Comparator.
04/23/2025 50
Get the output
Hashtable<Integer,String> ht=new
Hashtable<Integer,String>();
ht.put(1,"kebede");
ht.put(2, "abebe");
ht.put(3,"ayele");
System.out.print(ht);
System.out.println(ht.keySet());
System.out.println(ht.values());
ht.remove(2);
System.out.println(ht);
04/23/2025 51
n e
r o
p te A
h a
c ??Q &
o f ?
d ?
En ? ? ? ?
?
04/23/2025 52