java 如何从列表中排除某些元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6075894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:12:34  来源:igfitidea点击:

How to exclude certain elements from a List

java

提问by Joe

I have a List with the following elements.

我有一个包含以下元素的列表。

List<String> numbers = new ArrayList<String>();
numbers.add("1");
numbers.add("2");
numbers.add("3");

How do I get a subset of the List without say "1"? Is there a simpler function which will work against a List of any type.

如何在不说的情况下获得 List 的子集"1"?是否有一个更简单的函数可以处理任何类型的 List。

回答by Robin Green

The question is ambiguous.

这个问题是模棱两可的。

numbers.remove("1");will remove the first instance of "1" in any list. If you might have duplicate elements, use numbers.removeAll (Collections.singletonList ("1"));to remove all of them.

numbers.remove("1");将删除任何列表中“1”的第一个实例。如果您可能有重复的元素,请使用numbers.removeAll (Collections.singletonList ("1"));删除所有元素。

If on the other hand you want to get a subrange of the list, use the subListmethod.

另一方面,如果您想获得列表的子范围,请使用该subList方法。

回答by ColinD

You could use Guavato create a filtered Collectionview:

您可以使用Guava来创建过滤Collection视图:

Collection<String> filtered = Collections2.filter(numbers,
    Predicates.not(Predicates.equalTo("1")));

That's a live view of the original List, so it's very efficient to create but might not be efficient to use depending on how you need to use it and how small the filtered collection is compared to the original. You can copy it to a new list if that will work better:

这是原始视图的实时视图List,因此创建非常有效,但使用效率可能不高,具体取决于您需要如何使用它以及过滤后的集合与原始集合相比有多小。如果效果更好,您可以将其复制到新列表中:

List<String> filteredList = Lists.newArrayList(filtered);

The simplest way to create a copy list containing all elements except the one you don't want is to use a simple forloop:

创建包含除您不想要的元素之外的所有元素的副本列表的最简单方法是使用简单for循环:

List<String> copy = new ArrayList<String>();
for (String number : numbers) {
  if (!"1".equals(number))
    copy.add(number);
}

If you want to modify the original list instead, numbers.removeAllis what you want (as mentioned by @Robin Green).

如果您想修改原始列表,numbers.removeAll这就是您想要的(如@Robin Green 所述)。

回答by Premraj

you can take a look at Guava librarieswhich has a filter method on any collection type.
here are some examplesfor your reference.

If you don't want to use third party libraries, you have to manually iterate the list and need to copy the elements which doesn't match your criteria.

您可以查看Guava 库,它对任何集合类型都有过滤器方法。
这里有一些例子供您参考。

如果您不想使用第三方库,则必须手动迭代列表并需要复制与您的条件不匹配的元素。

回答by kirilv

I think this could help you out:

我认为这可以帮助你:

final List<String> numbers = Arrays.asList("1", "2", "3");

final List<String> filtered = numbers.stream()
                                     .filter(num -> !"1".equals(num))
                                     .collect(Collectors.toList());

回答by Kaj

List.subList(int fromIndex, int toIndex)

List.subList(int fromIndex, int toIndex)

Edit: Possible that I didn't understand the question.

编辑:可能我不明白这个问题。

@Op. Do you want to remove items of a certain value, or do you want to have a view between two indices?

@Op。您是要删除具有特定值的项目,还是要在两个索引之间查看?

回答by Ry-

Yes, for example to remove the 1(s) from the list you could do:

是的,例如1要从列表中删除(s),您可以执行以下操作:

int i = 0;
while((i = numbers.indexOf("1")) > -1) {
     numbers.remove(i);
}

You can do this with a list of objects, too, or a filtering criterion.

您也可以使用对象列表或过滤条件来执行此操作。

Edit: There will only be one and not many? Then just do:

编辑:只有一个而不是很多?然后就这样做:

int i = 0;
if((i = numbers.indexOf("1")) > -1) {
     numbers.remove(i);
}