A collection framework for the Java Foundation

Source: Internet
Author: User

Collection Framework (Collections framework) 1. Two large base classes collection and map

1). Public interface collection<e>extends Iterable<e>:Collection represents a set of pure data

Collection inherits from the Iterable interface, and the Iterable interface provides a way to directly get the iterator object, so all collections that inherit from Iterable can use the iterator iteration

2) Map represents a group of Key-value

Generally inherited from Collection or Map of a collection class, it provides two "standard" constructors:

    • constructor with no arguments, creating an empty collection class
    • Has a type with Collection the same constructor as the base class (or Map ), creating a new collection class with the same elements as the given parameter

      Because the interface cannot contain constructors, the conventions of the above two constructors are not mandatory, but in the current collection framework, all inherited Collection or derived Map subclasses follow this convention.

Inheritance schema for collection

Only the more commonly used classes are given here.

As shown, the collection class has three main interfaces:

    • SetRepresents a collection that does not allow duplicate elements (but can be null) (A collection that contains no duplicate elements)
    • ListRepresents a collection of allowed duplicate elements (an ordered collection (also known as a sequence))
    • QueueJDK1.5 is added, and the main difference between the above two collection classes is that Queue they are primarily used to store data instead of processing data. (A collection designed for holding elements prior to processing.)
The Inheritance schema of map

Map is not a true set (is not true collections), but this interface provides three "set View" (collection views), so that you can manipulate them like the set of operations, as follows:

The contents of the map are treated as a set of keys (map ' s contents to be viewed as a set of keys

    • Consider the contents of the map as a collection of values (map ' s contents to be viewed as a collection of value)
map<string,string> map =NewHashmap<string,string>(); Map.put ("Zhangsan", "the"); Map.put ("Lisi", "the"); Map.put ("Wangwu", "the"); Set<String> KeySet = Map.keyset ();//gets the set set of all keys for the map collection firstIterator<String> it = Keyset.iterator ();//with the set set, you can get its iterators.                          while(It.hasnext ()) {String key=It.next (); String value= Map.get (key);//a key can get its corresponding value through the Get method of the map collection. System.out.println ("Key:" +key+ "-->value:" +value);//get the key and value values}
iterate through keyset ()
    • Consider the contents of the map as a collection of key-value mappings (map ' s contents to be viewed as a set of key-value mappings)
New map.put("hashmap<string,string>", "Zhangsan"), Map.put ("The", "Lisi"); Map.put ( "", "Wangwu"); Collection<String> Collection = Map.values (); // The return value is a collection collection of values System.out.println (collection);
use the values () traversal
    • Consider the contents of the map as a collection of key-value mappings (map ' s contents to be viewed as a set of key-value mappings)
map<string,string> map =NewHashmap<string,string>(); Map.put ("Zhangsan", "the"); Map.put ("Lisi", "the"); Map.put ("Wangwu", "the");//The mapping relationship in the map collection is taken out by the EntrySet () method (the relationship is the Map.entry type)set<map.entry<string, string>> entryset =Map.entryset ();//iterate over the entryset of the relationship collection and store it in an iteratoriterator<map.entry<string, string>> it2 =Entryset.iterator ();  while(It2.hasnext ()) {Map.entry<string, string> me = It2.next ();//Get Map.entry Relationship object meString Key2 = Me.getkey ();//get key from a relational objectString value2 = Me.getvalue ();//get value from a relational objectSystem.out.println ("Key:" +key2+ "-->value:" +value2);}
With EntrySet () traversal, this approach is recommended for highest performanceHashMap
hashmap<k,v>extends abstractmap<k,v>implements map<k,v>, cloneable, Serializable

Characteristics:
    • Thread is non-secure and allows both key and value to be null, HashTable and conversely, for thread safety, both key and value do not allow null values.
    • The order of internal elements is not guaranteed, and the position of the same element may change over time (resize case)
    • The time complexity of the put and get Operations is O (1).
    • The time complexity of traversing its set view point is proportional to its capacity (capacity, number of slots) and the size of the existing elements (number of entry), so if the traversal performance requirements are high, do not put the capactiy set too much or the balance factor (load factor, When the number of entry is greater than Capacity*loadfactor, the resize,reside will cause the key to be rehash) set too low.
    • Since HashMap is thread-insecure, this means that if multiple threads are attempting to iterate over a HashMap collection at the same time (adding, deleting, entry, changing the value of entry only does not count structural changes), Then will be reported concurrentmodificationexception, professional terminology fail-fast , early error is necessary for multi-threaded program.
    • Map m = Collections.synchronizedMap(new HashMap(...));In this way, a thread-safe map can be obtained.
TreeMap
treemap<k,v>extends abstractmap<k,v>implements navigablemap<k,v>, cloneable, Serializable

A collection framework for the Java Foundation

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.