Java 如何获取未选定元素的列表?

Java 如何获取未选定元素的列表?,java,list,Java,List,我有两个数组列表,即readAllNames,另一个是selectedNames。现在我想得到另一个列表,其中包含未选择的名称 List<Name>readAllNames = {"a","b","c","d","e","f","g"}; List<Name>selectedNames = {"a","b"}; ListreadAllNames={“a”、“b”、“c”、“d”、“e”、“f”、“g”}; ListselectedNames={“a”,“b”}; 如何

我有两个数组列表,即
readAllNames
,另一个是
selectedNames
。现在我想得到另一个列表,其中包含
未选择的名称

List<Name>readAllNames = {"a","b","c","d","e","f","g"};
List<Name>selectedNames = {"a","b"};
ListreadAllNames={“a”、“b”、“c”、“d”、“e”、“f”、“g”};
ListselectedNames={“a”,“b”};
如何从
readAllNames
获取:
未选择的名称
{“c”、“d”、“e”、“f”、“g”}


如果不使用
remove()
removeAll()
为什么不查看
remove()
的源代码并使用它呢?

如果您不想使用
remove()
removeAll(),为什么不查看
remove()的源代码并使用它呢?

您始终可以遍历
readAllNames
并检查每个元素是否在
selectedNames
中添加了
contains()
,如果没有,则将其添加到
unselectedNames

但是您确实应该使用
removeAll()
;向我们展示您的
名称
类,我们可能会告诉您出了什么问题(提示:请阅读
equals()
hashCode()
semantics wrt.
集合
操作。)


干杯,

如果您不想使用
remove()
/
removeAll()
您可以始终迭代
readAllNames
并检查每个元素是否
包含()
,如果没有,则将其添加到
未选择的名称中

List<String> unread = new ArrayList<String>();
for(String s : readAllNames ){
    if(!selectedNames.contains(s))
        unread.add(s);
}
但是您确实应该使用
removeAll()
;向我们展示您的
名称
类,我们可能会告诉您出了什么问题(提示:请阅读
equals()
hashCode()
semantics wrt.
集合
操作。)

干杯,

列表未读=新建ArrayList();
List<String> unread = new ArrayList<String>();
for(String s : readAllNames ){
    if(!selectedNames.contains(s))
        unread.add(s);
}
for(字符串s:ReadAllName){ 如果(!selectedNames.contains)) 未读。添加; }
列表未读=新建ArrayList();
for(字符串s:ReadAllName){
如果(!selectedNames.contains))
未读。添加;
}
ArrayList unselectedName=new ArrayList();
对于(名称s:readAllNames){
如果(!selectedNames.contains)){
未选择的名称。添加;
}
}
ArrayList unselectedName=new ArrayList();
对于(名称s:readAllNames){
如果(!selectedNames.contains)){
未选择的名称。添加;
}
}
试试这个:

List<String> unSelectedNames = new ArrayList<String>(readAllNames);
unSelectedNames.removeAll(selectedNames);
List unSelectedNames=newarraylist(readAllNames);
取消选择名称。删除所有(选择名称);
您不必触摸原始的
列表,只需触摸该列表的副本。

尝试以下操作:

List<String> unSelectedNames = new ArrayList<String>(readAllNames);
unSelectedNames.removeAll(selectedNames);
List unSelectedNames=newarraylist(readAllNames);
取消选择名称。删除所有(选择名称);
您不必触摸原始的
列表,只需触摸该列表的副本。

试试这个

List<Name>readAllNames = {"a","b","c","d","e","f","g"};
List<Name>selectedNames = {"a","b"};
List<Name>unselectedNames = null;
Collections.copy(unselectedNames , readAllNames);
unselectedNames.removeAll(selectedNames);
ListreadAllNames={“a”、“b”、“c”、“d”、“e”、“f”、“g”};
ListselectedNames={“a”,“b”};
ListunselectedNames=null;
Collections.copy(未选择的名称、ReadAllName);
取消选择名称。删除所有(选择名称);
试试这个

List<Name>readAllNames = {"a","b","c","d","e","f","g"};
List<Name>selectedNames = {"a","b"};
List<Name>unselectedNames = null;
Collections.copy(unselectedNames , readAllNames);
unselectedNames.removeAll(selectedNames);
ListreadAllNames={“a”、“b”、“c”、“d”、“e”、“f”、“g”};
ListselectedNames={“a”,“b”};
ListunselectedNames=null;
Collections.copy(未选择的名称、ReadAllName);
取消选择名称。删除所有(选择名称);

为什么
不使用remove()和removeAll()
?尝试使用readAllNames.indexOf(对象);object-所选名称中的元素。如果remove和removeAll不能正常工作,可能是因为您的Name类没有正确实现equals/hashcode。在这里,如果selectedNames具有{“a”,“e”},则表示如何使用index…为什么不使用remove()和removeAll()
?请尝试使用readAllNames.indexOf(对象);object-选定名称中的元素。如果remove和removeAll不能正常工作,可能是因为您的Name类没有正确实现equals/hashcode。在这里,如果selectedNames具有{“a”,“e”},则表示如何使用index…bcz'@PostConstruct'方法remove()和removeAll()工作不正常。但是这些在junit测试中工作得很好。我不知道原因。对不起,我不理解你的回答。remove()肯定会执行您希望它执行的操作。如果它不起作用,则是您的数据或参数有问题。这可能是因为您的Name类没有正确实现equals/hashcode方法。bcz'@PostConstruct'方法remove()和removeAll()工作不正常。但是这些在junit测试中工作得很好。我不知道原因。对不起,我不理解你的回答。remove()肯定会执行您希望它执行的操作。如果它不起作用,则是您的数据或参数出现问题。这可能是因为您的Name类未正确实现equals/hashcode方法。但必须同时执行重写,但也必须执行重写