猿问

查找列表的对象是否存在于另一个列表中

我有两个列表List<AreaOfInterest>和List<AreaOfInterestBean>. 对应的类是,


@Entity

@Data

@Table( name = "interest_area" )

public class AreaOfInterest {


    @Id

    @GeneratedValue( strategy = GenerationType.AUTO )

    @Column( name = "ia_id" )

    private Long id;


    @Column( name = "ia_value" )

    private String value;


    @Column( name = "ia_is_active" )

    private Boolean isActive;


    @Column( name = "ia_created_at" )

    private Calendar createdAt;


    @Column( name = "ia_is_approved" )

    private Boolean isApprovedByAdmin;


    @Column( name = "ia_approved_at" )

    private Calendar approvedAt;


}

和,


@Data

public class AreaOfInterestBean {


    private Long id;


    private String value;


    private boolean isSelected;

}

我想检查 的对象List<AreaOfInterest>是否存在List<AreaOfInterestBean>,如果存在,我需要更新List<AreaOfInterestBean>. 现在,我觉得我必须迭代两个列表来解决这个问题。有什么有效的方法吗?


桃花长相依
浏览 175回答 2
2回答

动漫人物

老实说,我不确定这的复杂性(我明天会尝试弄清楚):Set<String> left = nonBeans.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(AreaOfInterest::getValue)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toSet());beans.stream()&nbsp; &nbsp; &nbsp; .filter(x -> left.contains(x.getValue()))&nbsp; &nbsp; &nbsp; .forEach(x -> x.setSelected(true));在这种情况下,复杂性将为O(n).

ibeautiful

假设:List<AreaOfInterest> list = /* ... */List<AreaOfInterestBean> listOfBeans = /* ... */两个简单的迭代可以轻松实现:for (int i=0; i<listOfBeans.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Iterate beans&nbsp; &nbsp; AreaOfInterestBean aoiBean = listOfBeans.get(i);&nbsp; &nbsp; &nbsp; // Get each bean&nbsp; &nbsp; for (int j=0; j<list.size(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Iterate entity&nbsp; &nbsp; &nbsp; &nbsp; AreaOfInterest aoi = list.get(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Get each entity&nbsp; &nbsp; &nbsp; &nbsp; if (aoiBean.getId() == aoi.getId()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// If they have the same ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aoiBean.setValue(aoi.getValue());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Update the bean value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Go to the next bean&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}示例:具有以下简化数据:AreaOfInterest同id和value:1, "Car",2, "Bike",3, "Ship"AreaOfInterestBeans同id和value:5, "Limo",2, "Scooter",7, "Yacht"结果AreaOfInterestBeans将是 id 的替换值2:Scooter-> Bike。AreaOfInterestBeans同id和value:5, "Limo",2, "Bike",7, "Yacht"另一种流方式:listOfBeans = listOfBeans.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .peek(aoiBean -> list.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.filter(aoi -> aoi.getId() == aoiBean.getId())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.findFirst()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.ifPresent(i -> aoiBean.setValue(i.getValue())))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());
随时随地看视频慕课网APP

相关分类

Java
我要回答