Encounter Java Collection Framework

Source: Internet
Author: User

Java Collection Framework is an important part of the Java language, it contains a system and a complete set of hierarchical system, encapsulating a large number of data structure implementation, a deep understanding of the composition of the Java Collection framework and its implementation classes and algorithms, can greatly improve the programmer's ability to encode.


first, the concept
Also known as a container, is an object that can assemble multiple elements of the same nature into a single whole. A collection framework is used to store, acquire, manipulate, and transmit aggregated data.

Ii. Introduction of Members
For example, the Java platform provides a new collection framework whose core interfaces are collection, list, set (set), and map (map). As the graph shows, collection is the topmost interface in the collection inheritance tree, and almost all of the containers implement that interface.




Three structures for collections, lists, and mappings:
List: unordered, no duplicate Items collection list: Ordered, repeatable map: Keywords and value pairs, keywords are unique labels cannot be duplicated, data can be duplicated.




third, ArrayList classis an extension to the Abstractlist class that supports dynamic arrays that can grow as needed. Standard arrays in Java are ordered, and they cannot be extended or shortened after they are created, which means that the developer must first know how many elements the array can hold. In general, the class set framework defines ArrayList, which can dynamically increase or decrease its size, only when it is run to know how large an array is needed.

EG1:
Import java.util.*;p ublic class test{public    static void Main (string[] args) {        Collection c=new ArrayList ();        C.add ("Hello");        C.add (New Name ("F1", "one"));        C.add (new Integer);        System.out.println (C.size ());        System.out.println (c);    }}






Four, HashSet class
A set collection is a collection that does not contain duplicate elements. is an extension to the Abstractset class that creates a set of classes that are stored using a hash table, whereas a hash table stores information by using a mechanism called hashing. In a hash, the information content of a keyword is used to determine a unique value, called a hash code, which is used as the storage subscript for the data connected to the keyword.

EG2:
Import java.util.*;p ublic class basiccontainner{public static void Main (string[] args) {Collection c=new HashSet (); C.add ("Hello"), C.add (New Name ("F1", "one")), C.add (New Integer), C.remove (New Integer (100)); System.out.println (C.remove (New Name ("F1", "11")); System.out.println (c);}} Class Name{private string Firstname,lastname;public Name (String firstname,string lastName) {this.firstname=firstname; This.lastname=lastname;} Public String Getfirstname () {return firstName;} Public String Getlastname () {return lastName;} Public String toString () {return firstName + "" + LastName;} public boolean equals (Object obj) {if (obj instanceof Name) {name name= (name) Obj;return (Firstname.equals (name.firstname ) && (Lastname.equals (Name.lastname));} return super.equals (obj); public int hashcode () {return Firstname.hashcode ();}}




Note: 1, rewrite equal also to override Hashcode Method 2, when the object is used as an index, use the Hashcode method
Five, HashMap class
let's start by describing what a map is. In the array we index the content by the array subscript, and in the map we index the object by object, the object to index is called key, and its corresponding object is called value. HashMap classThe implementation of a hash table-based MAP interface. This implementation provides all the optional mapping operations and allows NULL values and NULL keys to be used. using Get () to get value in HashMap, inserting Value,containskey () through put () is used to verify that the object already exists. It can be seen that, compared with ArrayList's operation, HashMap is not very different from other aspects except by indexing its contents by key.

EG3:
Import java.util.*;p ublic class myhashmap{public static void Main (string[] args) {HashMap hm=new HashMap (); Hm.put (1, ' Z ') ; Hm.put (2, ' H '); Hm.put (3, ' O '); Hm.put (4, ' U '); System.out.print ("The result after adding an element is:"); SYSTEM.OUT.PRINTLN (HM); Hm.remove (3); Hm.put (2, "replace"); System.out.print ("Results after deleting and replacing elements:"); SYSTEM.OUT.PRINTLN (HM);}}


Vi. Summary
this blog mainly introduces the concept of the collection framework, the collection framework interface, as well as the list, collection, mapping three kinds of structure. There is also a need to strengthen the understanding of the set framework in future operations.

Encounter Java Collection Framework

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.