cool hit counter List records in java for exact match method_Intefrankly

List records in java for exact match method


Today's talk is about grouping a List and then wrapping it with a Map, which may still be a little fuzzy after you read it.

Let's start by looking at the project structure.

The User class is a VO class, and the main logic is still on top of MapTestBak.

Running effect.

Principle diagram.

1.There are two groups of three people in the StarsList

2.There is a group of two people in the dolList

3.After the marched operation, a final set of people is matched to the RESULT. That is, the first group of people.

The principle is simple.

===================================================

Source code section.

===================================================

/mapTest/src/com/b510/map/MapTestBak.java

  1 /**
  2  * 
  3  */
  4 package com.b510.map;
  5 
  6 import java.util.ArrayList;
  7 import java.util.HashMap;
  8 import java.util.List;
  9 import java.util.Map;
 10 
 11 /**
 12  * @author Hongten
 13  * @created Apr 28, 2014
 14  */
 15 public class MapTestBak {
 16 
 17     public static void main(String[] args) {
 18         mapTest();
 19     }
 20 
 21     /**
 22      * 
 23      */
 24     private static void mapTest() {
 25         // starsList
 26         List<User> starsList = getStarsList();
 27 
 28         // dolList
 29         List<User> dolList = getDolList();
 30 
 31         Map<String, List<User>> map = groupingHandler(starsList);
 32         // test
 33         groupingHandlerTest(map);
 34 
 35         System.out.println("======= group finished =======");
 36         List<Map<String, List<User>>> compareResultList = compare(dolList, map);
 37         if(null != compareResultList){
 38             System.out.println(compareResultList.size());
 39         }
 40     }
 41 
 42     /**
 43      * @param dolList
 44      * @param map
 45      */
 46     private static List<Map<String, List<User>>> compare(List<User> dolList, Map<String, List<User>> map) {
 47         List<Map<String, List<User>>>  tempList = new ArrayList<Map<String, List<User>>>();
 48         if (null != map) {
 49             for (String key : map.keySet()) {
 50                 List<User> u = map.get(key);
 51                 List comList = new ArrayList();
 52                 boolean comFlag = true;
 53                 for (User us : u) {
 54                     List comList1 = new ArrayList();
 55                     for (User ul : dolList) {
 56                         if (us.getGroupNO() == ul.getGroupNO() && us.getName().trim().equals(ul.getName().trim())) {
 57                             comList1.add(1);
 58                         } else {
 59                             comList1.add(0);
 60                         }
 61                     }
 62                     if (comList1.contains(1)) {
 63                         // name has existed.
 64                         comList.add(1);
 65                     } else {
 66                         comList.add(0);
 67                     }
 68                 }
 69                 if (comList.contains(0)) {
 70                     comFlag = false;
 71                 }
 72                 // print out the match result in the console.
 73                 printMatchResult(tempList, key, u, comFlag);
 74             }
 75         }else{
 76             System.out.println("map is null!");
 77         }
 78         return tempList;
 79     }
 80 
 81     /**
 82      * @param tempList
 83      * @param key
 84      * @param u
 85      * @param comFlag
 86      */
 87     private static void printMatchResult(List<Map<String, List<User>>> tempList, String key, List<User> u, boolean comFlag) {
 88         if (comFlag) {
 89             // do something
 90             System.out.println("group : " + key + " compared!
 Detail:");
 91             System.out.println("================");
 92             for (User ut : u) {
 93                 System.out.println(ut.getGroupNO() + ", " + ut.getName());
 94             }
 95             System.out.println("================");
 96             Map<String, List<User>> tempMap = new HashMap<String, List<User>>();
 97             tempMap.put(key, u);
 98             tempList.add(tempMap);
 99         } else {
100             System.out.println("group : " + key + " NOT compared!");
101         }
102     }
103 
104     /**
105      * @param map
106      */
107     private static void groupingHandlerTest(Map<String, List<User>> map) {
108         if (null != map) {
109             for (String key : map.keySet()) {
110                 List<User> u = map.get(key);
111                 for (User u1 : u) {
112                     System.out.println(u1.getGroupNO() + ", " + u1.getName());
113                 }
114             }
115         }
116     }
117 
118     /**
119      * @param starsList
120      * @param map
121      */
122     private static Map<String, List<User>> groupingHandler(List<User> starsList) {
123         Map<String, List<User>> map = new HashMap<String, List<User>>();
124         for (User stars_user : starsList) {
125             String no = String.valueOf(stars_user.getGroupNO());
126             if (map.isEmpty()) {
127                 List<User> l = new ArrayList<User>();
128                 l.add(stars_user);
129                 map.put(no, l);
130             } else {
131                 List<User> user_map = map.get(no);
132                 if (null == user_map || "".equals(user_map)) {
133                     List<User> l = new ArrayList<User>();
134                     l.add(stars_user);
135                     map.put(no, l);
136                 } else {
137                     List<User> l = map.get(no);
138                     l.add(stars_user);
139                     map.put(no, l);
140                 }
141             }
142         }
143         return map;
144     }
145 
146     /**
147      * @param dolList
148      */
149     private static List<User> getDolList() {
150         List<User> dolList = new ArrayList<User>();
151         User user_B1_dol = new User(1, "MRS KON");
152         User user_A_dol = new User(1, "KON SUE");
153         dolList.add(user_B1_dol);
154         dolList.add(user_A_dol);
155         return dolList;
156     }
157 
158     /**
159      * @param starsList
160      */
161     private static List<User> getStarsList() {
162         List<User> starsList = new ArrayList<User>();
163         User user_B1 = new User(1, "MRS KON");
164         User user_A = new User(1, "KON SUE");
165         User user_B2 = new User(2, "MRS KON");
166         User user_C = new User(2, "LON MEI");
167         starsList.add(user_B1);
168         starsList.add(user_A);
169         starsList.add(user_B2);
170         starsList.add(user_C);
171         return starsList;
172     }
173 }

/mapTest/src/com/b510/map/User.java

 1 /**
 2  * 
 3  */
 4 package com.b510.map;
 5 
 6 /**
 7  * @author Hongten
 8  * @created Apr 28, 2014
 9  */
10 public class User {
11 
12     private int groupNO;
13     private String name;
14 
15     public int getGroupNO() {
16         return groupNO;
17     }
18 
19     public User(int groupNO, String name) {
20         this.groupNO = groupNO;
21         this.name = name;
22     }
23 
24     public void setGroupNO(int groupNO) {
25         this.groupNO = groupNO;
26     }
27 
28     public String getName() {
29         return name;
30     }
31 
32     public void setName(String name) {
33         this.name = name;
34     }
35 
36 }

What I want to document is the method:compare(List<User> dolList, Map<String, List<User>> map)

 1 private static List<Map<String, List<User>>> compare(List<User> dolList, Map<String, List<User>> map) {
 2         List<Map<String, List<User>>>  tempList = new ArrayList<Map<String, List<User>>>();
 3         if (null != map) {
 4             for (String key : map.keySet()) {
 5                 List<User> u = map.get(key);
 6                 List comList = new ArrayList();
 7                 boolean comFlag = true;
 8                 for (User us : u) {
 9                     List comList1 = new ArrayList();
10                     for (User ul : dolList) {
11                         if (us.getGroupNO() == ul.getGroupNO() && us.getName().trim().equals(ul.getName().trim())) {
12                             comList1.add(1);
13                         } else {
14                             comList1.add(0);
15                         }
16                     }
17                     if (comList1.contains(1)) {
18                         // name has existed.
19                         comList.add(1);
20                     } else {
21                         comList.add(0);
22                     }
23                 }
24                 if (comList.contains(0)) {
25                     comFlag = false;
26                 }
27                 // print out the match result in the console.
28                 printMatchResult(tempList, key, u, comFlag);
29             }
30         }else{
31             System.out.println("map is null!");
32         }
33         return tempList;
34     }

In this method, two Lists (i.e.: comList, comList1) are used here to record whether there is an exact match or not.

E | hongtenzone@foxmail.com B |http://www.cnblogs.com/hongten


Recommended>>
1、XAbased implementation of distributed transactions in MySQL
2、BDTC2017An indepth look at the exploration and practice of big data in the transportation and tourism sector
3、Worse than a world war were not just losing our jobs in the age of AI
4、JavaORMHibernateIntroductory Notes
5、224 seconds to train ImageNet This time the record was set by Sony Biglaw

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号