Summary of collection usage

Source: Internet
Author: User

Collections play an important role in programming. collection operations are nothing more than adding, deleting, traversing, and other basic actions. Therefore, this article describes the above basic operations based on the case and code.
First, the set is divided into Collection and Map ). Simply put, the Collection stores a single content, while the Map set stores keys and values. Just like the table, the Collection has only one column, so it is called a single column set, while Map has two columns, one element of Map is divided into two parts, one column stores the key, one column stores the value, and the key unique cannot be repeated.
Next, let's take a look at the Collection.
Collection
| --- The List element is not unique, and the storage order is the same as that of the retrieved element.
| --- The underlying data structure of ArrayList is an array. Features: Fast query, slow addition, deletion, and Low thread security and efficiency.
| --- The underlying data structure of the Vector is an array. Features: thread security, less efficient than ArrayList, replaced by ArrayList.
| --- Linked list the underlying data structure is the link list. Features: Fast addition, deletion, and slow query. The thread is not safe and efficient.
| --- The Set element is unique, but the storage and retrieval order are inconsistent.
| --- The underlying data structure of HashSet is a hash table. Feature: It depends on the hash table.
| --- The underlying data structure of LinkedHashSet is the hash table and link list. Features: unique and ordered elements.
| --- TreeSet the underlying layer is a binary tree. Features: elements can be sorted.
In the whole Collection system, except for the specific implementation class, the others are interfaces, and only some basic functions are defined. The specific implementation is in the class. It is relatively simple to add or delete elements. For traversal, Collection provides iterator traversal, while the enhanced for traversal actually relies on the iterator at the underlying layer. Currently, this is considered to be two methods, for List sets, you can also use a common for loop with the get () method for traversal.
1. Write the following test code for the List set:

           ArrayList<String> arrayList =  ArrayList<>        arrayList.add("Hello""World""Jason"        arrayList.remove("Jason"        Iterator<String> it ==         (Iterator<String> iterator ==                 ( i = 0; i < arrayList.size(); i++=

For the Set, JDK does not provide the corresponding get () method. Therefore, traversal cannot be performed through a common for loop or the get () method.
2. Sample Code of the Set operation:
The Set used by the Set HashSet is also a TreeSet. For the use of this Set, see the example in the use of the Set TreeSet.
The next step is the subclass of LinkedHashSet and HashSet. This set is special. The underlying structure is linked list and hash table. Although the structure is special, it is not used too much. Let's regard it as a HashSet. for sample code, refer to HashSet. The difference is that it subverts the disorder of the Set, its order depends on the linked list structure, and the only one relies on the hash table structure.

  

After the Collection set is finished, the Map set is followed. At first glance, the format of the Map set is a little scary. In fact, it is quite simple. First, let's look at the structure of the Map set.
Map
| --- The underlying data structure of HashMap is a hash table.
| --- The underlying data structure of LinkedHashMap is a hash table and a linked list.
| --- The underlying data structure of Hashtable is a hash table.
| --- The underlying data structure of TreeMap is a binary tree.
1. Check the code of the HashMap set.

HashMap <String, String> hashMap = HashMap <> hashMap. put ("Liu Lange", "finance" leng wenqing "," business "Li Nian Er", "Development" "Liu Xiaoshi", "market" "Jing yinmeng ", "project" hashMap. put ("Jingyin dream", "design" Set <String> set1 = "Employee:" + emp + "Department:" + Set <Map. entry <String, String> set2 = (Map. entry <String, String> "Employee:" + entry. getKey () + "Department:" +

Note: Associate the specified value with the specified key in the ing.
    To ensure that the key is unique.

   <String, Integer> treeMap =  TreeMap<>( Comparator<String>  num1 = arg0.length() - num2 = num1 == 0 ?"Roxette", 20"Sarah", 21"Carpenters", 23"Britney", 22"Beyonce", 19        treeMap.put("Beyonce", 21<String> set1 ="Name:" + key + "   Age:" +

The sixth in the example will still replace the original value. The unique and sorted operations of TreeMap are key-specific operations. For custom classes, you need to implement the Comparable interface or the Comparator interface. Although the String class has implemented the Comparable interface, the Comparator interface is implemented here.
  
If it is a hash table structure, you need to override hashCode () and equals ().
If the binary tree structure is used, Comparable or Comparator must be implemented.

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.