Core Java

Debasish Pratihari

Collection:


A collection is a generic term for any object that
represents a set of objec...
Core Java

Debasish Pratihari

Types of Collection:


The way a collection (group of objects) is
implemented in memory, a...
Core Java

Debasish Pratihari

Collection Interface:
Methods
public
public
public
public
public
public
public
public
publi...
Core Java

Debasish Pratihari

List Interface

Methods







public
public
public
public
public
public

ListIterato...
Core Java

Debasish Pratihari

Using List :


Supports basic operations of Collection
Interface.



Facilitate positiona...
Core Java

Debasish Pratihari

Using Map :


Elements are stored as key-value pair



A map can’t contain duplicate keys...
Upcoming SlideShare
Loading in...5
×

Lecture 24

280

Published on

Notes on core java

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
280
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lecture 24

  1. 1. Core Java Debasish Pratihari Collection:  A collection is a generic term for any object that represents a set of objects grouped together by some means or mechanism inside the memory.  Collection Framework is a specification by which we can store and manage objects of heterogeneous kinds in memory.   Collection Classes are also called Container classes We can define collection as a java data structure capable of storing & managing different kinds of objects.  Note : A collection has a capacity to grow dynamically. Collection Framework Provides:  Interface  Implementation  Algorithms Core Interfaces : Collection Set List Map SortedMap Queue SortedSet Lecture/core/collection/24 Page #1 feel the Technology…
  2. 2. Core Java Debasish Pratihari Types of Collection:  The way a collection (group of objects) is implemented in memory, accordingly it is categorized into following categories.  Set  List  Map Traversing Collections:  The Followings are the ways to traverse a collection:     For-each loop Iterator ListItetor Using Methods taking index as Parameter Collection Interfaces and Classes : Interface Implementation HashSet Set TreeSet ArrayList List LinkedList HashMap Map TreeMap Lecture/core/collection/24 Historical Vector Stack Hashtable Properties Page #2 feel the Technology…
  3. 3. Core Java Debasish Pratihari Collection Interface: Methods public public public public public public public public public          Iterator iterator () boolean add (Object obj) boolean addAll (Collection other) boolean remove (Object obj) void clear( ) boolean isEmpty ( ) boolean contains (Object obj) boolean containsAll (collection other) int size ( ) Iterator interface :  Used for traverse a Collection in forward direction. Methods     public public public public boolean hasNext( ) Object next( ) Object next( ) Object remove ( ) ListIterator interface :  ListIterator interface in extended from Iterator interface.  In contrast to Iterator interface ListIterator interface is used to traverse a collection in forward or backward. Method     public public public public Object next( )  public boolean hasPrevious( ) boolean hasNext( )  public Object remove( ) Object remove( )  public boolean add(Object obj) Object previous( )  public boolean set(Object obj) Lecture/core/collection/24 Page #3 feel the Technology…
  4. 4. Core Java Debasish Pratihari List Interface Methods       public public public public public public ListIterator listIterator( ) void add(int index, Object obj) void addAll(int i, Collection list) Object remove(int indx) Object set(int indx, Object obj) int indexOf (Object obj) Using Set : 25%  Support basic operation of Collection Interface  Can’t contain duplicate elements.  Implementations    HasSet TreeSet LinkedHashSet Example : import java.util.*; class SetDemo{ public static void main(String args[]){ HashSet <String>set= new HashSet<String>(); set.add("one"); set.add("two"); set.add("three"); set.add("four"); set.add("five"); System.out.println(set); } } Lecture/core/collection/24 Page #4 feel the Technology…
  5. 5. Core Java Debasish Pratihari Using List :  Supports basic operations of Collection Interface.  Facilitate positional access and Searching.  Supports range operation.  Implementations:   ArrayList LinkedList Example: import java.util.*; class ListDemo{ public static void main(String args[]){ LinkedList<Integer> list= new LinkedList<Integer>(); list.add(10); list.add(20); list.add(30); System.out.println(list); ListIterator itr= list.listIterator(); while(itr.hasNext()) System.out.println(itr.next()); while(itr.hasPrevious()) System.out.println(itr.previous()); } } Lecture/core/collection/24 Page #5 feel the Technology…
  6. 6. Core Java Debasish Pratihari Using Map :  Elements are stored as key-value pair  A map can’t contain duplicate keys  Accept null as key value.  Implementations:    HashMap TreeMap LinkedHashMap Example : import java.util.*; class MapDemo{ public static void main(String args[]){ HashMap<String,String> map = new HashMap<String,String>(); map.put("one","java"); map.put("two","j2ee"); map.put("three","struts"); map.put("four","hibernate"); map.put("","EJB"); System.out.println(map); } } HashTable vs HashMap : HashTable  Synchronized  Doesn’t accept null  Order is maintained Lecture/core/collection/24 vs HashMap    Un-synchronized Accept null No guarantee of order Page #6 feel the Technology…

×