Package com.xian.test;
Import java.util.ArrayList;
Import java.util.Enumeration;
Import Java.util.HashMap;
Import Java.util.HashSet;
Import java.util.Hashtable;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Set;
Import Java.util.TreeSet;
public class Javalist {
public static void Main (string[] args) throws Exception {
GetList ();
Gethash ();
Getset ();
}
/**
* ArrayList high random access efficiency, thread unsafe, repeatable, according to subscript desirable values the construction parameters of the source code is 10, the initial capacity of 10, each increase of 50%
* Add data to the front or middle inefficient, linkedlist linked list in sequential access, before or in the middle to add deleted data faster.
* Vector thread safety.
* @author Jia Xiaoxian
* @serialData 2015-4-16 10:37
*/
public static void GetList () {
List arraylist=new arrayList ();
arraylist.add ("Jia Xiaoxian");
arraylist.add ("Xiao Qing");
System.out.println (arraylist.get (0) + "| | "+arraylist.get (1));
arraylist.add (0, "fuck You");
arraylist.set (1, "My name Disappears");
System.out.println (arraylist.get (0) + "| | "+arraylist.get (1) +" | | "+arraylist.get (2));
System.out.println (Arraylist.contains ("Xiao Qing"));//Judging whether there is
}
/**
* HashMap thread is unsafe, allow key and value to be null, store out of order. ContainsKey, Containsvalue, Iterator iteration.
* Hashtable thread safe, does not allow key and value to be null, unordered storage, Contains,containskey, Containsvalue, enumeration iterations.
* (synchronized)
* @throws Exception
* @author Jia Xiaoxian
*/
public static void Gethash () throws Exception {
HashMap map=new HashMap ();
map.put ("You", "Xiao Qing");
map.put ("Me", "Jia Xiaoxian");
System.out.println (Map.get ("You") + "| | "+map.get (" Me ");
if (Map.put ("Me", "error")!=null)
System.out.println ("No repetition Allowed");
//throw New Exception ("Not allowed to repeat");
map.put ("null", "null");
System.out.println (Map.get ("null"));
if (Map.containskey ("Me"))
System.out.println ("Me" key is present.) ");
Set set=map.entryset (); The iterator iteration must be set so it is returned to set and then iterated.
Iterator iter=set.iterator ();
while (Iter.hasnext ()) {
System.out.println ("iterator Iteration:" +iter.next ());
}
Hashtable table=new Hashtable ();
table.put ("You", "Xiao Qing");
table.put ("Me", "Jia Xiaoxian");
table.put ("null", "null");//The null of the string is OK. This null will appear nullpointexception
Enumeration e=table.elements ();
while (e.hasmoreelements ()) {//can be seen as unordered.
Object next=e.nextelement ();
System.out.println ("Parameters of the iteration:" +next);
}
}
/**
* Set set has a feature. is the non-repetition within the set. So you can use set to do some heavy operation. Very convenient.
* Set iteration using iterator
* HashSet disorderly storage, treeset orderly storage.
*/
public static void Getset () {
Set set=new HashSet ();
set.add ("Jia Xiaoxian");
set.add ("Xiao Qing");
if (Set.add ("Xiao Qing"))
System.out.println ("no repetition. ");
TreeSet treeset=new TreeSet ();
Treeset.add ("Me");
Treeset.add ("You");
Iterator iter=treeset.iterator ();
while (Iter.hasnext ())//Ordered
System.out.println ("TreeSet's iterator Iteration" +iter.next ());
}
}
This article is from the "Jia Xiaoxian" blog, make sure to keep this source http://hackerxian.blog.51cto.com/9240575/1637446
Common collection classes for Java