You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将List<String>映射为List<Customer>(Java 8实现)

解决Java 8 Stream将List转换为List的问题

首先修正你的代码中存在的两处关键问题:

  1. Customer类的语法错误:Java中不能用{get; set;}这种C#风格的语法,需要改为标准的私有字段+getter/setter方法,或者使用Lombok简化(如果项目允许)。
  2. Stream源错误:你当前调用的是customerList.stream(),但这个集合是空的,应该遍历nameList来生成Customer实例。

修正后的Customer类(标准写法)

public class Customer {
    private int id;
    private String name;
    private String company;

    // 无参构造器
    public Customer() {}

    // 带参构造器(可选,方便快速创建实例)
    public Customer(int id, String name, String company) {
        this.id = id;
        this.name = name;
        this.company = company;
    }

    // getter和setter方法
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }
}

方案1:基础转换(设置name,其他属性用默认值)

如果不需要自动生成id,或者可以给其他属性设置固定默认值,直接在map方法中创建Customer实例并赋值:

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

public class CustomerConverter {
    public static List<Customer> convertList() { 
        List<String> nameList = new ArrayList<>();
        nameList.add("Customer A");
        nameList.add("Customer B");
        nameList.add("Customer C");
        nameList.add("Customer D");
        nameList.add("Customer E");
        nameList.add("Customer F");
        
        return nameList.stream()
                       .map(name -> {
                           Customer customer = new Customer();
                           customer.setName(name);
                           // 给其他属性设置默认值
                           customer.setId(0);
                           customer.setCompany("Unknown");
                           return customer;
                       })
                       .collect(Collectors.toList()); 
    }
}

方案2:自动生成递增ID

如果需要给每个Customer分配递增的id,可以结合IntStream来关联索引:

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

public class CustomerConverter {
    public static List<Customer> convertList() { 
        List<String> nameList = new ArrayList<>();
        nameList.add("Customer A");
        nameList.add("Customer B");
        nameList.add("Customer C");
        nameList.add("Customer D");
        nameList.add("Customer E");
        nameList.add("Customer F");
        
        return IntStream.range(0, nameList.size())
                       .mapToObj(index -> {
                           String name = nameList.get(index);
                           return new Customer(index + 1, name, "Unknown");
                       })
                       .collect(Collectors.toList()); 
    }
}

简化写法(利用构造器或方法引用)

如果给Customer新增一个仅接收name的构造器:

public Customer(String name) {
    this.name = name;
    this.id = 0;
    this.company = "Unknown";
}

可以进一步简化map的写法:

public static List<Customer> convertList() { 
    List<String> nameList = new ArrayList<>();
    nameList.add("Customer A");
    nameList.add("Customer B");
    nameList.add("Customer C");
    nameList.add("Customer D");
    nameList.add("Customer E");
    nameList.add("Customer F");
    
    return nameList.stream()
                   .map(Customer::new) // 方法引用,调用单参构造器
                   .collect(Collectors.toList()); 
}

内容的提问来源于stack exchange,提问作者Wion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 15:50:40