在我的spring boot应用程序中,有一个候选类型列表-List。它具有几个属性(id,firstName,lastName,designation等)。
我想从第一个列表中获得另一个列表(List),该列表仅包含少量属性(id,lastName),其余值设置为null。 必需的属性(表列-id,lastName)仅在运行时获取。如何实现?
答案 0 :(得分:0)
您将必须创建新的Candidates
,并将字段设置为null。
假设您的候选课程看起来像这样...
public class Candidate {
private long id;
private String firstName;
// etc
public Candidate(long id, String firstName, String lastName, String designation /* etc */) {
...assign fields...
}
// Getters...
}
您可以执行以下操作...
List<Candidate> candidates = getThemSomehow();
List<Candidate> partialCandidates = candidates.stream()
.map(c -> new Candidate(c.getId(), null, c.getLastName(), null, null))
.collect(Collectors.toList());
编辑:
为什么不使用bean内省器?
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
List<Candidate> candidates = Arrays.asList(
new Candidate(1L, "Fred", "West", "Lunatic"),
new Candidate(2L, "John", "Lennon", "Beatles"),
new Candidate(3L, "Dave", "Smith", "SomeBlokeIMetDownThePub")
);
Set<String> fields = new HashSet<>(Arrays.asList("id", "firstName", "designation"));
List<Candidate> partialCandidates = candidates.stream().map(c -> createPartialCopy(c, fields)).collect(Collectors.toList());
partialCandidates.forEach(System.out::println);
}
public static <T> T createPartialCopy(T source, Set<String> properties) {
BeanInfo beanInfo;
try {
beanInfo = Introspector.getBeanInfo(source.getClass());
} catch (IntrospectionException e) {
throw new IllegalStateException("Could not introspect " + source, e);
}
T copy;
try {
copy = (T) source.getClass().newInstance();
} catch (InstantiationException e) {
throw new IllegalStateException("Could not instantiate " + source.getClass(), e);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Could not create " + source.getClass() + " no public zero arg constructor.", e);
}
properties.stream()
.flatMap(p -> Arrays.stream(beanInfo.getPropertyDescriptors()).filter(d -> d.getName().equals(p)))
.forEach(d -> doCopy(d, source, copy));
return copy;
}
private static <T> void doCopy(PropertyDescriptor propertyDescriptor, T source, T copy) {
try {
propertyDescriptor.getWriteMethod().invoke(copy, propertyDescriptor.getReadMethod().invoke(source));
} catch (InvocationTargetException | IllegalAccessException e) {
throw new IllegalStateException("Could copy property " + propertyDescriptor.getName(), e);
}
}
public static class Candidate {
private long id;
private String firstName;
private String lastName;
private String designation;
public Candidate() {
}
public Candidate(long id, String firstName, String lastName, String designation) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.designation = designation;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
@Override
public String toString() {
return "Candidate{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", designation='" + designation + '\'' +
'}';
}
}
}
打印...
Candidate{id=1, firstName='Fred', lastName='null', designation='Lunatic'}
Candidate{id=2, firstName='John', lastName='null', designation='Beatles'}
Candidate{id=3, firstName='Dave', lastName='null', designation='SomeBlokeIMetDownThePub'}
答案 1 :(得分:0)
您可以使用Java 8轻松实现。
public class Candidate {
public Candidate() {
super();
}
public Candidate(int id, String firstName) {
super();
this.id=id;
this.firstName=firstName;
}
private int id;
private String firstName;
private String lastName;
private String designation;
//getters &setters
@Override
public String toString() {
return this.id +"....FirstName:"+this.firstName+"......LastName:"+this.lastName+"....Designation:"+this.designation;
}
}
公共课程测试{
public static void main(String[] args) {
List<Candidate> canditates= new ArrayList<>();
for(int i=1;i<=5;i++){
Candidate candidate = new Candidate();
candidate .setId(i);
candidate .setDesignation("Desig:"+i);
candidate .setFirstName("FrstName:"+i);
candidate .setLastName("LastName:"+i);
canditates.add(candidate );
}
System.out.println(canditates);
List<Candidate> newList=canditates.stream()
.map(c -> new Candidate(c.getId(), c.getFirstName()))
.collect(Collectors.toList());
System.out.println(newList);
}
}