Unit 4
Unit 4
Introduction to JAVA
PCC-CSE-309G
Collections in Java
The Collection in Java is a framework that provides an architecture to store and
manipulate the group of objects.
Java Collections can achieve all the operations that you perform on a data such as
searching, sorting, insertion, manipulation, and deletion.
Java Collection means a single unit of objects. Java Collection framework provides many
interfaces (Set, List, Queue, Deque) and classes (ArrayList,
Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).
The Collection framework represents a unified architecture for storing and manipulating a
group of objects. It has:
There are only three methods in the Iterator interface. They are:
1 public boolean hasNext() It returns true if the iterator has more elements
otherwis
2 public Object next() It returns the element and moves the cursor pointer to t
3 public void remove() It removes the last elements returned by the iterator. It
Iterable Interface
The Iterable interface is the root interface for all the collection classes. The Collection
interface extends the Iterable interface and therefore all the subclasses of Collection
interface also implement the Iterable interface.
It contains only one abstract method. i.e.,
1. Iterator<T> iterator()
Collection Interface
The Collection interface is the interface which is implemented by all the classes in the
collection framework. It declares the methods that every collection will have. In other
words, we can say that the Collection interface builds the foundation on which the
collection framework depends.
Some of the methods of Collection interface are Boolean add ( Object obj), Boolean addAll
( Collection c), void clear(), etc. which are implemented by all the subclasses of Collection
interface.
List Interface
List interface is the child interface of Collection interface. It inhibits a list type data
structure in which we can store the ordered collection of objects. It can have duplicate
values.
List interface is implemented by the classes ArrayList, LinkedList, Vector, and Stack. To
There are various methods in List interface that can be used to insert, delete, and access
the elements from the list.
The classes that implement the List interface are given below.
ArrayList
The ArrayList class implements the List interface. It uses a dynamic array to store the
duplicate element of different data types. The ArrayList class maintains the insertion order
and is non-synchronized. The elements stored in the ArrayList class can be randomly
accessed. Consider the following example.
1.import java.util.*;
2.class TestJavaCollection1{
3. public static void main(String args[]){
4. ArrayList<String> list=new ArrayList<String>();//Creating arraylist
5. list.add("Ravi");//Adding object in arraylist
6. list.add("Vijay");
7. list.add("Ravi");
8. list.add("Ajay");
9. //Traversing list through Iterator
10. Iterator itr=list.iterator();
11.while(itr.hasNext()){
12. System.out.println(itr.next());
13. }
14. }
15. }
Output:
LinkedList
LinkedList implements the Collection interface. It uses a doubly linked list internally to
store the elements. It can store the duplicate elements. It maintains the insertion order
and is not synchronized. In LinkedList, the manipulation is fast because no shifting is
required.
1.import java.util.*;
2.public class TestJavaCollection2{
3.public static void main(String args[]){
4.LinkedList<String> al=new LinkedList<String>();
5.al.add("Ravi");
6.al.add("Vijay");
7.al.add("Ravi");
8.al.add("Ajay");
9.Iterator<String> itr=al.iterator();
10.while(itr.hasNext()){
11.System.out.println(itr.next());
12. }
13. }
14. }
Output:
Ravi
Vijay Ravi Ajay
LinkedList(Collection<? extends It is used to construct a list containing the elements of the spe
E> c) they are returned by the collection's iterator.
void add(int index, E element) It is used to insert the specified element at the specifie
boolean addAll(Collection<? extends E> c) It is used to append all of the elements in the specified
list, in the order that they are returned by the specifie
d
boolean addAll(Collection<? extends E> c) It is used to append all of the elements in the specified
list, in the order that they are returned by the specified
boolean addAll(int index, Collection<? It is used to append all the elements in the specified co
extends E> c) specified position of the list.
int indexOf(Object o) It is used to return the index in a list of the first occurr or
-1 if the list does not contain any element.
int lastIndexOf(Object o) It is used to return the index in a list of the last occurr or
-1 if the list does not contain any element.
E set(int index, E element) It replaces the element at the specified position in a lis
<T> T[] toArray(T[] a) It returns an array containing all the elements in the pr
the last element); the runtime type of the returned arr
array.
1. import java.util.*;
2. public class LinkedList2{
3. public static void main(String args[]){
4. LinkedList<String> ll=new LinkedList<String>();
5. System.out.println("Initial list of elements: "+ll);
6. ll.add("Ravi");
7. ll.add("Vijay");
8. ll.add("Ajay");
9. System.out.println("After invoking add(E e) method: "+ll);
10. //Adding an element at the specific position
11. ll.add(1, "Gaurav");
12. System.out.println("After invoking add(int index, E element) method: "+ll);
13. LinkedList<String> ll2=new LinkedList<String>();
14. ll2.add("Sonoo");
15. ll2.add("Hanumat");
16. //Adding second list elements to the first list
17. ll.addAll(ll2);
18. System.out.println("After invoking addAll(Collection<? extends E> c) method: "+ ll);
LinkedList<String> ll3=new LinkedList<String>(); ll3.add("John");
19.
ll3.add("Rahul");
20.
//Adding second list elements to the first list at specific position ll.addAll(1, ll3);
21.
System.out.println("After invoking addAll(int index, Collection<? extends E> c) method:
22. "+ll);
23.
//Adding an element at the first position
24.
ll.addFirst("Lokesh");
System.out.println("After invoking addFirst(E e) method: "+ll);
25.
//Adding an element at the last position ll.addLast("Harsh");
26.
System.out.println("After invoking addLast(E e) method: "+ll);
27.
28.
29.
30.
31.
32. }
33. }
Initial list of elements: []
After invoking add(E e) method: [Ravi, Vijay, Ajay]
After invoking add(int index, E element) method: [Ravi, Gaurav, Vijay, Ajay]
After invoking addAll(Collection<? extends E> c) method: [Ravi, Gaurav,
Vijay, Ajay, Sonoo, Hanumat]
After invoking addAll(int index, Collection<? extends E> c) method: [Ravi,
John, Rahul, Gaurav, Vijay, Ajay, Sonoo, Hanumat]
After invoking addFirst(E e) method:
[Lokesh, Ravi, John, Rahul, Gaurav, Vijay, Ajay, Sonoo, Hanumat] After
invoking addLast(E e) method:
[Lokesh, Ravi, John, Rahul, Gaurav, Vijay, Ajay, Sonoo, Hanumat, Harsh]
1.import java.util.*;
2.public class LinkedList3 {
3.
4. public static void main(String [] args)
5. {
6. LinkedList<String> ll=new LinkedList<String>(); ll.add("Ravi");
7. ll.add("Vijay");
8. ll.add("Ajay");
9. ll.add("Anuj");
10. ll.add("Gaurav");
11. ll.add("Harsh");
12. ll.add("Virat");
13. ll.add("Gaurav");
14. ll.add("Harsh");
15. ll.add("Amit");
16. System.out.println("Initial list of elements: "+ll);
17. //Removing specific element from arraylist ll.remove("Vijay");
18. System.out.println("After invoking remove(object) method: "+ll);
19. //Removing element on the basis of specific position ll.remove(0);
20. System.out.println("After invoking remove(index) method: "+ll);
21. LinkedList<String> ll2=new LinkedList<String>(); ll2.add("Ravi");
22. ll2.add("Hanumat");
23. // Adding new elements to arraylist
24. ll.addAll(ll2); System.out.println("Updated list : "+ll);
25. //Removing all the new elements from arraylist ll.removeAll(ll2);
26. System.out.println("After invoking removeAll() method: "+ll);
27. //Removing first element from the list
28.
29.
30.
31.
32.
33.
34. ll.removeFirst();
35. System.out.println("After invoking removeFirst() method: "+ll);
36. //Removing first element from the list ll.removeLast();
37. System.out.println("After invoking removeLast() method: "+ll);
38. //Removing first occurrence of element from the list
39. ll.removeFirstOccurrence("Gaurav");
40. System.out.println("After invoking removeFirstOccurrence() method: "+ll);
41. //Removing last occurrence of element from the list
42. ll.removeLastOccurrence("Harsh");
43. System.out.println("After invoking removeLastOccurrence() method: "+ll);
44.
45.
46. //Removing all the elements available in the list ll.clear();
47. System.out.println("After invoking clear() method: "+ll);
48.
49. }
50. }
Initial list of elements: [Ravi, Vijay, Ajay, Anuj, Gaurav, Harsh, Virat,
Gaurav, Harsh, Amit]
After invoking remove(object) method: [Ravi, Ajay, Anuj, Gaurav, Harsh,
Virat, Gaurav, Harsh, Amit]
After invoking remove(index) method: [Ajay, Anuj, Gaurav, Harsh, Virat,
Gaurav, Harsh, Amit]
Updated list : [Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit, Ravi,
Hanumat]
After invoking removeAll() method: [Ajay, Anuj, Gaurav, Harsh, Virat,
Gaurav, Harsh, Amit]
After invoking removeFirst() method: [Gaurav, Harsh, Virat, Gaurav, Harsh,
Amit]
After invoking removeLast() method: [Gaurav, Harsh, Virat, Gaurav, Harsh]
After invoking removeFirstOccurrence() method: [Harsh, Virat, Gaurav, Harsh]
After invoking removeLastOccurrence() method: [Harsh, Virat, Gaurav]
After invoking clear() method: []
29. }
30. }
31. }
Output:
The JVM uses a JDBC driver to translate generalized JDBC calls into vendor
specific database calls
These drivers convert the JDBC API calls to direct network calls using vendor-
specific networking protocols by making direct socket connections with the
database
It is the most efficient method to access database, both in performance and
development time
It is the simplest to deploy
All major database vendors provide pure Java JDBC drivers for their databases
and they are also available from third party vendors
For a list of JDBC drivers, refer to
http://industry.java.sun.com/products/jdbc/drivers
Pure Java Driver (2)
DB Client Server
Java
Application
Data Source
JDBC
JDBC Driver
API
Typical JDBC Programming Procedure
Class.forName (“oracle.jdbc.driver.OracleDriver”);
con = DriverManager.getConnection (
“jdbc:oracle:thin:@bonsai.ite.gmu.edu:1521:ite”,
“accountname", “password”);
Class.forName (“org.gjt.mm.mysql.Driver”);
con = DriverManager.getConnection
(“jdbc:mysql://localhost/databasename”, uid, passwd);
Creating Tables
SQL query
• Creating JDBC statements
Statement stmt = con.createStatement ();
• Execute a statement
stmt.executeUpdate (“CREATE TABLE COFFEES “ +
“(COF_NAME VARCHAR(32), SUP_ID INTEGER,
PRICE FLOAT, “ + “SALES INTEGER, TOTAL
INTEGER)”);
Execute Statements
Statements that create a table, alter a table, or drop a table are all
examples of DDL statements and are executed with the method
executeUpdate
JNDI
Connection Manager