将对象的 ArrayList 转换为该对象的 ArrayList Java 中的字符串名称

发布于 2024-09-14 12:05:13 字数 233 浏览 4 评论 0原文

我有一个 User 对象的 ArrayList。现在我只需要这些用户名的 ArrayList。有没有办法在整个 ArrayList 上使用 toString() 并将其转换为字符串名称的 ArrayList,而不是在 for 循环中执行此操作?我还重写了 User 类中的 toString ,以便它返回用户名,并且我尝试了 ArrayListname = usersList.toString() 但它不起作用。

I have an ArrayList of User objects. Now I need the ArrayList of these user's names only. Is there a way to use toString() on entire ArrayList and convert it to ArrayList of String names rather than doing this in for loop? I have also overridden toString in User class so it returns user's name, and I have tried ArrayList <String> names = usersList.toString() but it didn't work.

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

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

发布评论

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

评论(4

十秒萌定你 2024-09-21 12:05:13

您可以使用 Google Collections API 执行此操作:

List<User> userList = ...;
List<String> nameList = Lists.transform(userList, new Function<User, String>() {
   public String apply(User from) {
      return from.toString(); // or even from.getName();
   }
});

该库已重命名为 Guava 。


在 Java 8 中,使用流和方法引用,即使不使用 Guava,您现在也可以实现相同的目标:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

class User {

    private String name;

    public User() {
    }

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class App {
    public static void main(String[] args) {
        List<User> users = new ArrayList<>(Arrays.asList(
                new User("Alan Turing"),
                new User("John von Neumann"),
                new User("Edsger W Dijkstra")
        ));

        List<String> names = users
                .stream()
                .map(User::getName)
                .collect(Collectors.toList());

        System.out.println(names);
    }
}

You can do this using the Google Collections API:

List<User> userList = ...;
List<String> nameList = Lists.transform(userList, new Function<User, String>() {
   public String apply(User from) {
      return from.toString(); // or even from.getName();
   }
});

The library has been renamed to Guava.


With Java 8, using streams and method references you can now achieve the same thing even without using Guava:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

class User {

    private String name;

    public User() {
    }

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class App {
    public static void main(String[] args) {
        List<User> users = new ArrayList<>(Arrays.asList(
                new User("Alan Turing"),
                new User("John von Neumann"),
                new User("Edsger W Dijkstra")
        ));

        List<String> names = users
                .stream()
                .map(User::getName)
                .collect(Collectors.toList());

        System.out.println(names);
    }
}
别靠近我心 2024-09-21 12:05:13

对象# toString() 返回一个 String,而不是 ArrayList。那是你的问题。你真的需要循环它。绝对没有理由反对循环。如果代码以某种方式困扰您,只需将其隐藏在某个实用方法中即可;)


使用 Java 8,您可以使用 lambda 表达式、流和方法引用来简化代码。

以下内容

List<User> users = getItSomehow();
List<String> names = new ArrayList<String>();
for (User user : users) {
    names.add(user.getName());
}

然后可以将

List<User> users = getItSomehow();
List<String> names = users.stream().map(User::getName).collect(Collectors.toList());

缩短为 like顺便说一句,您也可以将 for 放在一行中:

for (User user : users) names.add(user.getName());

但是对于初学者来说,它可能无法直接理解。

The Object#toString() returns a String, not an ArrayList. That's your problem. You really need to loop over it. There's absolutely no reason for an aversion against looping. Just hide it away in some utility method if the code is bothering you somehow ;)


With Java 8, you can use lambda expressions, streams, and method references to simplify your code.

The following

List<User> users = getItSomehow();
List<String> names = new ArrayList<String>();
for (User user : users) {
    names.add(user.getName());
}

can then be shortened to like

List<User> users = getItSomehow();
List<String> names = users.stream().map(User::getName).collect(Collectors.toList());

By the way, you can also put the for in a single line:

for (User user : users) names.add(user.getName());

It may however not be directly understandable for starters.

划一舟意中人 2024-09-21 12:05:13

总之:不。你需要的是一个 map 函数,它Java 中还没有(正如 BalusC 指出的那样)。您可以自己编写类似的内容,但无论如何您最终都会迭代列表中的每个元素。

In a word: No. What you need is a map function, which you don't have in Java (yet, as BalusC points out). You can write something similar yourself, but you'll end up iterating over each element in your List regardless.

最好是你 2024-09-21 12:05:13
public ArrayList< String > toStringList( Collection< MyObject > objectList )
{
    ArrayList< String > stringList = new ArrayList< String >();

    for( MyObject myobj : objectList ) {
        stringList.add( myobj.toString() );
    }

    return stringList;
}

...

ArrayList< String > myStringList = toStringList( objectList );

那里。一行,您可以将其与您将来拥有的任何对象集合一起重复使用。

public ArrayList< String > toStringList( Collection< MyObject > objectList )
{
    ArrayList< String > stringList = new ArrayList< String >();

    for( MyObject myobj : objectList ) {
        stringList.add( myobj.toString() );
    }

    return stringList;
}

...

ArrayList< String > myStringList = toStringList( objectList );

There. One line, and you can reuse it with whatever collection of objects you have in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文