+ All Categories
Home > Education > Presentation1

Presentation1

Date post: 31-Oct-2014
Category:
Upload: anand-grewal
View: 544 times
Download: 0 times
Share this document with a friend
Description:
 
Popular Tags:
23
The Collection Framework
Transcript
Page 1: Presentation1

The Collection Framework

Page 2: Presentation1

Collections Framework

A collection is a group of objects.The Collection Framework Standardizes the

way in which group of objects are handled by your program.

What c++ calls a Container,Java calls a Collection

Page 3: Presentation1

The Collection interfaces

Collection- enable you to work with groups of objects.

List- extends collection to handle sequences.

Set- extends collection to handle sets, which must contain unique elements.

Page 4: Presentation1

The Collection Classes

ArrayList – implements a dynamic array.LinkedList – implements a linked list.HashSet – uses hash table.linkedHashSet – allow insertion-order

iterations.TreeSet – implements a set stored in a tree.

Page 5: Presentation1

ArrayList class

ArrayList class implements the List interface.ArrayList supports dynamic arrays that can grow as needed.It is a Variable length array of object references.

ArrayLists are created with an initial size. When this size is enlarged, the collection is automatically enlarged. And when the objects are removed, the array may be shrunk.

Page 6: Presentation1

Using constructor

ArrayList()ArrayList(Collection c)ArrayList(int capacity)

Page 7: Presentation1

Methods and Examples

Create an array list ArrayList a1 = new ArrayList();

Add elements to the array list a1.add(“c”); a1.add(1,“A2”);

Page 8: Presentation1

Display the array list System.out.println(a1);

Remove elements from the array list a1.remove(“F”); a1.remove(2);

For finding the size of arrayLista1.size();

Page 9: Presentation1

Obtaining an Array from an ArrayList

Add elements to the array list a1.add(new Integer(1));

Get array object ia[ ] = a1.toArray();

Page 10: Presentation1

LinkedList Class

LinkedList class extends AbstractSequentialList and implements the List interface.

It provides a linked list data structure.

Page 11: Presentation1

Constructor

LinkedList()

LinkedList(collection c)

Page 12: Presentation1

Methods

To add elements at the first /last position.addFirst();

addLast();

To retrieve the elements at first /last position. getFirst(); getLast();

To remove the elements at first /last position. removeFirst(); removeLast();

Page 13: Presentation1

TreeSet class

TreeSet provides an implementations of the Set interface that uses a tree for storage.

Objects are stored in sorted order i.e ascending order.

Access and retrieval times are quite fast.

Page 14: Presentation1

Constructors

TreeSet()TreeSet(collection c)TreeSet(comparator comp)TreeSet(SortedSet ss)

Page 15: Presentation1

Example

Import java.util.*;Class TreeSetDemo {Public static void main(String args[]){TreeSet ts=new TreeSet();ts.add(“C”);ts.add(“A”);ts.add(“B”);System.out.println(ts);}}

Output- [A,B,C]

Page 16: Presentation1

HashSet class

HashSet extends AbstractSet and implements the Set interface.

It creates a collection that uses a hash table for storage. Basic operations:

add()

contains()

remove()

size()

Page 17: Presentation1

Using constructor

HashSet()HashSet(collection c)HashSet(int capacity)HashSet(int capacity, float fillRatio)

Page 18: Presentation1

Methods

boolean add(Object o) void clear() Object clone() boolean contains(Object o)boolean isEmpty() Iterator iterator() boolean remove(Object o) int size()

Page 19: Presentation1

Example

import java.util.*; class hashset { public static void main(String args[]) { HashSet hs = new HashSet(); hs.add("B");hs.add("A"); hs.add("D"); hs.add("E"); hs.add("C");hs.add("F"); System.out.println(hs);hs.remove("C");System.out.println("elements after removing C"+hs); } }

Page 20: Presentation1

LinkedHashSet class

LinkedHashSet extends HashSet, but adds no members of its own.

LinkedHashSet maintains a linked list of the entries in the set.

Page 21: Presentation1

Using constructor

LinkedHashSet( )LinkedHashSet(Collection c) LinkedHashSet(int capacity)LinkedHashSet(int capacity, float fillRatio)

Page 22: Presentation1

  Remove all elements from LinkedHashSet

import java.util.LinkedHashSet; public class linkedhashset { public static void main(String[] args) { LinkedHashSet lhashSet = new LinkedHashSet(); lhashSet.add(new Integer("1")); lhashSet.add(new Integer("2")); lhashSet.add(new Integer("3")); System.out.println("LinkedHashSet before removal : " + lhashSet); lhashSet.clear(); System.out.println("LinkedHashSet after removal : " + lhashSet); System.out.println("Is LinkedHashSet empty ? " + lhashSet.isEmpty());

}}

Page 23: Presentation1

House Is Open For Queries


Recommended