英文:
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<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论