【java開發系列】—— 集合使用方法

前言java

  在java中提供給咱們了一些相似C++泛型的簡單集合,list,set,map等。這裏,簡單介紹一下這些集合容器的使用方法,以及複雜對象元素的自定義排序。框架

  首先看一下集合的框架圖:

  因爲collection也繼承了Iterator和comparable接口,所以咱們能夠使用Iterator來遍歷元素,也能夠經過自定義compareTo函數來從新編寫本身的排序。ide

  List

 1 package testCollection;  2 
 3 import java.util.ArrayList;  4 import java.util.Iterator;  5 import java.util.List;  6 
 7 public class testList {  8     public static void main(String[] args){  9         List list = new ArrayList(); 10         list.add("test1"); 11         list.add("test2"); 12         list.add("test3"); 13         
14         System.out.println("out by for!"); 15         for(Object o : list){ 16  System.out.println(o); 17  } 18         
19         System.out.println("out by iterator!"); 20         Iterator iterator = list.iterator(); 21         while(iterator.hasNext()){ 22             String element = (String)iterator.next(); 23  System.out.println(element); 24  } 25  } 26 }

運行結果函數

out by for! test1 test2 test3 out by iterator! test1 test2 test3

Set,若是遇到重複的元素,不會添加

 1 package testCollection;  2 
 3 import java.util.HashSet;  4 import java.util.Set;  5 
 6 public class testSet {  7     public static void main(String[] args){  8         Set set = new HashSet();//使用set通常都是用hashset,這個會快一些
 9         set.add("test1"); 10         set.add("test2"); 11         if(set.add("test2")){ 12             System.out.println("add successful"); 13         }else{ 14             System.out.println("add failed"); 15  } 16  } 17 }

運行結果測試

add failed

Map

 1 package testCollection;  2 
 3 import java.util.HashMap;  4 import java.util.Map;  5 import java.util.Set;  6 
 7 public class testMap {  8     public static void main(String[] args){  9         Map map = new HashMap(); 10         
11         map.put(1, "test1"); 12         map.put(2, "test2"); 13         
14         System.out.println("size "+map.size()); 15         System.out.println(map.get(1)); 16         
17         Set keys = map.keySet(); 18         for(Object key : keys){ 19  System.out.println(key); 20  } 21         
22         map.remove(2); 23         System.out.println("size "+map.size()); 24  } 25 }

運行結果this

size 2 test1 1
2 size 1

自定義排序函數

person類,繼承Comparable接口,重載compareTo函數spa

 1 package testCollection;  2 
 3 public class Person implements Comparable{  4     private String name;  5     private int age;  6     public String getName() {  7         return name;  8  }  9     public void setName(String name) { 10         this.name = name; 11  } 12     public int getAge() { 13         return age; 14  } 15     public void setAge(int age) { 16         this.age = age; 17  } 18     public Person(String name,int age){ 19         this.name = name; 20         this.age = age; 21  } 22  @Override 23     public int compareTo(Object person) throws ClassCastException { 24         if(!(person instanceof Person)){ 25             throw new ClassCastException("A Person perspected!"); 26  } 27         int age = ((Person)person).getAge(); 28         return this.age-age; 29  } 30 }

測試類code

 1 package testCollection;  2 
 3 import java.util.Arrays;  4 
 5 public class testComparable {  6     public static void main(String[] args){  7         Person[] persons = new Person[4];  8         persons[0] = new Person("test1",18);  9         persons[1] = new Person("test2",20); 10         persons[2] = new Person("test3",15); 11         persons[3] = new Person("test4",19); 12         
13         System.out.println("before sorting!"); 14         
15         for(Person p : persons){ 16             System.out.println("name: "+p.getName()+" age: "+p.getAge()); 17  } 18         
19         System.out.println("after sorting!"); 20  Arrays.sort(persons); 21         for(Person p : persons){ 22             System.out.println("name: "+p.getName()+" age: "+p.getAge()); 23  } 24  } 25 }

運行結果對象

before sorting! name: test1 age: 18 name: test2 age: 20 name: test3 age: 15 name: test4 age: 19 after sorting! name: test3 age: 15 name: test1 age: 18 name: test4 age: 19 name: test2 age: 20
相關文章
相關標籤/搜索