J2SE basics: 9. Integration, j2se Basics
1: Set:
The concept of a set: Data container (similar to an array ). Containers are suitable for object loading.
2: Advantages and Disadvantages of collections and arrays:
Array access efficiency is high, requiring a fixed capacity during use; suitable for accessing basic data types;
There is a certain sacrifice in the collection access efficiency, and the size of the collection capacity can be changed according to actual needs,
Provides rich methods for saving and retrieving objects, and is suitable for the "Container" of objects"
3: Collection framework system diagram
Interface icon
Class icon
Inherited icon: solid arrow
Implementation icon: dotted line
4: Classification of the Collection framework:
A: SET)
B: List)
C: Map)
D: Queue)
Collection Method
Add)
Remove (delete)
Size)
Clear)
Iterator (Iterator)
ToArray (convert to array)
A: SET
Features: (1) not repeated (equal logic, equals );
(2) You can add a NULL element.
HashSet:
Features: (1) Hash Algorithm Implementation, unordered.
(2): It is not synchronized by threads.
Because the objects in the HashSet are not repeated. Therefore, you must override the equals method of the object. Because it is not repeated
When adding objects, check whether the comparison is repeated. Low efficiency.
Therefore, when adding elements to a set, first compare whether the hashCode of the object exists in the set.
If not, add
If yes: compare whether two objects are equal. If equals () is used, the object is repeated and is not added.
If not equals (), the object is unique.
LinkedHashSet
Features: (1) Implementation of the underlying mechanism linked list.
(2): elements are ordered. (It refers to the order of the added elements, not the order between objects)
(3): It is not synchronized by threads.
(4): it is easier to add, delete, and modify objects because linked lists are used. Slow query.
CopyOnWriteArraySet
Features: (1) the underlying mechanism is implemented in array mode.
(2): elements are ordered. (It refers to the order of the added elements, not the order between objects)
(3): It is synchronized by threads.
(4): the search is faster because it is implemented using arrays. Addition, deletion, and modification are slow.
Thread Synchronization and non-thread synchronization:
Thread Synchronization: when multiple threads simultaneously access a resource, only one thread can access the resource. Therefore, Efficiency Comparison
Slow. However, data is synchronized to multiple threads.
Non-thread synchronization: Multiple Threads can access a resource at the same time. Therefore, Efficiency Comparison
Fast, but the data seen by multiple data will not be synchronized.
TreeSet
Features: ordered elements (ordered objects)
It is known that A = {,}, B = {, 17} of the set is used to take A number of each of Set A and B as the coordinate of the point, which is determined in the same Cartesian coordinate system.
I got 32 types of.
(1, 1) (1, 7) (1, 17)
(3, 1) (3, 7) (3, 17)
(5, 1) (5, 7) (5, 17)
(7,1) (7,7) (7,17)
(9,1) (9,7) (9,17)
(11,1) (11,7) (11,17)
)
(7,3) (7,5) (7,9) (7,11)
(17,1) (17,3) (17,5) (17,7) (17,9) (17,11)
If you do not want to repeat it, there will be 6*3*2 = 36 combinations
Because there are the same numbers, there are four combinations repeated twice (), So 36 combinations must be subtracted from 4. Your results are correct. Check whether the answer is correct.
It is known that the Set A = {2, 4, 6, 8, 9}, B = {1, 2, 3, 5, 8}, and the set C is such A set: After all its elements are added with 2, it becomes A user of.
After adding 2 to each element, it becomes A subset of A. If all elements minus 2, it becomes A subset of B,
That is, the mean of two elements with 4 differences in element size in the AB set, and the element in A is greater than the element in B.
That is, element a B in element a B is taken as Element B a-B = 4 and (a + B)/2 belongs to C.
C = {1, 4.7}
(6 + 2)/2 = 4
(9 + 5)/2 = 7