java ArrayList is grouped according to the same attribute

  • 2020-06-07 04:36:22
  • OfStack

java ArrayList is grouped according to the same 1 attribute

Preface:

In general, when querying a batch of data using SQL, you can use the GROUP BY statement in SQL to group the data, but sometimes for performance reasons, GROUP BY is not used. Instead, the data is first retrieved and then grouped in memory by an attribute using code.

code


public class SkuVo {

  private Long skuId;
  private String productName;
  private Long brandStoreSn;

  public SkuVo(Long skuId, String productName, Long brandStoreSn) {
    super();
    this.skuId = skuId;
    this.productName = productName;
    this.brandStoreSn = brandStoreSn;
  }

  public Long getSkuId() {
    return skuId;
  }
  public void setSkuId(Long skuId) {
    this.skuId = skuId;
  }
  public String getProductName() {
    return productName;
  }
  public void setProductName(String productName) {
    this.productName = productName;
  }
  public Long getBrandStoreSn() {
    return brandStoreSn;
  }
  public void setBrandStoreSn(Long brandStoreSn) {
    this.brandStoreSn = brandStoreSn;
  }

  @Override
  public String toString() {
    return "SkuVo [skuId=" + skuId + ", productName=" + productName + ", brandStoreSn=" + brandStoreSn + "]";
  }
}

Suppose that a batch of data is queried from the data and List exists < SkuVo > The inside. Use 1 algorithm to match skuId to List < SkuVo > The same skuId is grouped into 1 group.

Grouping algorithm


public class TestArrayListGroupByKey {

  public static void main(String[] args) {
    /*1 , prepare data **/
    SkuVo sku1 = new SkuVo(1L,"p1",100L);
    SkuVo sku2 = new SkuVo(2L,"p2",101L);
    SkuVo sku3 = new SkuVo(3L,"p3",102L);
    SkuVo sku4 = new SkuVo(3L,"p4",103L);
    SkuVo sku5 = new SkuVo(2L,"p5",100L);
    SkuVo sku6 = new SkuVo(5L,"p6",100L);

    List<SkuVo> skuVoList = Arrays.asList(new SkuVo [] {sku1,sku2,sku3,sku4,sku5,sku6});

    /*2 , grouping algorithm **/
    Map<Long, List<SkuVo>> skuIdMap = new HashMap<>();
    for (SkuVo skuVo : skuVoList) {
      List<SkuVo> tempList = skuIdMap.get(skuVo.getSkuId());
      /* If you can't get the data , So directly new1 empty ArrayList**/
      if (tempList == null) {
        tempList = new ArrayList<>();
        tempList.add(skuVo);
        skuIdMap.put(skuVo.getSkuId(), tempList);
      }
      else {
        /* a sku It's been stored before , Appends the data directly to the original List In the **/
        tempList.add(skuVo);
      }
    }

    /*3 And traverse map, The verification results **/
    for(Long skuId : skuIdMap.keySet()){
      System.out.println(skuIdMap.get(skuId));
    }
  }
}

The results are as follows


[SkuVo [skuId=1, productName=p1, brandStoreSn=100]]
[SkuVo [skuId=2, productName=p2, brandStoreSn=101], SkuVo [skuId=2, productName=p5, brandStoreSn=100]]
[SkuVo [skuId=3, productName=p3, brandStoreSn=102], SkuVo [skuId=3, productName=p4, brandStoreSn=103]]
[SkuVo [skuId=5, productName=p6, brandStoreSn=100]]

From the output, the data has been grouped according to skuId.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: