两个list。如何使用流的写法将一个list中的对象中的某些属性根据另外一个list中的属性值赋值进去?

两个list。如何使用流的写法将一个list中的对象中的某些属性根据另外一个list中的属性值赋值进去?

你可以使用Java 8以上版本中的流(Stream)和Lambda表达式来实现这个需求。假设有两个List,一个是sourceList,包含要赋值属性的对象;另一个是targetList,包含属性值的对象。以下是一个示例代码:

import java.util.*;

class SourceObject {
    private String name;
    private int age;

    // 构造函数、getter和setter方法省略
}

class TargetObject {
    private String name;
    private int age;

    // 构造函数、getter和setter方法省略
}

public class Main {
    public static void main(String[] args) {
        List<SourceObject> sourceList = new ArrayList<>();
        sourceList.add(new SourceObject("John", 25));
        sourceList.add(new SourceObject("Alice", 30));
        sourceList.add(new SourceObject("Bob", 35));

        List<TargetObject> targetList = new ArrayList<>();
        targetList.add(new TargetObject(null, 0));
        targetList.add(new TargetObject(null, 0));
        targetList.add(new TargetObject(null, 0));

        // 使用流的方式将属性赋值
        List<TargetObject> result = IntStream.range(0, sourceList.size())
                .mapToObj(i -> {
                    TargetObject target = targetList.get(i);
                    SourceObject source = sourceList.get(i);
                    target.setName(source.getName());
                    target.setAge(source.getAge());
                    return target;
                })
                .collect(Collectors.toList());

        // 输出结果
        result.forEach(obj -> System.out.println("Name: " + obj.getName() + ", Age: " + obj.getAge()));
    }
}

在这个示例中,我们使用IntStream的range方法生成一个数字序列,然后通过mapToObj方法将序列中的索引与源列表和目标列表中的对象一一对应起来。然后,我们根据对应的索引来获取源列表中的对象和目标列表中的对象,并将需要赋值的属性赋值过去。最后,将赋值完毕的目标对象收集到一个新的列表中。

Java中使用流的方式将list变成k-v结构的map?

在Java中,你可以使用流(Stream)的collect方法结合Collectors.toMap来将List转换为K-V结构的Map。下面是一个示例代码:

import java.util.*;
import java.util.stream.Collectors;

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

public class Main {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("John", 25));
        personList.add(new Person("Alice", 30));
        personList.add(new Person("Bob", 35));

        // 使用流的方式将List转换为Map
        Map<String, Integer> personMap = personList.stream()
                .collect(Collectors.toMap(Person::getName, Person::getAge));

        // 输出结果
        personMap.forEach((name, age) -> System.out.println("Name: " + name + ", Age: " + age));
    }
}

在这个示例中,我们使用流的stream方法将personList转换为一个流。然后,使用collect方法结合Collectors.toMap来将流中的每个元素映射为键值对,并收集到一个Map中。在toMap方法中,Person::getName表示将Person对象的name属性作为键,Person::getAge表示将Person对象的age属性作为值。最后,我们通过forEach方法遍历并输出转换后的Map结果。

请注意,如果List中存在重复的键,会抛出java.lang.IllegalStateException异常。这种情况下,你可以使用第三个参数mergeFunction来处理重复键的冲突,例如:

Map<String, Integer> personMap = personList.stream()
        .collect(Collectors.toMap(Person::getName, Person::getAge, (oldValue, newValue) -> newValue));

在这个例子中,我们简单地选择了新值作为键的值,你可以根据自己的需求进行适当的处理。

猜你喜欢

转载自blog.csdn.net/weixin_50503886/article/details/132310121