簡體   English   中英

具有相交和並集的LinkedList

[英]LinkedList with intersection and union

我有兩個列表,都添加了整數。 我想將兩個列表和聯合相交。 我有我的代碼,但是由於某種原因它無法編譯。

關於為什么我不能簡單地LinkedList.intersection(argument, argument)任何建議?

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

public class IntegerSet 
{


    public static void main(String args[]) {


        LinkedList<Double> list1 = new LinkedList<Double>();
        list1.add((double) 5);
        list1.add((double) 15);
        list1.add((double) 3);
        list1.add((double) 15);


        LinkedList<Double> list2 = new LinkedList<Double>();
        list2.add((double) 15);
        list2.add((double) 8);
        list2.add((double) 16);
        list2.add((double) 11);

        // Calculating Intersection of two Set in Java
        LinkedList<Double> intersection = LinkedList.intersection(list1, list2);
        System.out.printf("Intersection of two Set %s and %s in Java is %s %n",
                list1.toString(), list2.toString(), intersection.toString());
        System.err.println("Number of elements common in two Set : "
                           + intersection.size());

        // Calculating Union of two Set in Java
        LinkedList<Double> union = LinkedList.union(list1, list2);
        System.out.printf("Union of two Set %s and %s in Java is %s %n",
                list1.toString(), list2.toString(), union.toString());
        System.out.println("total number of element in union of two Set is : "
                            + union.size());
    }

}

除了要在linked list上使用intersection和並union方法之外,就復雜性而言,這是最糟糕的事情之一。 也,

// Calculating Union of two Set in Java
LinkedList<Double> union = LinkedList.union(list1, list2);

SetList API中還存在以下內容:

col.retainAll(otherCol) // for intersection
col.addAll(otherCol) // for union

另請參閱這篇文章: Java中ArrayList的交集和並集

第三方庫: Apache Commons CollectionUtils

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM