Objective
In Java, we provide some simple collections like C + + generics, List,set,map and so on. Here, a brief description of how these collection containers are used, and the custom ordering of complex object elements.
first look at the frame diagram of the collection:
Since collection also inherits the iterator and comparable interfaces, we can use iterator to iterate through the elements, or we can rewrite our own sorting by customizing the CompareTo function.
List
1 Packagetestcollection;2 3 Importjava.util.ArrayList;4 ImportJava.util.Iterator;5 Importjava.util.List;6 7 Public classtestlist {8 Public Static voidMain (string[] args) {9List List =NewArrayList ();TenList.add ("Test1"); OneList.add ("Test2"); AList.add ("Test3"); - -System.out.println ("Out by for!"); the for(Object o:list) { - System.out.println (o); - } - +System.out.println ("Out by iterator!"); -Iterator Iterator =list.iterator (); + while(Iterator.hasnext ()) { AString element =(String) Iterator.next (); at System.out.println (element); - } - } -}
Run results
for! test1test2test3out by iterator! Test1test2test3
Set, if a duplicate element is encountered, does not add
1 Packagetestcollection;2 3 ImportJava.util.HashSet;4 ImportJava.util.Set;5 6 Public classTestset {7 Public Static voidMain (string[] args) {8Set set =NewHashSet ();//using set is generally used hashset, this will be faster9Set.add ("Test1");TenSet.add ("Test2"); One if(Set.add ("Test2")){ ASystem.out.println ("Add Successful"); -}Else{ -System.out.println ("Add Failed"); the } - } -}
Run results
Add failed
Map
1 Packagetestcollection;2 3 ImportJava.util.HashMap;4 ImportJava.util.Map;5 ImportJava.util.Set;6 7 Public classTestMap {8 Public Static voidMain (string[] args) {9Map map =NewHashMap ();Ten OneMap.put (1, "Test1"); AMap.put (2, "Test2"); - -SYSTEM.OUT.PRINTLN ("size" +map.size ()); theSystem.out.println (Map.get (1)); - -Set keys =Map.keyset (); - for(Object key:keys) { + System.out.println (key); - } + AMap.Remove (2); atSYSTEM.OUT.PRINTLN ("size" +map.size ()); - } -}
Run results
Size 2test11
Custom Sort Functions
Person class , inheriting comparable interface, overloading CompareTo function
1 Packagetestcollection;2 3 Public classPersonImplementscomparable{4 PrivateString name;5 Private intAge ;6 PublicString GetName () {7 returnname;8 }9 Public voidsetName (String name) {Ten This. Name =name; One } A Public intGetage () { - returnAge ; - } the Public voidSetage (intAge ) { - This. Age =Age ; - } - PublicPerson (String name,intAge ) { + This. Name =name; - This. Age =Age ; + } A @Override at Public intCompareTo (Object person)throwsclasscastexception { - if(! (PersoninstanceofPerson )) { - Throw NewClassCastException ("A person perspected!"); - } - intAge =(person ). Getage (); - return This. age-Age ; in } -}
Test class
1 Packagetestcollection;2 3 Importjava.util.Arrays;4 5 Public classtestcomparable {6 Public Static voidMain (string[] args) {7person[] Persons =NewPerson[4];8Persons[0] =NewPerson ("test1", 18);9PERSONS[1] =NewPerson ("test2", 20);TenPERSONS[2] =NewPerson ("test3", 15); OnePERSONS[3] =NewPerson ("test4", 19); A -System.out.println ("Before sorting!"); - the for(person p:persons) { -System.out.println ("Name:" +p.getname () + "Age:" +p.getage ()); - } - +System.out.println ("After sorting!"); - arrays.sort (persons); + for(person p:persons) { ASystem.out.println ("Name:" +p.getname () + "Age:" +p.getage ()); at } - } -}
Run results
Before sorting! aftersorting! 20
"Java Development Series"--Collection use method