Summary and introduction of list, set, and map in Java __java

Source: Internet
Author: User
Tags int size set set
Package hing.test.collection;
Import java.util.ArrayList;
Import Java.util.Comparator;
Import Java.util.EnumMap;
Import Java.util.EnumSet;
Import Java.util.HashMap;
Import Java.util.HashSet;
Import java.util.Hashtable;
Import Java.util.Iterator;
Import Java.util.LinkedHashMap;
Import Java.util.LinkedHashSet;
Import java.util.LinkedList;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Set;
Import Java.util.TreeMap;
Import Java.util.TreeSet;

Import Java.util.Vector;

Import Org.junit.Test; The public class Testcollection {@Test the public void Testcollection () {//collection interface is the parent interface of the List,set and queue interfaces/** * Col
		 Lection the method of the set of operations defined by the interface: * * Boolean Add (Object o);
		 * Boolean AddAll (Collection c);
		 * void Clear ();
		 * Boolean contains (Object O);
		 * Boolean contains (Collection c);
		 * Boolean isempty ();
		 * Iterator iterator ();
		 * Boolean remove (Object O);
		 * Boolean RemoveAll (Collection c);
		 * Boolean Retaionall (Collection c);
		 * int size (); * object[] tOarray ();
	 * */** * List represents an ordered, repeatable collection.
	 * Vectors are less performance than ArrayList because they are thread safe.
		 * * @Test public void Testarraylist () {/** * ArrayList is a list class based on an array implementation, which is thread insecure and requires manual assurance of the synchronization of the set.
		
		* * list<string> List = new arraylist<string> ();
		List.add ("Beijing");
		List.add ("Shanghai");
		List.add ("Tianjin");
		
		List.add ("Chongqing");
		for (String s:list) {System.out.println (s);
		
	} System.out.println (List.tostring ());
		 The @Test public void Testlinkedlist () {/** * LinkedList is very fast for sequentially accessing elements in the collection, especially inserting and deleting rows first, in the list.
		
		* * list<string> List = new linkedlist<string> ();
		List.add ("Beijing");
		List.add ("Shanghai");
		List.add ("Tianjin");
		
		List.add ("Chongqing");
		for (String s:list) {System.out.println (s);
		
	} System.out.println (List.tostring ()); The @Test public void Testvector () {/** * vector is a list class based on an array implementation and is thread-safe.
		 It is not recommended for use.

		* * list<string> List = new vector<string> ();
		List.add ("Beijing");
		List.add ("Shanghai");
		List.add ("Tianjin"); List.add ("Chongqing");
		
		for (String s:list) {System.out.println (s);
	} System.out.println (List.tostring ());
	 The/** * Set represents a unordered, not repeatable collection. * HashSet and TreeSet are two typical implementations of set.
	 However, the performance of HashSet is always better than tree, especially the common adding and querying elements.
	 * Enumset is the best performance in all set implementation classes, but it can only hold enumerated values of the same enumeration class as the collection element. 
		 * * @Test public void Testhashset () {/** * HashSet the hash algorithm to store elements in the collection, so it has good storage and lookup performance * 1. The sort order of the elements is not guaranteed, and the order is likely to change.
		 * 2.HashSet is not synchronized.
		 * 3. The value of the collection can be null.
		
		* * set<string> Set = new hashset<string> ();
		Set.add ("Beijing");
		Set.add ("Shanghai");
		Set.add ("Tianjin");
		Set.add ("Chongqing");
		
		iterator<string> i = Set.iterator ();
			while (I.hasnext ()) {String str = i.next ();
			if (Str.equals ("Chongqing")) {i.remove ();
		} for (String s:set) {System.out.println (s); @Test public void Testlinkedhashset () {/** * Linkedhashset is a subclass of HashSet, which determines where the element is stored based on the hashcode value of the element, * but It also uses a linked list to maintain the order of elements so that the elements appear to be saved in the order in which they were inserted.
		 So the performance is slightly lower than hashset.
		
		* * set<string> Set = new linkedhashset<string> (); Set.add ("Beijing");
		Set.add ("Shanghai");
		Set.add ("Tianjin");
		
		Set.add ("Chongqing");
		
		iterator<string> i = Set.iterator ();
		while (I.hasnext ()) {System.out.println (I.next ());
		 @Test public void Testtreeset () {/** * TreeSet is the implementation class of the SortedSet interface to ensure that the collection elements are in a sorted state.
		 * If you do not define comparator () when defining TreeSet, the collection calls the CompareTo of the collection element (Object o) * As opposed to the HashSet collection, and TreeSet provides several additional methods: * Object a ();
		 * Object last ();
		 * Object lower (object o);
		 * Object Higher (object o);
		 * SortedSet subset (fromelement,toelement);
		 * SortedSet Headset (toelement);
		 * SortedSet Tailset (fromelement);
				*/set<user> Set = new treeset<user> (new Comparator () {public int compare (Object o1,object O2) {
				User U1 = (user) O1;
				User U2 = (user) O2; return u1.age > U2.age? -1:u1.age < u2.age?
			1:0;
		
		}
		});
		Set.add (New User ("Hing01", 20));
		Set.add (New User ("Hing02", 25));
		Set.add (New User ("hing03", 23));
		Set.add (New User ("Hing04", 21)); Set.add (New User ("Hing05", 26));
		
		iterator<user> i = Set.iterator ();
		while (I.hasnext ()) {System.out.println (I.next ()). ToString ());
			Class user{public User (String name, int age) {this.name = name;
		This.age = age;
		} String name;
		
		int age;
		Public String toString () {return this.name + ":" +this.age; The enum season{spring,summer,fall,winter} @Test public void Testenumset () {/** * Enumset is a specially designed for enumeration classes
		 Collection class, which is stored internally in the form of a bit vector, * So the Enumset object takes up a small amount of memory and runs well.
		* * set<season> Set = Enumset.allof (Season.class);
		Output [SPRING, SUMMER, FALL, WINTER] System.out.println (set.tostring ());
		
	Creates a enumset empty set set<season> SetNull = enumset.noneof (Season.class); The/** * map<key,value> is used to hold data that has a mapping relationship.
	 Key does not allow duplicates * HashMap and Hashtable are typical implementations of the map, they do not guarantee the storage order, and the relationship between them is exactly like the one still has the list and the Vertor.
		 */@Test public void Testhashmap () {/** * HashMap is thread insecure and can use NULL as key or value. * * map<string, string> Map = new Hashmap<sTring, string> ();
		Map.put ("1", "one");
		Map.put ("2", "two");
		Map.put ("3", "three");
		
		Map.put ("4", "four");
		
	SYSTEM.OUT.PRINTLN (map);
		 @Test the public void Testlinkedhashmap () {/** * Linkedhashmap is a subclass of HashMap that guarantees a consistent order of deposit and output, so performance is slightly lower than hashmap.
		* * map<string, string> Map = new linkedhashmap<string, string> ();
		Map.put ("1", "one");
		Map.put ("2", "two");
		Map.put ("3", "three");
		
		Map.put ("4", "four");
		
	SYSTEM.OUT.PRINTLN (map);
		 @Test public void testhashtable () {/** * hashtable is thread-safe, but null can not be used as a key or value.
		* * map<string, string> Map = new hashtable<string, string> ();
		Map.put ("1", "one");
		Map.put ("2", "two");
		Map.put ("3", "three");
		
		Map.put ("4", "four");
		
	SYSTEM.OUT.PRINTLN (map); @Test public void Testtreemap () {/** * TreeMap can be analogous treeset/map<string, string> Map = new TreeMap
		<string, string> ();
		Map.put ("1", "one");
		Map.put ("2", "two");
		Map.put ("3", "three");
		
		Map.put ("4", "four"); System.out.println (map); @Test public void Testenummap () {Map<season, string> Map = new Enummap<season, string> (season.class
		
		);
		Map.put (season.spring, "Spring");
		Map.put (Season.summer, "summer Heat");
		Map.put (Season.fall, "crisp");
		
		Map.put (Season.winter, "Miles of Ice");
		
	SYSTEM.OUT.PRINTLN (map);
	 The/** * Collections is a Java-provided tool class for manipulating sets, lists, and maps.
	 * It provides a number of methods: sorting, search, replacement and synchronization control operations, * interested in their own research, here is not listed ...
 */@Test public void Testcollections () {}}

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.