求兩個集合差的幾種常見算法

有兩個數組:
String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
String[] arr02={"Andy","Bill","Felex","Green","Gates"};
求存在於arr01而不存在於arr02的元素的集合?

 

最容易想到的解法-雙重循環

Java代碼  收藏代碼
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.   
  4. /**  
  5.  * 利用雙重循環實現的篩選 
  6.  */  
  7. public class DoubleCycling{  
  8.     public static void main(String[] args){  
  9.         String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};  
  10.         String[] arr02={"Andy","Bill","Felex","Green","Gates"};  
  11.           
  12.         // 篩選過程,注意其中異常的用途  
  13.         List<String> ls=new ArrayList<String>();  
  14.         for(String str:arr01){  
  15.             try{  
  16.                 ls.add(getNotExistStr(str,arr02));  
  17.             }  
  18.             catch(Exception ex){  
  19.                 continue;  
  20.             }              
  21.         }  
  22.           
  23.         // 取得結果  
  24.         Object[] arr03=ls.toArray();  
  25.         for(Object str:arr03){  
  26.             System.out.println(str);  
  27.         }  
  28.     }  
  29.       
  30.     /**  
  31.      * 查找數組Arr中是否包含str,若包含拋出異常,否則將str返回 
  32.      * @param str 
  33.      * @param arr 
  34.      * @return 
  35.      * @throws Exception 
  36.      */  
  37.     public static String getNotExistStr(String str,String[] arr) throws Exception{  
  38.         for(String temp:arr){  
  39.             if(temp.equals(str)){  
  40.                 throw new Exception("");  
  41.             }  
  42.         }  
  43.           
  44.         return str;  
  45.     }  
  46. }  

 

 

速度較高的解法-利用哈希表

 

Java代碼  收藏代碼
  1. import java.util.ArrayList;  
  2. import java.util.Collection;  
  3. import java.util.Hashtable;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. /**  
  8.  * 利用哈希表進行篩選 
  9.  */  
  10. public class HashtableFilter{  
  11.     public static void main(String[] args){  
  12.         String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};  
  13.         String[] arr02={"Andy","Bill","Felex","Green","Gates"};  
  14.           
  15.           
  16.         Map<String,String> ht=new Hashtable<String,String>();  
  17.           
  18.         // 將arr02所有元素放入ht  
  19.         for(String str:arr02){  
  20.             ht.put(str, str);  
  21.         }  
  22.           
  23.         // 取得在ht中不存在的arr01中的元素  
  24.         List<String> ls=new ArrayList<String>();  
  25.         for(String str:arr01){  
  26.             if(ht.containsKey(str)==false){  
  27.                 ls.add(str);  
  28.             }  
  29.         }  
  30.           
  31.         // 取得結果  
  32.         Object[] arr03=ls.toArray();  
  33.         for(Object str:arr03){  
  34.             System.out.println(str);  
  35.         }  
  36.     }  
  37. }  

 

 

最方便的解法-利用工具類

 

Java代碼  收藏代碼
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.   
  4. /**  
  5.  * 使用工具類的篩選去除 
  6.  */  
  7. public class Tool{  
  8.     public static void main(String[] args){  
  9.         String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};  
  10.         String[] arr02={"Andy","Bill","Felex","Green","Gates"};  
  11.           
  12.         // 直接轉的話,生成的List不支持removeAll  
  13.         List<String> ls01=new ArrayList<String>();  
  14.         for(String str:arr01){  
  15.             ls01.add(str);  
  16.         }  
  17.           
  18.         // 同上  
  19.         List<String> ls02=new ArrayList<String>();  
  20.         for(String str:arr02){  
  21.             ls02.add(str);  
  22.         }  
  23.           
  24.         // 去除arr01中存在於arr02中的元素  
  25.         ls01.removeAll(ls02);  
  26.           
  27.         // 取得結果  
  28.         Object[] arr03=ls01.toArray();  
  29.         for(Object str:arr03){  
  30.             System.out.println(str);  
  31.         }  
  32.     }  
  33. }  

 

利用二叉樹的解法 

 

Java代碼  收藏代碼
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3.   
  4. /**  
  5.  * 使用二叉樹的篩選去除 
  6.  */  
  7. public class Test{  
  8.     public static void main(String[] args){  
  9.         String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};  
  10.         String[] arr02={"Andy","Bill","Felex","Green","Gates"};  
  11.           
  12.         // 以數組2為基礎創建二叉樹  
  13.         Tree tree=new Tree();          
  14.         for(String str:arr02){  
  15.             tree.insert(str);  
  16.         }  
  17.           
  18.         // 將在二叉樹中不存在的元素放入鏈錶  
  19.         List<String> ls=new ArrayList<String>();          
  20.         for(String str:arr01){  
  21.             if(tree.find(str)==null){  
  22.                 ls.add(str);  
  23.             }  
  24.         }  
  25.           
  26.         // 輸出  
  27.         for(String str:ls){  
  28.             System.out.println(str);  
  29.         }  
  30.     }  
  31. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章