Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java比较两种列表模型_Java_Arrays_Jlist_Defaultlistmodel - Fatal编程技术网

Java比较两种列表模型

Java比较两种列表模型,java,arrays,jlist,defaultlistmodel,Java,Arrays,Jlist,Defaultlistmodel,我想问一下,是否有任何方法可以区分两个DefaultListModel的包含元素。例如:有两个模型,第一个模型包含a、b、c、d,第二个模型包含a、b,因此我想找到一种方法来比较这两个模型并在数组中返回“d”和“c”。谢谢。您必须构建两个列表的交集,然后从并集中“减去”它: // consider m1 and m2 your two DefaultListModels: DefaultListModel m1 = ... ; DefaultListModel m2 = ... ; //

我想问一下,是否有任何方法可以区分两个DefaultListModel的包含元素。例如:有两个模型,第一个模型包含a、b、c、d,第二个模型包含a、b,因此我想找到一种方法来比较这两个模型并在数组中返回“d”和“c”。谢谢。

您必须构建两个列表的交集,然后从并集中“减去”它:

// consider m1 and m2 your two DefaultListModels:
DefaultListModel m1 = ... ;  
DefaultListModel m2 = ... ;

// retrieve the elements
List<?> elements1 = Arrays.asList(m1.toArray());
List<?> elements2 = Arrays.asList(m2.toArray());

// build the union set
Set<Object> unionSet = new HashSet<Object>();
unionSet.addAll(elements1);
unionSet.addAll(elements2);

// build the intersection and subtract it from the union
elements1.retainAll(elements2);
unionSet.removeAll(elements1);

// unionSet now holds the elements that are only present 
// in elements1 or elements2 (but not in both)
<代码> //考虑M1和M2的两个Debug表模型: DefaultListModelM1=; DefaultListModelM2=; //检索元素 List elements1=Arrays.asList(m1.toArray()); List elements2=Arrays.asList(m2.toArray()); //构建联合集 Set unionSet=新的HashSet(); unionSet.addAll(元素1); unionSet.addAll(elements2); //构建交点并将其从并集中减去 要素1.保留(要素2); unionSet.removeAll(元素1); //unionSet现在保存仅存在的元素 //在元素s1或元素s2中(但不是在两者中)
考虑使用
设置
,它不允许重复。。使用一些技巧你可以达到你想要的。