The architecture of the collection-the basics of the collection

Why do you need a collection

Object-oriented languages ​​reflect things in the form of objects. In order to facilitate the operation of multiple objects, it is necessary to store multiple objects. Because arrays have some drawbacks, collections are introduced.

  1. Both collections and arrays are structures that store multiple data, referred to as Java containers.
  2. The characteristics of the array, once initialized, its length is fixed; when declaring the array, you need to specify the type of the array, and the type of its elements is also determined. For example: String [] arr; int [] arr; Object [] arr;
    3. The shortcomings of arrays (in turn, the advantages of collections)
    once initialized, the length cannot be modified;
    the methods provided in the array are very limited. , Deleting and inserting data is very inconvenient, and the efficiency is also very low; to
    get the number of actual elements in the array, the array has no ready-made attributes or methods available;
    the characteristics of the array to store data are ordered, and there can be no spaces in the middle. The characteristics of stored data are orderly and repeatable. Unable to meet the disorderly and non-repeatable needs.

Java collections can be divided into two systems: Collection and Map

  1. Collection interface: single-column data, which defines a collection of methods for storing a group of objects
  • List interface: The elements are ordered and repeatable. (Often referred to as "dynamic" array, auto-expanding)
    ArrayList, LinkedList, Vector
  • Set interface: The elements are disordered and cannot be repeated. (Sets mentioned in high school. Disorder, certainty, reciprocity)
    Set sets are often used for data filtering.
    HashSet, LinkedHashSet, TreeSet
  1. The Map interface
    double-column data, saves a collection of "key-value pairs" with a mapping relationship.
    (Y = f(x) I have learned in high school. Each x corresponds to a y. Different ones may correspond to the same y, but one x cannot correspond to multiple ys. The Map set is also a value that can correspond to different keys, but One key cannot correspond to multiple values)
    HashMap, LinkedHashMap, TreeMap, HashTable, properties

Collection interface inheritance treeMap interface inheritance tree
Choose different containers according to different functions.

Review

Use of methods in the Collection interface

Collection collection = new ArrayList();
add(); size(); addAll();clear(); isEntry();

The similarities and differences between throw and throws

  1. The same point: No (all used in the exception handling part, does this count?)
  2. different:
  • throw: Generate an exception object and throw it. The location used is inside the method, and the exception object is automatically thrown.
  • throws: The way to handle exceptions is used at the end of the method declaration. Another corresponding way is try catch finally
    "Upstream pollution discharge, downstream pollution control".
    Talk about your understanding of the synchronization monitor and shared data in the synchronization code block. Individual requirements.
    Synchronization monitor: commonly known as lock. ① Any object of a class can act as a lock. ② Multiple threads share the same lock.
    Shared data: data that multiple threads operate together, that is, synchronized data. The synchronization mechanism that needs to be used is to package the code that operates the shared data. You can't pack too much, and you can't pack less.

Guess you like

Origin blog.csdn.net/weixin_43941676/article/details/108186761