Java collection classes

Source: Internet
Author: User

Common collections in Java are the list collection, the Map collection, and the set set. the list collection and set set inherit the collection interface

List is an ordered, repeatable collection of elements, and the list collection interfaces commonly used implementation classes are ArrayList and LinkedList. The ArrayList can provide fast random access to the collection based on the index location, with the disadvantage of inserting or deleting objects at the specified index location at a slower speed; LinkedList makes it easy to insert and delete objects into the collection, with the disadvantage that objects in the collection are less efficient to access

PackageCom.list;ImportJava.util.Iterator;ImportJava.util.List;/*** List is an ordered, repeatable set of elements*/PublicClassarrayorlinkedlist{PublicStaticvoidMain (string[] args) {//List<user> arraylist=new java.util.arraylist<user> (); List<user> arraylist=New java.util.linkedlist<user> (); User Userone=new User (1, "John Doe" new User (2, "Zhang San" // looping through for (int i=0;i< Arraylist.size (); I++ ); } //iterator Traverse iterator<user> it= Arraylist.iterator (); while (It.hasnext ()) {System.out.print (It.next (). GetUserName () + "" //foreach for (User user:arrayList) { System.out.print (User.getusername () + ""          
PackageCom.list;/*** User class **/PublicClassUser {PrivateIntUserId;PrivateString UserName;Public User (IntUserId, String userName) {super (); this.userid = UserId; this.username = UserName;} public int GetUserId () {return UserId; } public void Setuserid (int UserId) { Span data-mce-= "" >this.userid = UserId; public String GetUserName () {return UserName; } public void Setusername (String userName) { this.username = UserName;}         

Set is an unordered, non-repeatable set of elements , and the common implementation classes of set-set interfaces are HashSet and TreeSet

PackageCom.list;ImportJava.util.Iterator;ImportJava.util.Set;/*** Set is an unordered, non-repeatable set of elements*/Publicclass HashSet {public static void Main (string[] args) {set<user> hashset=new Java.util.hashset<user> (); User Userone=new User (1, "John Doe" new User (2, "Zhang San" //iterator Traverse iterator<user> it=hashSet.iterator (); Span data-mce-= "" >while (It.hasnext ()) {System.out.print (It.next (). GetUserName () + "" for (User user:hashset) {System.out.print (User.getusername () + ""  

The map collection stores a key value (Key-value) pair that cannot contain duplicate keys, but can contain the same value

PackageCom.list;ImportJava.util.Iterator;ImportJava.util.Map;PublicClassHashMap {PublicStaticvoidMain (string[] args) {map<integer, string> Map =New Java.util.hashmap<integer, string>(); Map.put (1, "a"); Map.put (2, "B"); Map.put (3, "AB"); Map.put (4, "AB"); Map.put (4, "AB");System.out.println (Map.size ());//The first type:/** Set<integer> Set = Map.keyset (); Get the collection of all keys * * for (Integer in:set) {String str = map.get (in); * SYSTEM.OUT.PRINTLN (In + "" + str);}*/System.out.println ("The first: Traverse key and value through Map.keyset:");For(Integer In:map.keySet ()) {//Map.keyset () returns the value of all keys String str = Map.get (in);//Get each key many pairs with value System.out.println (in + "" +STR); }//The second type: System.out.println ("The second type: using iterator to traverse key and value through Map.entryset:"); Iterator<map.entry<integer, string>> it =Map.entryset (). iterator ();While(It.hasnext ()) {Map.entry<integer, string> Entry =It.next (); System.out.println ("key=" + entry.getkey () + "and value=" + Entry.getvalue ()); }// third: recommended, especially when the capacity is large System.out.println ("Third: Traverse key and value through Map.entryset" for (Map.entry<integer, String> Entry:map.entrySet ()) { // map.entry<integer,string> mapping Entries (key-value pairs) There are several ways: Use the name above entry // Entry.getkey (); Entry.getvalue (); Entry.setvalue (); // Map.entryset () returns the set view of the mappings contained in this map. System.out.println ("key=" + entry.getkey () + "and value=" + Entry.getvalue ());} // Fourth: System.out.println ("Fourth type: Traverse all the value through Map.values (), but cannot traverse key" for (String v:map.values ()) {System.out.println ("value=" +  V); } }} 

Java collection classes

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.