JAVA 去重List 重复对象

/**
     * 去重前端传来的参数 userOthers-- 用户对象避免数据库对象主键重复
     * @param userOthers
     * @return List<MeetingAppOtherUserCommand>
     * 2016-11-21
     * List<MeetingAppOtherUserCommand>
     */
    public static List<MeetingAppOtherUserCommand> tarnsMeetingAppOtherUserCommand(List<MeetingAppOtherUserCommand> userOthers){
    Set setWorknumber = new HashSet();
    List<MeetingAppOtherUserCommand> newUsers = new ArrayList<MeetingAppOtherUserCommand>();
    //当数据对象没有返回原有对象
    if(userOthers == null || userOthers.size() ==0){
    return userOthers;
    }
    System.out.println(" userOthers----size--:"+userOthers.size()+"----");
    for(MeetingAppOtherUserCommand meetingAppOtherUserCommand: userOthers){
    if(setWorknumber.add(meetingAppOtherUserCommand.getWorkNumber())){
    newUsers.add(meetingAppOtherUserCommand);
    }else{
    System.out.println(" MeetingAppOtherUserCommand----有重复对象---:"+meetingAppOtherUserCommand.getWorkNumber()+"----");
    }
    }
    System.out.println(" newUsers----size---:"+newUsers.size()+"----");
    return newUsers;
    }
   
    public static void main(String[] args) {
    System.out.println("  -- tarnsMeetingAppOtherUserCommand ----");
    List<MeetingAppOtherUserCommand> users = new ArrayList<MeetingAppOtherUserCommand>();
    MeetingAppOtherUserCommand user1 = new MeetingAppOtherUserCommand();
   
    user1.setWorkNumber("000001");
    user1.setGivenSign("Y");
    users.add(user1);
    users.add(user1);
    MeetingAppOtherUserCommand user2 = new MeetingAppOtherUserCommand();
   
    user2.setWorkNumber("000002");
    user2.setGivenSign("Y");
    users.add(user2);
    users.add(user2);
    MeetingAppOtherUserCommand user3 = new MeetingAppOtherUserCommand();
   
    user3.setWorkNumber("000002");
    user3.setGivenSign("Y");
    users.add(user3);
    //List<MeetingAppOtherUserCommand> users = new ArrayList<MeetingAppOtherUserCommand>();
    //List<MeetingAppOtherUserCommand> newUsers = tarnsMeetingAppOtherUserCommand(users);
    users = tarnsMeetingAppOtherUserCommand(users);
}

猜你喜欢

转载自javayuanliwang.iteye.com/blog/2339524