java中的List记录是否完全匹配方法
时间:2014-05-01 20:47:38
收藏:0
阅读:474
今天要说的是给List分组,然后用Map来封装,可能你看了以后还是有一些模糊。
先看一下项目结构图:
User类是一个VO类,主要逻辑还是在MapTestBak上面。
运行效果:
原理图:
1.在starsList中有两组人,共三人
2.在dolList中有一组人,共两人
3.经过marched操作,最后匹配到一组人到result中。即第一组人。
原理很简单。
===================================================
源码部分:
===================================================
/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!\n 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 }
我想要记录一下的是方法: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 }
在这个方法中,这里使用了两个List(即:comList, comList1)来记录是否完全匹配。
========================================================
多读一些书,英语很重要。
More reading,and english is important.
I‘m Hongten
========================================================
评论(0)