You are on page 1of 18

Programming in Java

Collections

2011 BlueSignet LLC. All rights reserved.

Java Collections Framework

Java has a vast collection of classes and interfaces that provide collection data structures Collections are objects that contains multiple items in a single instance

You can think of them like an array, but they are fundamentally different That is an example of a collection!

Remember the LinkedList class we wrote in C++?

2011 BlueSignet LLC. All rights reserved.

Java Collections Framework

How is a collection different from an array?

Collections contains many sophisticated methods surrounding them that allow seamless interaction with the data being stored
Data can easily be added, changed, removed, extracted, condensed, etc

Designed specifically to encapsulate data for interfacing with APIs

2011 BlueSignet LLC. All rights reserved.

Java Collections Framework

Collections are typically categorized as such

Collection - Encapsulated group of objects (elements)


Set

Collection of elements of a specific type that cannot duplicate Common example is a deck of cards A ordered or unordered sequence of elements that may duplicate

List

2011 BlueSignet LLC. All rights reserved.

List Class

2011 BlueSignet LLC. All rights reserved.

http://download.oracle.com/javase/1.4.2/docs/api/java/util/List.html

List Class

2011 BlueSignet LLC. All rights reserved.

http://download.oracle.com/javase/1.4.2/docs/api/java/util/List.html

ArrayList Class

2011 BlueSignet LLC. All rights reserved.

http://download.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

ArrayList Class

2011 BlueSignet LLC. All rights reserved.

http://download.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

ArrayList Class

2011 BlueSignet LLC. All rights reserved.

http://download.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

ListTest.java
import java.util.*; # of Items: public class ListTest Hey { Buddy! public static void main(String[] args) { List<String> myList = new ArrayList<String>(); myList.add("Hey"); myList.add("Buddy!"); System.out.println("# of Items:\t" + myList.size()); for(int i = 0; i < myList.size(); i++) System.out.println(myList.get(i)); } }
2011 BlueSignet LLC. All rights reserved.

ListTest.java import java.util.*; public class ListTest { public static void main(String[] args) { List<String> myList = new ArrayList<String>(); myList.add("Hey"); myList.add("Buddy!"); for(String item : myList) System.out.println(item); } }
2011 BlueSignet LLC. All rights reserved.

Hey Buddy!

Set Class

2011 BlueSignet LLC. All rights reserved.

http://download.oracle.com/javase/1,5.0/docs/api/java/util/Set.html

HashSet Class

2011 BlueSignet LLC. All rights reserved.

http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashSet.html

HashSet Class

2011 BlueSignet LLC. All rights reserved.

http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashSet.html

HashSet Class

2011 BlueSignet LLC. All rights reserved.

http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashSet.html

ListTest.java
import java.util.*; public class SetTest { public static void main(String[] args) { Set<Integer> mySet = new HashSet<Integer>(); for(int i = 0; i < 10; i++) 0 mySet.add(i); 1 mySet.add(0); 2 mySet.add(0); 3 for(Integer item : mySet) 4 System.out.println(item); 5 6 } 7 }
8 9
2011 BlueSignet LLC. All rights reserved.

ListTest.java
import java.util.*; public class SetTest { public static void main(String[] args) { Set<Integer> mySet = new HashSet<Integer>(); for(int i = 0; i < 10; i++) mySet.add(i); mySet.add(0); mySet.add(0); Iterator it = mySet.iterator(); while(it.hasNext()) { Integer i = (Integer)it.next(); System.out.println("Current value is: " + i); } } }
2011 BlueSignet LLC. All rights reserved.

Current Current Current Current Current Current Current Current Current Current

value value value value value value value value value value

is: is: is: is: is: is: is: is: is: is:

0 1 2 3 4 5 6 7 8 9

The End?
Thank You For Watching!

2011 BlueSignet LLC. All rights reserved.

You might also like