Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "12-CRS-0106 REVISED 8 FEB 2013 Java Collection. 12-CRS-0106 REVISED 8 FEB 2013 Java Collection."— Presentation transcript:

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

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

3 12-CRS-0106 REVISED 8 FEB 2013 Java Collection add( item ) remove( item ) addAll( collection ) removeAll( collection ) retainAll( collection ) contains( item )

4 12-CRS-0106 REVISED 8 FEB 2013 Java Collection Collection cl = new HashSet(); // Loop using iterator Iterator itr = cl.iterator(); while (itr.hasNext()) { Object o = itr.next(); } // loop using for-element for (Object o : cl) { }

5 12-CRS-0106 REVISED 8 FEB 2013 Generic Collection Collection str = new HashSet (); List arr_i = new List (); ArrayList emp = new ArrayList ();

6 12-CRS-0106 REVISED 8 FEB 2013 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 { int compare(T object1, T object2); }

7 12-CRS-0106 REVISED 8 FEB 2013 Sorting Collection Example public class Employee implements Comparable { 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); }

8 12-CRS-0106 REVISED 8 FEB 2013 Sorting Collection Example import java.util.Comparator; public class MyComparator implements Comparator { public int compare(Employee emp1, Employee emp2){ return emp1.getSalary() - emp2.getSalary(); }

9 12-CRS-0106 REVISED 8 FEB 2013 Sorting Collection Example public static void main(String[] args){ List 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.0 name=bobby, salary=5.0 name=erick, salary=56.0 name=rey, salary=25.0 name=bobby, salary=5.0 name=anna, salary=15.0 name=rey, salary=25.0 name=erick, salary=56.0

10 12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming

11 12-CRS-0106 REVISED 8 FEB 2013 Computer and Me

12 12-CRS-0106 REVISED 8 FEB 2013 Input / Output Input : –Keyboard –File Output –Screen –file

13 12-CRS-0106 REVISED 8 FEB 2013 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

14 12-CRS-0106 REVISED 8 FEB 2013 Java Stream A Java stream is composed of discrete bytes (characters) of data –Byte streams –Character streams

15 12-CRS-0106 REVISED 8 FEB 2013 Byte Streams Object InputStream OutputStream FileInputStream FilterInputStream FileOutputStream FilterOutputStream BufferedInputStream DataInputStream BufferedOutputStream DataOutputStream PrintStream

16 12-CRS-0106 REVISED 8 FEB 2013 Character Streams Object Reader writer BufferedReader InputStreamReader FileReader BufferedWriter OutputStreamWriter PrintWriter FileWriter

17 12-CRS-0106 REVISED 8 FEB 2013 Snippet Code : Write String

18 12-CRS-0106 REVISED 8 FEB 2013 Snippet Code : Read String

19 12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming

20 12-CRS-0106 REVISED 8 FEB 2013 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).

21 12-CRS-0106 REVISED 8 FEB 2013 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

22 12-CRS-0106 REVISED 8 FEB 2013 Live-time Object Illustration Instantiation Object used in program Some-time Object Saved//Resurect into/from Storage Object destroyed

23 12-CRS-0106 REVISED 8 FEB 2013 Object Oriented Live as an Object constructor Save as an Object serializing Read as an Object De-serializing

24 12-CRS-0106 REVISED 8 FEB 2013 Persistence Object To File Object Must be able to transform to binary (implements java.io.Serializa ble) BinaryStream Using io.stream mechanism FileOutputStrea m Write Object to File Using method writeObject() in ObjectOutputSt ream Object in File Open file with FileInputStream class ReadObject in File Using method readObject() in ObjectInputStre am Object Object ready to use

25 12-CRS-0106 REVISED 8 FEB 2013 Snippet Code : Write Object

26 12-CRS-0106 REVISED 8 FEB 2013 Snippet Code : Read Object

27 12-CRS-0106 REVISED 8 FEB 2013 Object Relational Mapping

28 12-CRS-0106 REVISED 8 FEB 2013 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

29 12-CRS-0106 REVISED 8 FEB 2013 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

30 12-CRS-0106 REVISED 8 FEB 2013 Java Hibernate

31 12-CRS-0106 REVISED 8 FEB 2013 Java Persistence API (JPA)

32 12-CRS-0106 REVISED 8 FEB 2013 Question?

33 12-CRS-0106 REVISED 8 FEB 2013 THANK YOU Credits M usic : Yonezawa Madoka - Oui! Ai Kotoba (Instrumental)


Download ppt "12-CRS-0106 REVISED 8 FEB 2013 Java Collection. 12-CRS-0106 REVISED 8 FEB 2013 Java Collection."

Similar presentations


Ads by Google