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

有兩個數組:
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. }  
import java.util.ArrayList;
import java.util.List;

/** 
 * 利用雙重循環實現的篩選
 */
public class DoubleCycling{
    public static void main(String[] args){
        String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
        String[] arr02={"Andy","Bill","Felex","Green","Gates"};
        
        // 篩選過程,注意其中異常的用途
        List<String> ls=new ArrayList<String>();
        for(String str:arr01){
            try{
                ls.add(getNotExistStr(str,arr02));
            }
            catch(Exception ex){
                continue;
            }            
        }
        
        // 取得結果
        Object[] arr03=ls.toArray();
        for(Object str:arr03){
            System.out.println(str);
        }
    }
    
    /** 
     * 查找數組Arr中是否包含str,若包含拋出異常,否則將str返回
     * @param str
     * @param arr
     * @return
     * @throws Exception
     */
    public static String getNotExistStr(String str,String[] arr) throws Exception{
        for(String temp:arr){
            if(temp.equals(str)){
                throw new Exception("");
            }
        }
        
        return str;
    }
}

 

 

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

 

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. }  
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

/** 
 * 利用哈希表進行篩選
 */
public class HashtableFilter{
    public static void main(String[] args){
        String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
        String[] arr02={"Andy","Bill","Felex","Green","Gates"};
        
        
        Map<String,String> ht=new Hashtable<String,String>();
        
        // 將arr02所有元素放入ht
        for(String str:arr02){
            ht.put(str, str);
        }
        
        // 取得在ht中不存在的arr01中的元素
        List<String> ls=new ArrayList<String>();
        for(String str:arr01){
            if(ht.containsKey(str)==false){
                ls.add(str);
            }
        }
        
        // 取得結果
        Object[] arr03=ls.toArray();
        for(Object str:arr03){
            System.out.println(str);
        }
    }
}

 

 

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

 

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. }  
import java.util.ArrayList;
import java.util.List;

/** 
 * 使用工具類的篩選去除
 */
public class Tool{
    public static void main(String[] args){
        String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
        String[] arr02={"Andy","Bill","Felex","Green","Gates"};
        
        // 直接轉的話,生成的List不支持removeAll
        List<String> ls01=new ArrayList<String>();
        for(String str:arr01){
            ls01.add(str);
        }
        
        // 同上
        List<String> ls02=new ArrayList<String>();
        for(String str:arr02){
            ls02.add(str);
        }
        
        // 去除arr01中存在於arr02中的元素
        ls01.removeAll(ls02);
        
        // 取得結果
        Object[] arr03=ls01.toArray();
        for(Object str:arr03){
            System.out.println(str);
        }
    }
}

 

利用二叉樹的解法 

 

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. }  
import java.util.ArrayList;
import java.util.List;

/** 
 * 使用二叉樹的篩選去除
 */
public class Test{
    public static void main(String[] args){
        String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
        String[] arr02={"Andy","Bill","Felex","Green","Gates"};
        
        // 以數組2為基礎創建二叉樹
        Tree tree=new Tree();        
        for(String str:arr02){
            tree.insert(str);
        }
        
        // 將在二叉樹中不存在的元素放入鏈錶
        List<String> ls=new ArrayList<String>();        
        for(String str:arr01){
            if(tree.find(str)==null){
                ls.add(str);
            }
        }
        
        // 輸出
        for(String str:ls){
            System.out.println(str);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章