Talking about Java Collection

Source: Internet
Author: User
Tags comparable sorts

  

1, the classification of the collection (all located under the Java.util package)

Set Frame system

1) collection is the parent class interface of the collection (except for the map collection), and the Set and list collections are inherited from him;

1> List Interface

1) His realization class has the ArrayList, the vector and the linkedlist three kinds;

Arraylist: A variable-length array is implemented, and the efficiency of random finding and diachronic is high; Adding an element to a collection is with the Add () method, and it is a thread that is unsynchronized, Arraylist grows at 50% of the current length, and the insertion deletion is inefficient;

LinkedList: It is convenient to insert and delete elements by using the method of storing the list, and it is worth to be allowed null; List.add (NULL);

Vector: He and ArrayList very similar, the only difference is that he is thread-safe, that is, thread synchronization, each increase in the original length of one times;

2〉set interface

1) Its realization class has hashset and Threeset;

HashSet: The elements in the collection are unordered, unique, can hold null values, threads are unsynchronized, and when an element is deposited into HashSet, HashSet calls the Hashcode () method of the object to get the Hashcode value of the object, and then according to the The hashcode value to determine where the object is stored in HashSet. Simply put, the HashSet collection holds elements that are compared by equals (), and the return value of Hashcode () is also equal; If you want to put an object in the collection, override the Equals () of that object's corresponding class, and you should override its Hashcode (). The rule is that if the equals of two objects is compared with the return value true, its hashcode value should also be equal, and the property used to compare equals in the object should also be used to match the value of Hashcode.

Threeset: The elements in the collection are ordered, TreeSet is the only implementation class of the SortedSet interface, his two sorts are natural sort and custom sort, in which the natural sort is the default sorting method, and the custom sort needs to implement comparable interface;

Both of these collections need to be asked by iterators because they do not have get ();

2) The implementation class of the map set HashMap, HashTable, TreeMap

The 1〉map collection is a key-value pair that holds the elements, stores the data according to the hashcode value of the key, has a fast access speed, and allows a key of at most one record to be null, but he can have more than one null and does not support thread synchronization.

2〉hashmap and Hashtable differences:

HashMap elements can be empty, threads are unsafe, hashtable elements cannot be empty and thread-safe;

Hashtable is slower than HashMap, because it's synchronous.

  The HASHMAP can be synchronized by using map m = Collections.synchronizedmap (HASHMAP).

TreeMap's internal structure is a red-black tree (also known as the number of sorts, is a two-fork tree), using chained storage, you can specify the comparator comparator,key need to implement comparable interface. Key cannot be null. The storage node performance is slightly worse, because the tree structure needs to be adjusted, the node is used for the link list traversal, but it is ordered comparison, performance medium. The middle order traversal of the tree used for iterating is an ordered sequence. Applies to situations where there is a sorting requirement.

3) iterator (algorithm)

The general traversal array is either a for loop or an enhanced for, and these two methods can also be used in the collection framework, but there is also a way to iterate through the collection framework, which is an object that implements the iterator interface or the Listiterator interface.

1〉 Traversal ArrayList Collection

  List<string> list=new arraylist<string> ();     List.add ("Hello");     List.add ("World");     List.add ("Hahahaha");     The first traversal method uses foreach to traverse list for     (String str:list) {            ///can also overwrite for (int i=0;i<list.size (); i++) This form        System.out.println (str);
The second way, the linked list into the array of related content to traverse
String [] s=new string[list.size ()]
List.toarray (s);
for (int i=0,i<s.length,i++) {
System.out.print (S[i]);
}
Third, iterate with iterators
Iterator Is=list.itrrator ();
while (Is.hanext) {


}

2> traversing the Map collection

map<string, string> map = new hashmap<string, string> ();      Map.put ("1", "value1");      Map.put ("2", "value2"); Map.put ("3", "Value3");//The first type: Universal use, two-time value
System.Out.println("Traverse key and value through Map.keyset:");
For (String Key:Map.KeySet()) {
System. Out. println("key= "+ key + "and value= " + map. ) Get(key));
}
     //The second Kind
System.Out.println("Use iterator to traverse key and value through Map.entryset:");
For (entry<string,string> Item:map.entrySet ()) {
System.out.print (Item.getkey () + "-" +item.getvalue ())
}
//Third Kind
System.Out.println("Traverse all value through Map.values (), but cannot traverse key");
For (String V:map. Values) Span class= "hl-brackets" >{
       system. Out. Println ( "value= " + v       }
      }


  

  

Talking about Java Collection

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.