在 ArrayList 中替换字符串

huangapple go评论78阅读模式
英文:

Replace string in ArrayList

问题

我正在尝试在一个列的ArrayList中替换一个字符串。在表达式中出现了错误

> lambda表达式中的返回类型错误:无法将String转换为Articles

articlesArrayList.replaceAll(articles -> articles.getAuthor().replace("."," "));
英文:

I am string to replace a string in a column ArrayList. I am getting an error on the expression

> Bad return type in lambda expression: String cannot be converted to Articles

articlesArrayList.replaceAll(articles -> articles.getAuthor().replace("."," "));

答案1

得分: 1

replaceAll 方法试图返回一组文章的数组。你的函数 map 却返回了一个字符串。如果你想要替换每个作者中的 '.' 字符为 ' ',请改用 ArrayList.forEach 方法。

英文:

The replaceAll method is trying to return an array of articles. Your function map is returning a string instead. If you want to replace the '.' character with ' ' for each author, use the ArrayList.forEach method instead.

答案2

得分: 0

class Main {
    public static void main(String[] args) {
        new Main().replace();
    }

    public void replace() {
        List<Article> articlesArrayList = new ArrayList<>();
        articlesArrayList.add(Article.builder().author("Sagar.Gangwal").build());
        articlesArrayList.stream().forEach(article -> article.getAuthor().replaceAll(".", " "));

        articlesArrayList = articlesArrayList.stream()
                .map(a -> this.update(a)).collect(Collectors.toList());
        System.out.println(articlesArrayList.get(0));
    }

    private static Article update(Article a) {
        a.author = a.getAuthor().replaceAll("\\.", " ");
        return a;
    }
}

You can see above code.

Here i am trying to create stream and then foreach element of that stream i want to update AuthorName with ' ' to space.

articlesArrayList.stream().map(a -> this.update(a)).collect(Collectors.toList());

You need to use stream API and then custom update method to update value.

Also for replacing '.', you need to use escape character as well. replaceAll method of String class contains first argument as regular expression.

If you see this , i am escaping '.' with '\\.'.
a.getAuthor().replaceAll("\\.", " ");

So instead of replaceAll, you can go with replace method which simply take first argument as simply replace one character with passed target character.

You can see difference between replace and replaceAll.

英文:
class Main {
    public static void main(String[] args) {
        new Main().replace();
    }

    public void replace() {
        List&lt;Article&gt; articlesArrayList = new ArrayList&lt;&gt;();
        articlesArrayList.add(Article.builder().author(&quot;Sagar.Gangwal&quot;).build());
        articlesArrayList.stream().forEach(article -&gt; article.getAuthor().replaceAll(&quot;.&quot;, &quot; &quot;));

        articlesArrayList = articlesArrayList.stream()
                .map(a -&gt; this.update(a)).collect(Collectors.toList());
        System.out.println(articlesArrayList.get(0));
    }

    private static Article update(Article a) {
        a.author = a.getAuthor().replaceAll(&quot;\\.&quot;, &quot; &quot;);
        return a;
    }
}

You can see above code.

Here i am trying to create stream and then foreach element of that stream i want to update AuthorName with '.' to space.

articlesArrayList.stream().map(a -&gt; this.update(a)).collect(Collectors.toList());

You need to use stream API and then custom update method to update value.

Also for replacing &#39;.&#39;, you need to use escape character as well. replaceAll method of String class contains first argument as regular expression.

If you see this , i am escaping &#39;.&#39; with &#39;\\.&#39;.
a.getAuthor().replaceAll(&quot;\\.&quot;, &quot; &quot;);

So instead of replaceAll, you can go with replace method which simply take first argument as simply replace one character with passed target character.

You can see difference between replace and replaceAll.

huangapple
  • 本文由 发表于 2020年8月23日 23:48:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63548969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定