Repeat loop and set new value based on conditional check - java

I want to create such a condition that if the products have the same model, I want to install newID(A, B, C). The table below is the sample data that I return as List<MyDTO>when the method is called getAllListValue().

sample data:

pID    prodName  Model    Year 
10       PN1      ABX      1999
11       PN2      ABX      1999
12       PN3      ABX      2000
13       PN4      XP       2002
14       PN5      xP       2003
15       PN6      HP       2006
16       PN7      LX       2008
17       PN8      LX       2009
  • For PN1u model ABXi want to set newIDas A.
  • For PN2u model ABXi want to set newIDas B.
  • For PN3u model ABXi want to set newIDas C
  • For PN4and XP- set newIDas A
  • For PN5and XP- set newIDas B
  • For PN6and HP- set newIDas A
  • ..

code example:

public class MyDTO {
    private String pID;
    private String productName;
    private String model;
    private String year;
    private String newID;
    // getter & setter
}

public class UseLoop {
    //logic
    List<MyDTO> dtoList = getAllListValue();
    //here i want to iterate the list and set the newID (A,B,c) for the products with same model
    for(MyDTO dto : dtoList) {   
        //here how can i compare with previous object and assign the newID if the product has the same model.
    }
}
+6
2

Map<String, Character>, - , - .

Map<String, Character> map = new HashMap<>();
List<MyDTO> dtoList = getAllListValue();
for (MyDTO dto : dtoList) {
    // put 'A' in the map if the model name is not present yet, 
    // increment character otherwise
    Character c = map.merge(dto.getModel(), 'A', (k, v) -> (char) (v + 1));
    dto.setNewID(c.toString());
}

- :

MyDTO{pID='10', productName='PN1', model='ABX', year='1999', newID='A'}
MyDTO{pID='11', productName='PN2', model='ABX', year='1999', newID='B'}
MyDTO{pID='12', productName='PN3', model='ABX', year='2000', newID='C'}
MyDTO{pID='13', productName='PN4', model='XP', year='2000', newID='A'}
MyDTO{pID='14', productName='PN5', model='XP', year='2003', newID='B'}
MyDTO{pID='15', productName='PN6', model='HP', year='2006', newID='A'}
MyDTO{pID='16', productName='PN7', model='LX', year='2008', newID='A'}
MyDTO{pID='17', productName='PN8', model='LX', year='2009', newID='B'}
+1

, Java :

    list.stream().collect(
        Collectors.groupingBy( //group by model
            dto -> dto.getModel().toLowerCase(),

            Collector.of( () -> new int[1], //initial count for each group
                          (count, dto) -> {
                                              //count of 1 corresponds to A, 2 to B and so on
                                              dto.setNewID( String.valueOf((char) (count[0] + 65) ) ); 
                                              //increment count for each group
                                              count[0] += 1; 
                                          }, 
                          (count1, count2) -> {
                                              //combine counts for each group
                                              count1[0] += count2[0]; 
                                              return count1;
                                          }
                          )
            )
     );
0

All Articles