Total Pageviews

2017/11/05

[Java] Make a unique list of objects

Problem
I am using do query to retrieve a List of Cam101wGridData data. The data contains user name and email address. 
User asks to remove duplicate email address from the data list, how to do it?
1
    List<Cam101wGridData> result= doQuery(form, result);


How-To
Steps are as following:
Step1. Create a Cam101wEmailCsvBean and override equals and hashCode methods (using email attribute)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    package x.x.x.model;
    
    import java.io.Serializable;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import lombok.NoArgsConstructor;
    
    /**
     * Mail格式下載 bean (解決 Email 重複的資料)
     *
     * Any class definition may be annotated with @EqualsAndHashCode to let lombok generate
     * implementations of the equals(Object other) and hashCode() methods. By default, it'll use all
     * non-static, non-transient fields, but you can exclude more fields by naming them in the optional
     * exclude parameter to the annotation. Alternatively, you can specify exactly which fields you wish
     * to be used by naming them in the of parameter.
     */
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    @EqualsAndHashCode(of = "email")
    public class Cam101wEmailCsvBean implements Serializable {
        private static final long serialVersionUID = 1L;
    
        /**
         * 會員姓名
         */
        private String userName;
    
        /**
         * E-Mail
         */
        private String email;
    
    }


Step2. Convert from List<Cam101wGridData>  to List<Cam101wEmailCsvBean>:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
   /**
     * 將 List<Cam101wGridData> 轉成 List<Cam101wEmailCsvBean>.
     * 
     * @param result List<Cam101wGridData>
     * @return List<Cam101wEmailCsvBean>
     */
    private List<Cam101wEmailCsvBean> convertToCam101wEmailCsvBean(List<Cam101wGridData> result) {
        List<Cam101wEmailCsvBean> emailCsvBeans = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(result)) {
            for (Cam101wGridData row : result) {
                Cam101wEmailCsvBean emailBean = new Cam101wEmailCsvBean(row.getUserName(),
                        row.getEmail());
                emailCsvBeans.add(emailBean);
            }
        }
        return emailCsvBeans;
    }


Step3. Convert List to Set (remove duplicate email), and convert Set to List 
1
2
3
4
5
6
7
8
   private List<Cam101wEmailCsvBean> getDistinctEmailCsvBean(List<Cam101wEmailCsvBean> emailCsvBeans) {
       List<Cam101wEmailCsvBean> distinctEmailCsvBeans = new ArrayList<>();
       
       Set<Cam101wEmailCsvBean> emailCsvSet = new HashSet<>(emailCsvBeans);
       distinctEmailCsvBeans.addAll(emailCsvSet);
       
       return distinctEmailCsvBeans;
   }


Code snippet (Before and after)
1
2
3
4
5
6
7
    // before (with duplicate emails)
    List<Cam101wGridData> result= doQuery(form, result);
    
    // after (with distinct email)
    List<Cam101wGridData> result= doQuery(form, result);
    List<Cam101wEmailCsvBean> emailCsvBeans = convertToCam101wEmailCsvBean(result);
    List<Cam101wEmailCsvBean> distinctEmailCsvBeans = getDistinctEmailCsvBean(emailCsvBeans);




Reference
[1] https://projectlombok.org/features/EqualsAndHashCode.html    
[2] https://stackoverflow.com/questions/20126470/java-how-to-keep-unique-values-in-a-list-of-objects




No comments: