Classified by Map

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/wxm994116/article/details/78500777

Map by classification, the following were resolved through a small example of how easier to understand!

Objective: To be classified according to material code Material name + id + material

Procedure:
1, first define a class map, a map which stood key and a value, as shown in the following code: (refer to the above mentioned key is not encoded + name + id, value refers to the combination of these three satisfies together thin single)

    Map<String, List<AppPriceAffirmDetail>> map = new HashMap<String, List<AppPriceAffirmDetail>>();

2, traversing the whole obtained data is a List, traversing first determined material code and the name is empty, the next is not empty, assignment key, and acquiring the condition by the map GET (key) method single classified fine judgment, if at this time to obtain a single thin classified as empty, then Create a list, and then traversing the thin single piece of unsorted into them, then put map of the (key, value ) the method of placement, if the acquired single classified fine is not empty, place the proof found value corresponding to the key, then a single thin single thin strip directly into them unsorted list of classified specific code as follows:

Map<String, List<AppPriceAffirmDetail>> map = new HashMap<String, List<AppPriceAffirmDetail>>();
        for (AppPriceAffirmDetail priceAffirmDetail : appPriceAffirmDetails) {
            String materialCode = priceAffirmDetail.getMaterialCode();
            String materialName = priceAffirmDetail.getMaterialName();
            Long priceInquiryMaterialId = priceAffirmDetail
                    .getPriceInquiryMaterialId();
            if (StringUtils.isBlank(materialCode)
                    && StringUtils.isBlank(materialName)) {
                continue;
            }
            String key = materialCode + "," + materialName + ","
                    + priceInquiryMaterialId;
            List<AppPriceAffirmDetail> priceAffirmDetails = map.get(key);
            if (priceAffirmDetails == null) {
                priceAffirmDetails = new ArrayList<AppPriceAffirmDetail>();
                priceAffirmDetails.add(priceAffirmDetail);
                map.put(key, priceAffirmDetails);
            } else {
                priceAffirmDetails.add(priceAffirmDetail);
            }
        }

3, after the completion of the classification finishing, finishing by a set method, get out of something that is boring material coding + name + material id, then the following is to satisfy the conditions of a single fine! Since the interface requires, the following code only set of materials and material coding name, but does not affect the results of the classification!
The similar effect as shown in FIG, tick ignored, it is successful and not successful in distinguishing

    for (String str : keys) {
            String[] strs = str.split(",");
            AppPriceAffirmDetailByMaterial appPriceAffirmDetailByMaterial = new AppPriceAffirmDetailByMaterial();
            appPriceAffirmDetailByMaterial.setMaterialCode(strs[0]);
            appPriceAffirmDetailByMaterial.setMaterialName(strs[1]);
            List<AppPriceAffirmDetail> a = map.get(str);
            ......做你想做的事情......
            }

These are the small series little experience in the course of the project, if it helps you, welcome thumbs up or to force a little Oh! Source forwarded the article to be marked, thank you!

Guess you like

Origin blog.csdn.net/wxm994116/article/details/78500777
map