java中的List记录是否完全匹配方法
今天要说的是给List分组,然后用Map来封装,可能你看了以后还是有一些模糊。
先看一下项目结构图:

User类是一个VO类,主要逻辑还是在MapTestBak上面。
运行效果:

原理图:
1.在starsList中有两组人,共三人
2.在dolList中有一组人,共两人
3.经过marched操作,最后匹配到一组人到result中。即第一组人。
原理很简单。

===================================================
源码部分:
===================================================
/mapTest/src/com/b510/map/MapTestBak.java
/**
*
*/
package com.b510.map; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* @author Hongten
* @created Apr 28, 2014
*/
public class MapTestBak { public static void main(String[] args) {
mapTest();
} /**
*
*/
private static void mapTest() {
// starsList
List<User> starsList = getStarsList(); // dolList
List<User> dolList = getDolList(); Map<String, List<User>> map = groupingHandler(starsList);
// test
groupingHandlerTest(map); System.out.println("======= group finished =======");
List<Map<String, List<User>>> compareResultList = compare(dolList, map);
if(null != compareResultList){
System.out.println(compareResultList.size());
}
} /**
* @param dolList
* @param map
*/
private static List<Map<String, List<User>>> compare(List<User> dolList, Map<String, List<User>> map) {
List<Map<String, List<User>>> tempList = new ArrayList<Map<String, List<User>>>();
if (null != map) {
for (String key : map.keySet()) {
List<User> u = map.get(key);
List comList = new ArrayList();
boolean comFlag = true;
for (User us : u) {
List comList1 = new ArrayList();
for (User ul : dolList) {
if (us.getGroupNO() == ul.getGroupNO() && us.getName().trim().equals(ul.getName().trim())) {
comList1.add(1);
} else {
comList1.add(0);
}
}
if (comList1.contains(1)) {
// name has existed.
comList.add(1);
} else {
comList.add(0);
}
}
if (comList.contains(0)) {
comFlag = false;
}
// print out the match result in the console.
printMatchResult(tempList, key, u, comFlag);
}
}else{
System.out.println("map is null!");
}
return tempList;
} /**
* @param tempList
* @param key
* @param u
* @param comFlag
*/
private static void printMatchResult(List<Map<String, List<User>>> tempList, String key, List<User> u, boolean comFlag) {
if (comFlag) {
// do something
System.out.println("group : " + key + " compared!\n Detail:");
System.out.println("================");
for (User ut : u) {
System.out.println(ut.getGroupNO() + ", " + ut.getName());
}
System.out.println("================");
Map<String, List<User>> tempMap = new HashMap<String, List<User>>();
tempMap.put(key, u);
tempList.add(tempMap);
} else {
System.out.println("group : " + key + " NOT compared!");
}
} /**
* @param map
*/
private static void groupingHandlerTest(Map<String, List<User>> map) {
if (null != map) {
for (String key : map.keySet()) {
List<User> u = map.get(key);
for (User u1 : u) {
System.out.println(u1.getGroupNO() + ", " + u1.getName());
}
}
}
} /**
* @param starsList
* @param map
*/
private static Map<String, List<User>> groupingHandler(List<User> starsList) {
Map<String, List<User>> map = new HashMap<String, List<User>>();
for (User stars_user : starsList) {
String no = String.valueOf(stars_user.getGroupNO());
if (map.isEmpty()) {
List<User> l = new ArrayList<User>();
l.add(stars_user);
map.put(no, l);
} else {
List<User> user_map = map.get(no);
if (null == user_map || "".equals(user_map)) {
List<User> l = new ArrayList<User>();
l.add(stars_user);
map.put(no, l);
} else {
List<User> l = map.get(no);
l.add(stars_user);
map.put(no, l);
}
}
}
return map;
} /**
* @param dolList
*/
private static List<User> getDolList() {
List<User> dolList = new ArrayList<User>();
User user_B1_dol = new User(1, "MRS KON");
User user_A_dol = new User(1, "KON SUE");
dolList.add(user_B1_dol);
dolList.add(user_A_dol);
return dolList;
} /**
* @param starsList
*/
private static List<User> getStarsList() {
List<User> starsList = new ArrayList<User>();
User user_B1 = new User(1, "MRS KON");
User user_A = new User(1, "KON SUE");
User user_B2 = new User(2, "MRS KON");
User user_C = new User(2, "LON MEI");
starsList.add(user_B1);
starsList.add(user_A);
starsList.add(user_B2);
starsList.add(user_C);
return starsList;
}
}
/mapTest/src/com/b510/map/User.java
/**
*
*/
package com.b510.map; /**
* @author Hongten
* @created Apr 28, 2014
*/
public class User { private int groupNO;
private String name; public int getGroupNO() {
return groupNO;
} public User(int groupNO, String name) {
this.groupNO = groupNO;
this.name = name;
} public void setGroupNO(int groupNO) {
this.groupNO = groupNO;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
我想要记录一下的是方法:compare(List<User> dolList, Map<String, List<User>> map)
private static List<Map<String, List<User>>> compare(List<User> dolList, Map<String, List<User>> map) {
List<Map<String, List<User>>> tempList = new ArrayList<Map<String, List<User>>>();
if (null != map) {
for (String key : map.keySet()) {
List<User> u = map.get(key);
List comList = new ArrayList();
boolean comFlag = true;
for (User us : u) {
List comList1 = new ArrayList();
for (User ul : dolList) {
if (us.getGroupNO() == ul.getGroupNO() && us.getName().trim().equals(ul.getName().trim())) {
comList1.add(1);
} else {
comList1.add(0);
}
}
if (comList1.contains(1)) {
// name has existed.
comList.add(1);
} else {
comList.add(0);
}
}
if (comList.contains(0)) {
comFlag = false;
}
// print out the match result in the console.
printMatchResult(tempList, key, u, comFlag);
}
}else{
System.out.println("map is null!");
}
return tempList;
}
在这个方法中,这里使用了两个List(即:comList, comList1)来记录是否完全匹配。
========================================================
多读一些书,英语很重要。
More reading,and english is important.
I'm Hongten

========================================================
java中的List记录是否完全匹配方法的更多相关文章
- JAVA 中LinkedHashMap要点记录
JAVA 中LinkedHashMap要点记录 构造函数中可能出现的几个参数说明如下: 1.initialCapacity 初始容量大小,使用无参构造方法时,此值默认是16 2.loadFactor ...
- (转载)java中判断字符串是否为数字的方法的几种方法
java中判断字符串是否为数字的方法: 1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < ...
- Java中获取键盘输入值的三种方法
Java中获取键盘输入值的三种方法 Java程序开发过程中,需要从键盘获取输入值是常有的事,但Java它偏偏就没有像c语言给我们提供的scanf(),C++给我们提供的cin()获取键盘输入值 ...
- Java中Date和Calender类的使用方法
查看文章 Java中Date和Calender类的使用方法 2009-10-04 20:49 Date和Calendar是Java类库里提供对时间进行处理的类,由于日期在商业逻辑的应用中占据着 ...
- JAVA中文件与Byte数组相互转换的方法
JAVA中文件与Byte数组相互转换的方法,如下: public class FileUtil { //将文件转换成Byte数组 public static byte[] getBytesByFile ...
- java中什么是bridge method(桥接方法)
java中什么是bridge method(桥接方法) https://blog.csdn.net/z69183787/article/details/81115524
- slf4j+log4j在Java中实现日志记录
小Alan今天来跟大家聊聊开发中既简单又常用但必不可少的一样东西,那是什么呢?那就是日志记录,日志输出,日志保存. 后面就统一用日志记录四个字来形容啦. 日志记录是项目的开发中必不可少的一个环节,特别 ...
- JAVA 中日志的记录于使用
java中常用的日志框架 日志接口 Commons Logging Apache Commons Logging是一个基于Java的日志记录实用程序,是用于日志记录和其他工具包的编程模型.它通过其他一 ...
- JAVA中一些需要记录的知识点(进阶部分)···持续更新
1.JAVA中的相对路径 file = new file("")与file = new file("./")方式相同,该路径为整个project的根目录(实际上 ...
随机推荐
- 图结构练习——最短路径(floyd算法(弗洛伊德))
图结构练习——最短路径 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 给定一个带权无向图,求节点1到节点n的最短路径. 输 ...
- POJ2778 DNA Sequence(AC自动机 矩阵)
先使用AC自动机求得状态转移关系,再建立矩阵,mat[i][j]表示一步可从i到j且i,j节点均非终止字符的方案数,则此矩阵的n次方表示n步从i,到j的方法数. #include<cstdio& ...
- .NET Nancy 详解(四) Self Host
Self Host 使得Nancy 能够在任意application 中启动,无论是console 还是windows service.这期我们使用的版本是Nancy v0.4.0. Demo 首先看 ...
- sublime text 2 安装emmet插件
一.添加插件之前先 下载Package Control 按 Ctrl+`(就是~这个键) 复制下面的代码 确认 重新启动sublime text2 import urllib2,os;pf='Pack ...
- set[c++]
#include <iostream> using namespace std; #include <set> int main(int argc, const char * ...
- FAST特征点检测
Features From Accelerated Segment Test 1. FAST算法原理 博客中已经介绍了很多图像特征检测算子,我们可以用LoG或者DoG检测图像中的Blobs(斑点检测) ...
- Understanding, Operating and Monitoring Apache Kafka
Apache Kafka is an attractive service because it's conceptually simple and powerful. It's easy to un ...
- DataTables - 问题集
1.增加额外搜索条件 var reqData = {}; var extraSearch = []; var oTable = $('table selector').dataTable({ 'aja ...
- light oj 1422 Halloween Costumes (区间dp)
题目链接:http://vjudge.net/contest/141291#problem/D 题意:有n个地方,每个地方要穿一种衣服,衣服可以嵌套穿,一旦脱下的衣服不能再穿,除非穿同样的一件新的,问 ...
- poj3642 01背包
http://poj.org/problem?id=3624 #include<iostream> #include<cstdio> #include<algorithm ...