合并包含列表时,从包含列表的列表对象中删除重复项

时间:2018-03-27 19:08:30

标签: java list collections duplicates

I hava a POJO Classes:
    class Response {
        private List<Transmission> transmission;
    }
    class Transmission {
        private String transmission_id;
        private String transmission_name;
        private String transmission_type;
        private List<Page> page;
    }
    class Page {
        private String page_id;
        private String page_no;
        private String page_type;
    }

我有从数据库返回的响应列表:

transmission_id     transmission_name  transmission_type    page_id page_no         page_loaction


000066              XXX                     AAA                 1           1                 cdrive                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
000066              XXX                     AAA                 2           2                 cddfdrive                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
000066              XXX                     AAA                 3           3                 asdecdrive

000067              YYY                     BBB                 1           1                 dDrive                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
000067              YYY                     BBB                 2           2                 dDadecrive                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
000067              YYY                     BBB                 3           3                 dqwdDrive

我想通过修改从传输列表中删除重复元素来返回返回的Responseclass,这意味着每个transmission_id,transmission_name,transmission_type 我试图通过仅采用传输的唯一元素来实现以下目标

Ex:  <Transmission>
         <transmission_id>000066 </transmission_id>
         < transmission_name> XXX </transmission_name>
         < transmission_type> AAA </transmission_type>
         <Page>
            < page_id>1</ page_id>
            <page_no>1</page_no>
            <page_loaction>cdrive</page_loaction>
         </Page>
         <Page>
            < page_id>2</ page_id>
            <page_no>2</page_no>
            <page_loaction>cdrive</page_loaction>
         </Page>
         <Page>
            < page_id>3</ page_id>
            <page_no>3</page_no>
            <page_loaction>cdrive</page_loaction>
         </Page>
         </Transmission>
          <Transmission>
         <transmission_id>000067 </transmission_id>
         < transmission_name> YYY </transmission_name>
         < transmission_type> BBB </transmission_type>
         <Page>
            < page_id>1</ page_id>
            <page_no>2</page_no>
            <page_loaction> dDrive</page_loaction>
         </Page>
         <Page>
            < page_id>2</ page_id>
            <page_no>2</page_no>
            <page_loaction> dDrive</page_loaction>
         </Page>
         <Page>
            < page_id>3</ page_id>
            <page_no>3</page_no>
            <page_loaction>dDrive/page_loaction>
         </Page>
         </Transmission>

1 个答案:

答案 0 :(得分:0)

覆盖equals上的hashcodeTransmission。将所有结果添加到Set<Transmission>然后列出该集。

*编辑

Response addTransmission添加一项功能,检查您是否已有传输并向其添加页面或使用该页面创建新页面。

您可能希望使用像hibernate这样的ORM来为您处理映射