Home » Java » java – How to check duplicate in an arraylist based on some object attribute-Exceptionshub

java – How to check duplicate in an arraylist based on some object attribute-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have an employee object define as follows:

Employee class contains 3 attributes :

String employeID
//payCodeType can only be T/Y/N/W/M/F
String payCodeType
String serial

The list contains duplicate values as follows:

employeID - emp01
payCodeType - M
serial 1
employeID - emp01
payCodeType - M 
serial 2

employeID - emp01
payCodeType - F
serial 3

employeID - emp02
payCodeType - F
serial 1

employeID - emp02
payCodeType - F
serial 2

employeID - emp02
payCodeType - F
serial 3

employeID - emp02
payCodeType - F
serial 4

employeID - emp02
payCodeType - F
serial 5

employeID - emp03
payCodeType - T
serial 3

employeID - emp04
payCodeType - T
serial 1

employeID - emp04
payCodeType - T
serial 2

employeID - emp04
payCodeType - T
serial 3

employeID - emp05
payCodeType - F
serial 1

employeID - emp05
payCodeType - F
serial 1
...

Basically I need display an error message when there is duplicate data and the rule is as follows:

  1. Any of the pay type code cannot have more than one occurrence of
    employee with same employeID,payCodeType and serial
  2. The same employeID cannot have more than one payCodeType – M – in
    the above example emp01 is wrong it has 2
  3. The same employeID cannot have more than 4 occurrence for
    payCodeType -F with different serial id – in the
    above example emp02 it has 5
  4. The same employeID cannot have more than 2 payCodeType – T with different serial id – in the above example emp04
    has 3

Any idea how i can achieve it in Java 8 please?

How to&Answers: