取差集

作者: zcwfeng | 来源:发表于2017-07-27 17:40 被阅读4次

public static void main(String[] args) {

    String t = "test.dict";
    String rpStr = t.replaceAll("\\.dict","");
    System.out.println(rpStr);


    List<String> firstArrayList = new ArrayList<String>();
    List<String> secondArrayList = new ArrayList<String>();
    List<String> defectList = new ArrayList<String>();//差集List

    secondArrayList.add("main_in");
    secondArrayList.add("main_en_gb");
    secondArrayList.add("main_en.dict");
    secondArrayList.add("main_de");
    firstArrayList.add("main_en.dict");

    // 获取差集
    defectList = receiveDefectList(firstArrayList, secondArrayList);




    Iterator<String> defectIterator = defectList.iterator();
    System.out.println("===================差集===================");
    while(defectIterator.hasNext()) {
        System.out.println(defectIterator.next());
    }
}



/**
 * @方法描述:获取两个ArrayList的差集
 * @param firstArrayList 第一个ArrayList
 * @param secondArrayList 第二个ArrayList
 * @return resultList 差集ArrayList
 */
public static List<String> receiveDefectList(List<String> firstArrayList, List<String> secondArrayList) {
    List<String> resultList = new ArrayList<String>();
    LinkedList<String> result = new LinkedList<String>(firstArrayList);// 大集合用linkedlist
    HashSet<String> othHash = new HashSet<String>(secondArrayList);// 小集合用hashset
    Iterator<String> iter = result.iterator();// 采用Iterator迭代器进行数据的操作
    while(iter.hasNext()){
        if(othHash.contains(iter.next())){
            iter.remove();
        }
    }
    resultList = new ArrayList<String>(result);
    return resultList;
}

相关文章

网友评论

    本文标题:取差集

    本文链接:https://www.haomeiwen.com/subject/gcftlxtx.html