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

.ArchJava Lesson 10-5 (List)

Uploaded by

kaung71669
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

.ArchJava Lesson 10-5 (List)

Uploaded by

kaung71669
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Java Programming: Collection

Framework in Java

Dr. Kyawt Kyawt Htay


Lecturer

©https://blogs.ashrithgn.com/
Objectives

• To understand Collection Framework in java

• To manipulate a collection of objects, using Lists

• To describe the methods of List interface

• To understand Automatic Boxing and Unboxing

Faculty of Computer Science ©https://blogs.ashrithgn.com/


What is Collection Framework?

• The Collection framework represents a unified architecture for storing and


manipulating a group of objects. It has:
• Interfaces and its implementations, i.e., classes
• Algorithm

• Collection interfaces:
• Represent different types of collections, such as sets, lists, and maps. These
interfaces form the basis of the framework.

• The collection interfaces are divided into two groups:


• java.util.Collection
• java.util.Map Faculty of Computer Science ©https://blogs.ashrithgn.com/
Collection Framework Hierarchy

java.util
package
contains all the
classes and
interfaces for
the Collection
framework

https://www.javatpoint.com/collections-in-java
Faculty of Computer Science ©https://blogs.ashrithgn.com/
What is List?

• List: Ordered collection that can contain duplicate elements.

• List interface is found in the java.util package and inherits the Collection
interface. java.util.Collection
• The implementation classes of List interface :

• ArrayList

• LinkedList

• Stack

• Vector
Faculty of Computer Science ©https://blogs.ashrithgn.com/
List Declaration and Creation

• Declaration:
interface-name<element-type> identifier;

• Creation:
identifier = new class-name<element-type> (parameters);

• Example that declares and creates a list of Person objects:


List<Person> friends = new ArrayList<Person> ( );

Faculty of Computer Science ©https://blogs.ashrithgn.com/


Methods of the List interface

Method Description
add() adds an element to a list
addAll() adds all elements of one list to another
get() helps to randomly access elements from lists
iterator() returns iterator object that can be used to sequentially access elements of lists

set() changes elements of lists


remove() removes an element from the list
removeAll() removes all the elements from the list
clear() removes all the elements from the list (more efficient than removeAll())
size() returns the length of lists
toArray() converts a list into an array
contains() returns true if a list contains specified element

Faculty of Computer Science ©https://blogs.ashrithgn.com/


Automatic Boxing

• Auto boxing : With a list, we can store only objects. If we need to store primitive
data values in a list, then we must use wrapper classes such as
Integer, Float, and Double.
There are no restrictions on
the type of objects we can
List<Integer> intList = new ArrayList<Integer>(); add to the list.
intList.add(15);
intList.add(30); the compiler translates it to

int sum=0; intList.add(new Integer(30));
for ( int value : intList){
sum+=value; Automatic boxing
}
Faculty of Computer Science ©https://blogs.ashrithgn.com/
Automatic Unboxing

• Auto unboxing :

int num = intList.get(1); intValue method used to


get the integer value
the compiler translates it to

int num = intList.get(1).intValue( );

Automatic unboxing

Faculty of Computer Science ©https://blogs.ashrithgn.com/


List with Primitive Data Type

package bosjavaprogram;
import java.util.*;
public class ListDemo {
public static void main(String[] args) {
List<Integer> list1= new ArrayList<Integer>();
list1.add(0, 1); Add element to list at
list1.add(1, 2); specific index position
System.out.println(list1); [1, 2]
List<Integer> list2= new ArrayList<Integer>();
list2.add(1);
list2.add(2);
list2.add(3);
System.out.println(list2); [1, 2, 3]
Faculty of Computer Science ©https://blogs.ashrithgn.com/
Cont’d
list1.addAll(1, list2);
Output:
System.out.println("After inserting list2 to
list1 at position 1: "+list1);
int index = list1.indexOf(2);
System.out.println("Position of 3 is " + index);

list1.remove(1);
System.out.println(list1);
System.out.println(list1.get(3));
list1.set(0, 5); Change element
System.out.println(list1); specific index position
}
} Faculty of Computer Science ©https://blogs.ashrithgn.com/
List with Object Data Type

package bosjavaprogram;
import java.util.*;
class Book {
int id;
String name, author, publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
} Faculty of Computer Science ©https://blogs.ashrithgn.com/
Cont’d

public static void main(String[] args){


List<Book> list=new ArrayList<Book>(); Creating Books
Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(102,"Data Communications and Networking","Forouzan","Mc Graw Hill",4);
Book b3=new Book(103,"Operating System","Galvin","Wiley",6);
list.add(b1);
Adding Books
list.add(b2);
to List Output:
list.add(b3);
for(Book b:list){
System.out.println(b.id+" "+b.name+" “
+b.author+" "+b.publisher+" "+b.quantity);
}
}} Faculty of Computer Science ©https://blogs.ashrithgn.com/
Sample Program: List Iterator
package bosjavaprogram;
import java.util.*;
public class ListIteratorExample1{
public static void main(String args[ ]){
List<String> al=new ArrayList<String>( );
al.add("Amit");
al.add("Vijay"); List interface provides a listIterator()
al.add("Kumar"); method that returns an instance of
al.add(1,"Sachin"); the ListIterator interface.

ListIterator<String> itr=al.listIterator( );

Faculty of Computer Science ©https://blogs.ashrithgn.com/


Cont’d

System.out.println("Traversing elements in forward direction");


while(itr.hasNext( )) {
System.out.println("index:"+itr.nextIndex( )+"
Output:
value:"+itr.next( ));
}
System.out.println("Traversing elements in backward direction");
while(itr.hasPrevious( )){
System.out.println("index:"+itr.previousIndex( )+"
value:"+itr.previous( ));
}
}
}
Faculty of Computer Science ©https://blogs.ashrithgn.com/
Summary

• What is Collections Framework?

• What is List ?

• Methods of the List interface

• Automatic Boxing & Unboxing

• Using List Iterator

Faculty of Computer Science ©https://blogs.ashrithgn.com/


Practice program

• ListDemo.java

• Book.java

• ListIteratorExample1.java

https://docs.oracle.com/javase/8/docs/
technotes/guides/collections/overview.html
Faculty of Computer Science ©https://blogs.ashrithgn.com/
What will be next?

• The next lecture will focus how to use Map interface.

Faculty of Computer Science ©https://blogs.ashrithgn.com/

You might also like