12-CRS-0106 REVISED 8 FEB 2013 Java Collection. 12-CRS-0106 REVISED 8 FEB 2013 Java Collection.

33
Java Collection

Transcript of 12-CRS-0106 REVISED 8 FEB 2013 Java Collection. 12-CRS-0106 REVISED 8 FEB 2013 Java Collection.

Java Collection

Java Collection

Java Collection

add( item )

remove( item )

addAll( collection )

removeAll( collection )

retainAll( collection )

contains( item )

Java CollectionCollection cl = new HashSet();

// Loop using iteratorIterator itr = cl.iterator();while (itr.hasNext()) {

Object o = itr.next();}

// loop using for-elementfor (Object o : cl) {

}

Generic CollectionCollection<String> str = new HashSet<String>();

List<Integer> arr_i = new List ();

ArrayList<Employee> emp = new ArrayList ();

Sorting Collection

Collection.sort( list )– Element of List must be comparable (implement

comparable)

Collection.sort( list, comparator )– Create a comparator class to compare the element

– public interface Comparator<T> {int compare(T object1, T object2);

}

Sorting Collection Examplepublic class Employee implements Comparable<Employee> {

private String name; private double salary;

public Employee(String name, double salary) { this.name = name; this.salary = salary; }

public String getName() { return name; }

public double getSalary() { return salary; }

@Override public String toString() { return "name=" + name + ", salary=" + salary; }

@Override public int compareTo(Employee t) { return (this.name).compareTo(t.name); }

}

Sorting Collection Exampleimport java.util.Comparator;public class MyComparator

implements Comparator<Employee>{

public int compare(Employee emp1, Employee emp2){return emp1.getSalary() - emp2.getSalary();

}

}

Sorting Collection Examplepublic static void main(String[] args){

List<Employee> listEmp = new ArrayList();

listEmp.add(new Employee("bobby", 5));listEmp.add(new Employee("erick", 56));listEmp.add(new Employee("anna", 15));listEmp.add(new Employee("rey", 25));

Collections.sort(listEmp);for (Employee listEmp1 : listEmp) {

System.out.println(listEmp1);}

Comparator comparator = new MyComparator();

Collections.sort(listEmp, comparator);for (Employee listEmp1 : listEmp) {

System.out.println(listEmp1);}

}

name=anna, salary=15.0name=bobby, salary=5.0name=erick, salary=56.0name=rey, salary=25.0

name=bobby, salary=5.0name=anna, salary=15.0name=rey, salary=25.0name=erick, salary=56.0

CSG2H3 Object Oriented Programming

Java I/O

Computer and Me

Input / Output

Input : – Keyboard

– File

Output– Screen

– file

Java Stream

Sequence of data of undetermined length

Input streams move data into a Java program usually from an external source– System.in

Output streams move data from a Java program to an external target.– System.out

Java Stream

A Java stream is composed of discrete bytes (characters) of data– Byte streams

– Character streams

Byte Streams

Object

InputStream

OutputStream

FileInputStream

FilterInputStream

FileOutputStream

FilterOutputStream

BufferedInputStream

DataInputStream

BufferedOutputStream

DataOutputStream

PrintStream

Character Streams

Object

Reader

writer

BufferedReader

InputStreamReader FileReader

BufferedWriter

OutputStreamWriter

PrintWriter

FileWriter

Snippet Code : Write String

Snippet Code : Read String

CSG2H3 Object Oriented Programming

Object Persistence

Object Persistence

Persistence is the property of an object through which its existence transcends time (i.e. the object continues to exist after its creator ceases to exist) and/or space (i. e. the objects location moves from the address space in which it was created).

the ability of an object to survive the lifetime of the OS process in which it resides

relevant for objects with an internal state

The state needs to be retained between object deactivation and object activation

Object Persistence

Live-time Object Illustration

Instantiation

Object used in program

Some-time Object

Saved//Resurect into/from Storage

Object destroyed

Object Oriented

Live as an Object• constructor

Save as an Object• serializing

Read as an Object• De-serializing

Persistence Object To FileObject

• Must be able to transform to binary (implements java.io.Serializable)

BinaryStream

• Using io.stream mechanism FileOutputStream

Write Object to File

• Using method writeObject() in ObjectOutputStream

Object in File

• Open file with FileInputStream class

ReadObject in File

• Using method readObject() in ObjectInputStream

Object

• Object ready to use

Snippet Code : Write Object

Snippet Code : Read Object

Object Relational Mapping

Object Relational Mapping

a programming technique for converting data between incompatible type systems in object-oriented programming languages

object-oriented (OO) objects are almost always non-scalar values

However, many popular database products such as structured query language database management systems (SQL DBMS) can only store and manipulate scalar values

Object Relational Mapping

The programmer must either convert the object values into groups of simpler values for storage in the database (and convert them back upon retrieval), or only use simple scalar values within the program.

Object-relational mapping is used to implement the first approach

Java Hibernate

Java Persistence API (JPA)

Question?

THANK YOUCreditsMusic : Yonezawa Madoka - Oui! Ai Kotoba (Instrumental)