Part iv Distributed Java Applications and Sun JDK libraries

A collection of package

CollectionList interface:

List interface: List(ordered, repeatable) implementation classes are ArrayList, Vector, LinkList ArrayList, Vector is implemented through arrays, LinkList is implemented through bidirectional lists. Vector supports thread synchronization (Stack is the implementation of Vector).

Set interface: Set(unordered, non-repeatable) implementation classes include HashSet(based on HashMap, which can be null) and TreeSet(based on TreeMap, which cannot be null). Both HashSet and TreeMap are non-thread-safe.

Queue interface: Queue: implementation class has Deque: bottom is an array, there are 2 flag bits pointing to the head and tail of the array to achieve a double-ended Queue.

The Map interface:

HashMap (sorting not supported)

  • An array is used to store Entry objects consisting of keys and values without capacity limitation
  • Locate objects based on key hash, and resolve hash conflicts using linked list or open address method
  • When the array size needs to be expanded, the hash value is recalculated
  • HashMap is thread-unsafe

TreeMap (sorting support)

  • Based on red black tree implementation, no capacity limitation
  • Not thread-safe
  • sort-enabled

Hashtable thread safe

The differences between a HashMap and a HashTable are as follows: 1. The key and value of a HashMap object can be null. The key and value of the HahTable object cannot be null. 2.HashTalle is thread-safe and HashMap is not thread-safe. 3.Hashtable Recalculates the hash value using the object’s Hashcode and HashMap. 4.HashTable uses Enumeration, whereas HashMap uses Iterator. Enumeration can Only be Read-Only and cannot be deleted.

Summary: 1. Only Vector and Hashtable Collections are thread-safe (other Collections can be thread synchronized using collections.syschronied ***()) 2. The List interface is implemented in Collections and TreeMap and Linkhashmap support sorting (using collections.sort)

And contract

Common set of synchronization methods: HashMap: ConcurrentHashMap HashMap implementation is thread-safe. ArrayList: CopyOnWriteArrayList is thread-safe, and reading operation is unlocked ArrayList. Set:CopyOnWriteArraySet is realistic based on CopyOnWriteArrayList. Queue:ArrayBlockingQueue is an array-based, first-in, first-out, thread-safe collection class that blocks at specified times and has a limitable capacity.

Serialization and deserialization

ByteArrayOutputStream /ByteArrayInputStream ObjectOutputStream/ObjectInputStream

The process of converting an object to a byte sequence is called object serialization, and the process of restoring a byte sequence to an object is called object deserialization.

Purpose: file copy, network data transmission.