Java Set与List集合区别


分别向Set集合和List集合中添加"A","a","c","C","a"5个元素, 观察重复的a值能否在List或者Set中成功添加。

[java]

  1. package com.han;  
  2.   
  3. import java.util.*;  
  4.   
  5. /** 
  6.  * 分别向Set集合和List集合中添加"A","a","c","C","a"5个元素, 
  7.  * 观察重复的a值能否在List或者Set中成功添加。 
  8.  * @author han 
  9.  * 
  10.  */  
  11. public class SetVsList {  
  12.   
  13.     @SuppressWarnings("unchecked")  
  14.     public static void main(String[] args) {  
  15.         // TODO Auto-generated method stub   
  16.         @SuppressWarnings("rawtypes")  
  17.         TreeSet treeset=new TreeSet();  
  18.         treeset.add("A");  
  19.         treeset.add("a");  
  20.         treeset.add("c");  
  21.         treeset.add("C");  
  22.         treeset.add("a");  
  23.         @SuppressWarnings("rawtypes")  
  24.         List list=new ArrayList();  
  25.         list.add("A");  
  26.         list.add("a");  
  27.         list.add("c");  
  28.         list.add("C");  
  29.         list.add("a");  
  30.         @SuppressWarnings("rawtypes")  
  31.         Iterator it=treeset.iterator();  
  32.         System.out.println("Set集合中所有的元素:");  
  33.         while (it.hasNext()){  
  34.             System.out.println(it.next());  
  35.         }  
  36.         @SuppressWarnings("rawtypes")  
  37.         Iterator it2=list.iterator();  
  38.         System.out.println("List集合中所有的元素:");  
  39.         while (it2.hasNext()){  
  40.             System.out.println(it2.next());  
  41.         }  
  42.           
  43.     }  
  44.   
  45. }  

相关内容