Docsity

Java Containers, Collections, Maps, I/O, Serialization, Classes, Threads, GUI, Slides of Java Programming

Partial preview of the text

Download Java Containers, Collections, Maps, I/O, Serialization, Classes, Threads, GUI and more Slides Java Programming in PDF only on Docsity!

Programming Language - JAVA

Roadmap today

Review Inner class Multi-threading Graphics

Collection List ArrayList LinkedList Vector Set HashSet LinkedHashSet TreeSet Collection: hold one item at each location List: items in order Set: no duplicates, no ordering Preserve the insertion of the elements

Map HashMap LinkedHashMap Hashtable TreeMap Map: key-value pairs, fast retrieval no duplicate keys, no ordering Preserve the insertion of the elements

Iterator object

Access method regardless of the underlying structure Generic programming Can change underlying structure easily “light-weight” object Cheap to create Can move in only one direction

Iterator constraints

 Container. iterator () returns you an Iterator, which is ready to return the first element in the sequence on your first call to next ()  Get the next object in the sequence with next ()  Set there are more objects in the sequence with hasNext ()  Remove the last element returned by the iterator with remove ()  Example: revisit CatsAndDogs.java

Serialization

An important feature of Java Convert an object into a stream of byte, and can later deserialize it into a copy of the original object Takes care of reassembling objects Need to cast type when read back Any object as long as it implements Serializable interface No method inside

Java networking

Java.net.

Socket, ServerSocket

Grab the associated streams InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); URL class InputStream is = u.openStream();

Inner class

What if we want to use classes for our internal implementation?  Inner classes!A class defined inside another class Similar to the methods or fields of class Can be static, public, private Can be private so no other class can use it Usable by enclosing class

Example

InnerClassTest.java

Anonymous class

A local class without a name Defined and instantiated in a single expression using the new operator. public void start (){ ActionListener listener = new ActionListener{ public void actionPerformed(ActionEvent e){ System.out.println(“get the event ”+e); } } ; //Need to give a ; here, since it’s an expression }

When to use anonymous class?

The class has a short body Only one instance of the class is needed The class is used right after it is defined The name of the class does not make your code any easier to understand

Threads

Threads are lightweight, application-level form of multi-tasking Also preemptive (when time is up, switch to another one) But not protected: all can see the same memory Which means much faster communication than classic “interprocess communication (IPC)”

All Java programs are MT

You have the main() thread, and The garbage collector thread And any other thread by yourself