如何从集合中删除长度为 5 的字符串?

发布于 2024-12-19 10:18:03 字数 658 浏览 1 评论 0原文

我想从集合中删除长度为 5 的字符串,但它一直输出集合本身。

public void remove5()
{
Set<String> newSet = new HashSet<String>();
newSet.add("hello");
newSet.add("my");
newSet.add("name");
newSet.add("is");
newSet.add("nonsense");
for(String word: newSet)
{
    if(word.length()==5)
    {
        newSet.remove(word); // Doesn't Help - throws an error Exception in thread "main" java.util.ConcurrentModificationException
    }
}
    System.out.println(newSet);
}

我希望输出为:

my
name
is
nonsense

(hello 被删除,因为它有 5 个字符)

但我每次都会得到这个:

hello
my
name
is 
nonsense

你能帮忙吗?

I want to remove strings of length 5 from a set, but it keeps outputting the set itself.

public void remove5()
{
Set<String> newSet = new HashSet<String>();
newSet.add("hello");
newSet.add("my");
newSet.add("name");
newSet.add("is");
newSet.add("nonsense");
for(String word: newSet)
{
    if(word.length()==5)
    {
        newSet.remove(word); // Doesn't Help - throws an error Exception in thread "main" java.util.ConcurrentModificationException
    }
}
    System.out.println(newSet);
}

I want the output to be:

my
name
is
nonsense

(hello was removed because it's 5 characters)

But I get this everytime:

hello
my
name
is 
nonsense

Can you please help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

看春风乍起 2024-12-26 10:18:03
Iterator<String> it= newStr.iterator();
while(it.hasNext()) { // iterate
   String word = it.next();
   if(word.length() == 5) { // predicate
      it.remove();  // remove from set through iterator - action
   }
}
Iterator<String> it= newStr.iterator();
while(it.hasNext()) { // iterate
   String word = it.next();
   if(word.length() == 5) { // predicate
      it.remove();  // remove from set through iterator - action
   }
}
猫腻 2024-12-26 10:18:03

为了实际修改你的集合,你需要做这样的事情:

Iterator<String> iter = newSet.iterator();
while (iter.hasNext())
    if (iter.next().length() == 5)
        iter.remove();

由于字符串是不可变的,你不能修改已经添加到集合中的字符串,无论如何,即使你可以就地修改它们,将它们替换为"" 不会将它们从集合中删除。

For actually modifying your set, you need to do something like this:

Iterator<String> iter = newSet.iterator();
while (iter.hasNext())
    if (iter.next().length() == 5)
        iter.remove();

Since Strings are immutable, you can't modify the ones that were already added to the set, and anyway, even if you could modify them in-place, replacing them by "" would not remove them from the set.

红焚 2024-12-26 10:18:03

正如其他人所建议的,您无法更改字符串的原因是,代码片段:

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class TestString {
public void remove5() {
    Set<String> newSet = new HashSet<String>();
    newSet.add("hello");
    newSet.add("my");
    newSet.add("name");
    newSet.add("is");
    newSet.add("nonsense");
    for (Iterator<String> iter = newSet.iterator(); iter.hasNext();) {
        if (iter.next().length() == 5) {
            iter.remove();
        }
    }
    System.out.println(newSet);
}

public static void main(String[] args) {
    new TestString().remove5();
}
}

如果您迭代集合并在循环中删除对象,它将抛出 ConcurrentModificationException 作为 HastSet迭代器是一个快速失败迭代器。

As other suggested you cannot change a String reason being, Code snippet:

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class TestString {
public void remove5() {
    Set<String> newSet = new HashSet<String>();
    newSet.add("hello");
    newSet.add("my");
    newSet.add("name");
    newSet.add("is");
    newSet.add("nonsense");
    for (Iterator<String> iter = newSet.iterator(); iter.hasNext();) {
        if (iter.next().length() == 5) {
            iter.remove();
        }
    }
    System.out.println(newSet);
}

public static void main(String[] args) {
    new TestString().remove5();
}
}

If you iterate over the set and in the loop you remove the object, it will throw you ConcurrentModificationExceptionas HastSet iterator is a fail fast Iterator.

蓝海似她心 2024-12-26 10:18:03

当您找到长度为 5 的字符串时,需要将其从集合中删除:

newSet.remove(word);

事实上,您似乎正在尝试将 word 更改为空字符串,但字符串是不可变的。您的调用实际上所做的是返回一个空字符串。

When you find a string of length 5, you need to remove it from the set:

newSet.remove(word);

As it is, you appear to be trying to change word to an empty string, but strings are immutable. What your call actually does is return an empty string.

迷你仙 2024-12-26 10:18:03

字符串是不可变,对字符串word或任何其他字符串所做的更改不会反映在Set的字符串中

if(word.length()==5)
    {
        word.replaceAll(word, "");
        newSet.remove(word);
    }

您可以参考此函数HashSet

remove(Object o) 

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/HashSet.html

Strings are immutable, changes made to the String word or any other String will not reflect in the string of Set

add

if(word.length()==5)
    {
        word.replaceAll(word, "");
        newSet.remove(word);
    }

you can refer to this function of HashSet

remove(Object o) 

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/HashSet.html

简单爱 2024-12-26 10:18:03

字符串在 Java 中是不可变的,这意味着当您调用 word.replaceAll(word,"") 时,它会返回字符串“”(您没有将其分配给任何内容)。 word 不会更改,并且 Set 仍指向 word 的旧值。您需要从集合本身中删除单词

Strings are immutable in Java, that means when you call word.replaceAll(word,""), it returns the String "" (which you aren't assigning to anything). The word doesn't change and the Set is still pointing to the old value of word. You need to remove word from the Set itself.

两人的回忆 2024-12-26 10:18:03
int i = 0;
Set<String> newSet = new HashSet<String>();
newSet.add("hello");
newSet.add("my");
newSet.add("name");
newSet.add("is");
newSet.add("nonsense");
for(String word: newSet)
{
   if(word.length()==5)
   {
       newSet.remove(i);
   }
   i++;
}
int i = 0;
Set<String> newSet = new HashSet<String>();
newSet.add("hello");
newSet.add("my");
newSet.add("name");
newSet.add("is");
newSet.add("nonsense");
for(String word: newSet)
{
   if(word.length()==5)
   {
       newSet.remove(i);
   }
   i++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文