0% found this document useful (0 votes)
16 views

Notes

The document discusses Java collection framework and differences between arrays and collections. It provides examples of using ArrayList in a type-safe way by specifying the generic type and using for-each loop for traversal. Additional collection utilities like sort, reverse and shuffle are also demonstrated.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Notes

The document discusses Java collection framework and differences between arrays and collections. It provides examples of using ArrayList in a type-safe way by specifying the generic type and using for-each loop for traversal. Additional collection utilities like sort, reverse and shuffle are also demonstrated.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Lecture 21-Nov-2020 Saturday

Topics To Cover:
Core Java:
1. Collection Framework
2. Swing Part:
=>Checking Progress of Student
=>Problem/Error Correction
-----------------------------------------------------------------------------------
-----------------------
Core Java:
1. Collection Framework
OCJP1. What are the three different meanings of Collection?
Ans:
a) In English - Collection represent 'Group of Items'
b) java.util.Collection - It is an "interface" [OCJP2], act as parent of all other
collection types
c) java.util.Collections - It is a "class" [OCJP3], it provides some useful 'static
fn'
to perform operation on Collection like
sort, shuffle, binarySearch, reverse,etc.

OCJP4. Difference between array and Collection?


Ans:
In case of array, we need to
a) Specify max size during design time or at runtime. Where as no size is required
for Collection - It can grow dynamically.
b) Can only be accessed using numerical index that starts with 0 upto (size-1) -
Where as Collection types can also be accessed using String Index known as key
[As we did with Properties p; p.getString("username"); ]

c) To insert/delete item between two elements we require shifting of adjacent items


known as compaction process. Which takes time if array is to large. Where as item
can be inserted directly in collection at any position without need to shift
adjacent elements.

d) array cannot grow or shrink dynamically where as collections can.

Example of Compaction:
Before Insert 20 After
See Fig1-21-Nov-2020

Program: Obsolete/Old Way - Supported by <= Jdk1.4 [Type Unsafe Approach]


import java.util.*; //Referencing to all class/interface of util package
public class CollectionExample1{
public static void main(String args[ ]){
ArrayList a=new ArrayList(); //It is a type of Collection -Step I
//Add items to Array List -Step II
a.add("Amandeep"); a.add("Varnita"); a.add("Prabhat");
a.add(10); //Unsafe - All others are String but it is int value
//Traversal -Step III
Iterator i=a.iterator(); //OCJP5 Iterator=>interface iterator()=>Fn
while(i.hasNext()){ //OCJP6. hasNext gives true if item found
System.out.println(i.next()); //OCJP7. next() is used to access/retrieve value
}
} //main
}//class
Program II: Same program but using 'Type Safe' introduced in Jdk1.5 [OCJP8] using
concept of 'Generic Class'.
import java.util.*;
public class TypeSafeCollection{
public static void main(String args[ ]){
//Step I: Create an object of ArrayList with value of Type Parameter T
ArrayList<String>a=new ArrayList<>(); //<> Diamond Operator

//Add items to Array List -Step II


a.add("Amandeep"); a.add("Varnita"); a.add("Prabhat");
a.add(10); //OCJP9. Compile Time Error
//(*1) Leave a line here for additional Logics

//Traversal - Using Advance for or 'for-each' loop [OCJP10]


for(String i: a){ //Syntax for(datatype variable: arrayname){
System.out.println(i); //code
} //}
}
}
Applying some additional Logics in place of (*1) in above Code
a) Collections.sort(a); //Output: Amandeep Prabhat Varnita
b) Collections.reverse(a); //Output: Prabhat Varnita Amandeep
c) Collections.shuffle(a); //Data appears random order, useful for Quiz
//and Lottery/Party based programs

You might also like