和风细雨

世上本无难事,心以为难,斯乃真难。苟不存一难之见于心,则运用之术自出。

求集合差的几种算法

原题(这里使用了数组代替集合)

有两个数组:
String[] arr01={"Andy","Bill","Cindy","Douglas","Felex","Green"};
String[] arr02={"Andy","Bill","Felex","Green","Gates"};
求存在于arr01而不存在于arr02的元素的集合?

最容易想到的解法-双重循环

package com.junglesong;

import java.util.ArrayList;
import java.util.List;

/**
 * 利用双重循环实现的筛选
 * 
@author: sitinspring(junglesong@gmail.com)
 * @date: 2008-3-8
 
*/

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;
    }

}


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

package com.junglesong;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

/**
 * 利用哈希表进行筛选
 * 
@author: sitinspring(junglesong@gmail.com)
 * @date: 2008-3-8
 
*/

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);
        }

    }

}

 

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

 

package com.junglesong;

import java.util.ArrayList;
import java.util.List;

/**
 * 使用工具类的筛选去除
 * 
@author: sitinspring(junglesong@gmail.com)
 * @date: 2008-3-8
 
*/

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);
        }

    }

}


利用二叉树的解法 

package com.junglesong.binarytree;

import java.util.ArrayList;
import java.util.List;

/**
 * 使用二叉樹的筛选去除
 * 
@author: sitinspring(junglesong@gmail.com)
 * @date: 2008-3-8
 
*/

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);
        }

    }

}

二叉树节点类:
package com.junglesong.binarytree;

/**
 * 二叉樹節點類
 * 
@author: sitinspring(junglesong@gmail.com)
 * @date: 2008-3-8
 
*/

public class Node{
    
private String data;
    
private Node left;
    
private Node right;
    
    
public Node(String data){
        
this.data=data;
    }


    
public String getData() {
        
return data;
    }


    
public Node getLeft() {
        
return left;
    }


    
public Node getRight() {
        
return right;
    }


    
public void setData(String data) {
        
this.data = data;
    }


    
public void setLeft(Node left) {
        
this.left = left;
    }


    
public void setRight(Node right) {
        
this.right = right;
    }

}

二叉树树类:
package com.junglesong.binarytree;

/**
 * 二叉樹類
 * 
@author: sitinspring(junglesong@gmail.com)
 * @date: 2008-3-8
 
*/

public class Tree{
    
/**
     * 根節點
     
*/

    
private Node root;
    
    
/**
     * 插入一個值
     * 
@param str
     
*/

    
public void insert(String str){
        Node node
=new Node(str);
        
        
if(root==null){
            root
=node;
        }

        
else{
            Node curr
=root;
            Node parrent;
            
            
while(true){
                parrent
=curr;
                
                
if(str.compareTo(curr.getData())>0){
                    curr
=curr.getRight();
                    
                    
if(curr==null){
                        parrent.setRight(node);
                        
return;
                    }

                }

                
else{
                    curr
=curr.getLeft();
                    
                    
if(curr==null){
                        parrent.setLeft(node);
                        
return;
                    }

                }

            }

        }

    }

    
    
/**
     * 尋找一個值對應的節點
     * 
@param str
     * 
@return
     
*/

    
public Node find(String str){
        Node curr
=root;
        
        
while(curr.getData().equals(str)==false){
            
if(str.compareTo(curr.getData())>0){
                curr
=curr.getRight();
            }

            
else{
                curr
=curr.getLeft();
            }

            
            
if(curr==null){
                
return null;
            }

        }

        
        
return curr;
    }

    
    
/**
     * 輸出
     *
     
*/

    
public void printAll(){
        inorder(root);
    }

    
    
/**
     * 中序遍歷
     * 
@param node
     
*/

    
private void inorder(Node node){
        
if(node!=null){
            inorder(node.getLeft());
            
            
if(node.getData().equals("EqualMark")==false){
                System.out.println(node.getData());
            }

            inorder(node.getRight());
        }

    }

}


代码下载:
http://www.blogjava.net/Files/junglesong/RemoveAll20080308023355.rar

posted on 2008-03-08 02:25 和风细雨 阅读(713) 评论(0)  编辑  收藏 所属分类: 算法


只有注册用户登录后才能发表评论。


网站导航: