Java programming ideology Reading Notes-3 (Chapter 1-1 Use of containers and their working principles)

Source: Internet
Author: User

Chapter 4 hold your object
1. Introduction to containers
1. container classification
1.1. Collection: A group of independent elements, that is, each position in the group holds only one element.
1) List: the elements are placed in the order of element insertion and will not be rearranged.
2) Set: it does not like repeated elements. It uses its own internal arrangement mechanism.
1.2. Map: a group of key-value objects holding key-value pairs.
A Map cannot contain duplicate keys. It has its own internal arrangement mechanism.
2. The element types in the container are all objects. When getting an element from a container, you must convert it to the original type.
Ii. Container details
1. Collection
Collection does not provide the get () method. To traverse the elements in Collectin, you must use Iterator.
1.1. List
1.1.1 List (interface): List adds some functions to Collectin so that it can be inserted and removed from the List. The ListIterator is generated by List, which can be used to access the List in two directions, or to insert and remove elements in the List.
1.1.2 ArrayList: quick and random access. However, it is inefficient to insert or remove an element at the central position of the List. It is not advisable to use ArrayList for the insert and remove operations.
1.1.3 reverse list: opposite to ArrayList, it is suitable for inserting and removing, but the random access speed is slow. In addition, stack, queue, and deque can be implemented through the consumer list.
1) The addFirst (), addLast (), getFirst (), getLast (), removeFirst (), and removeLast () functions in the detail list are not defined in any interface or base class, therefore, it can only be used in the listing list.
1.2. Set
1.2.1 Set (interface): Set has the same interface as Collection (difference: List adds its own function), so Set is a Collection, but its behavior is different. Each element added to the Set must be unique and not repeated with other elements. Set does not allow repeated elements. Each element must define equals () to determine the uniqueness.
1.2.2 HashSet: a set that determines the search time. All elements must define hashCode ().
1.2.3 TreeSet: an ordered Set with the underlying structure of tree.
2. Map
2.1. Map: maintain the relevance of the key-value so that you can use the key to find the value.
1) KeySet () and values () Functions

  1. ImportJava. util .*;
  2. Public ClassExplicitStatic {
  3. Public Static VoidPrintKeys (MapM ){
  4. System. Out. print ("Size =" + m. size ());
  5. System. Out. println (", Keys:" + m. keySet ());
  6. }
  7. Public Static VoidPrintValues (MapM ){
  8. System. Out. println ("Values:" + m. values ());
  9. }
  10. Public Static VoidTest (MapM ){
  11. For(IntI = 1; I <10; I ++)
  12. M. put ("km" + I, "m" + I );
  13. PrintKeys (m );
  14. PrintValues (m );
  15. System. Out. println ("km1-" + m. get ("km1 "));
  16. SetKeys = m. keySet ();// (1)
  17. CollectionValues = m. values ();// (2)
  18. Keys. remove ("km2 ");// (3)
  19. Values. remove ("m1 ");// (4)
  20. System. Out. println ("km1-" + m. get ("km1 "));
  21. PrintKeys (m );
  22. PrintValues (m );
  23. }
  24. Public Static VoidMain (String[] Args ){
  25. System. Out. println ("Testing HashMap ");
  26. Test (New HashMap());
  27. }
  28. }

Result:
Testing HashMap
Size = 9, Keys: [km5, km4, km3, km1, km1, km9, km8, km7, km6]
Values: [m5, m4, m3, m2, m1, m9, m8, m7, m6]
Km1-m1 // before execution (3) (4)
Km1-null
Size = 7, Keys: [km5, km4, km3, km9, km8, km7, km6] // (5)
Values: [m5, m4, m3, m9, m8, m7, m6] // (6)
At (1) (2), the code obtains the keys and values in Map. We can see from the code before and after execution (3) (4) that modifying the values obtained through the keySet () and values () functions reflects the Map itself.
(3) the key with the value of "km²" is deleted, (4) the value with the value of "m1" is deleted, and they are the same key-value pair, however, the result (5) (6) indicates that two keys-pair are deleted in Map. Slave

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.