`
DavyJones2010
  • 浏览: 154265 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Java SE: A Tip for Joining Two Table Together in Java

阅读更多

1. Requirement Specification

    1) After we have loading data from two different file, and then assembly them into two different kind of JavaBean.

    2) We want to join the two list of JavaBean together into a single list of bean using the foreign key.

 

1. The first kind of JavaBean

package edu.xmu.tablejoin.TableJoin_Vo;

public class TableOneVo
{
	private String category;
	private String fdl;
	private String bu;
	private String frsValue;

	public TableOneVo()
	{
	}

	public TableOneVo(String category, String fdl, String bu, String frsValue)
	{
		super();
		this.category = category;
		this.fdl = fdl;
		this.bu = bu;
		this.frsValue = frsValue;
	}

	public String getCategory()
	{
		return category;
	}

	public void setCategory(String category)
	{
		this.category = category;
	}

	public String getFdl()
	{
		return fdl;
	}

	public void setFdl(String fdl)
	{
		this.fdl = fdl;
	}

	public String getBu()
	{
		return bu;
	}

	public void setBu(String bu)
	{
		this.bu = bu;
	}

	public String getFrsValue()
	{
		return frsValue;
	}

	public void setFrsValue(String frsValue)
	{
		this.frsValue = frsValue;
	}

}

 

2. The second of JavaBean

package edu.xmu.tablejoin.TableJoin_Vo;

public class TableTwoVo
{
	private String category;
	private String fdl;
	private String bu;
	private String leaValue;

	public TableTwoVo()
	{
	}

	public TableTwoVo(String category, String fdl, String bu, String leaValue)
	{
		this.category = category;
		this.fdl = fdl;
		this.bu = bu;
		this.leaValue = leaValue;
	}

	public String getCategory()
	{
		return category;
	}

	public void setCategory(String category)
	{
		this.category = category;
	}

	public String getFdl()
	{
		return fdl;
	}

	public void setFdl(String fdl)
	{
		this.fdl = fdl;
	}

	public String getBu()
	{
		return bu;
	}

	public void setBu(String bu)
	{
		this.bu = bu;
	}

	public String getLeaValue()
	{
		return leaValue;
	}

	public void setLeaValue(String leaValue)
	{
		this.leaValue = leaValue;
	}

}

 

3. The Key for Joining Two Table Together

package edu.xmu.tablejoin.TableJoin_Vo;

public class TableKey
{
	private String category;
	private String fdl;

	public TableKey()
	{
	}

	public TableKey(String category, String fdl)
	{
		this.category = category;
		this.fdl = fdl;
	}

	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((category == null) ? 0 : category.hashCode());
		result = prime * result + ((fdl == null) ? 0 : fdl.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TableKey other = (TableKey) obj;
		if (category == null)
		{
			if (other.category != null)
				return false;
		} else if (!category.equals(other.category))
			return false;
		if (fdl == null)
		{
			if (other.fdl != null)
				return false;
		} else if (!fdl.equals(other.fdl))
			return false;
		return true;
	}

	public String getCategory()
	{
		return category;
	}

	public void setCategory(String category)
	{
		this.category = category;
	}

	public String getFdl()
	{
		return fdl;
	}

	public void setFdl(String fdl)
	{
		this.fdl = fdl;
	}
}

 

4. The Third kind of JavaBean for Storing The Union of Joining the two table together

package edu.xmu.tablejoin.TableJoin_Vo;

public class TableUnionVo
{
	private String category;
	private String fdl;
	private String bu;
	private String frsValue;
	private String leaValue = "-";

	public TableUnionVo()
	{
	}

	public TableUnionVo(String category, String fdl, String bu,
			String frsValue, String leaValue)
	{
		super();
		this.category = category;
		this.fdl = fdl;
		this.bu = bu;
		this.frsValue = frsValue;
		this.leaValue = leaValue;
	}

	public String getCategory()
	{
		return category;
	}

	public void setCategory(String category)
	{
		this.category = category;
	}

	public String getFdl()
	{
		return fdl;
	}

	public void setFdl(String fdl)
	{
		this.fdl = fdl;
	}

	public String getBu()
	{
		return bu;
	}

	public void setBu(String bu)
	{
		this.bu = bu;
	}

	public String getFrsValue()
	{
		return frsValue;
	}

	public void setFrsValue(String frsValue)
	{
		this.frsValue = frsValue;
	}

	public String getLeaValue()
	{
		return leaValue;
	}

	public void setLeaValue(String leaValue)
	{
		this.leaValue = leaValue;
	}

	@Override
	public String toString()
	{
		return "TableGlobalVo [category=" + category + ", fdl=" + fdl + ", bu="
				+ bu + ", frsValue=" + frsValue + ", leaValue=" + leaValue
				+ "]";
	}

}

 

5.  The Main Function of Joining the Two Table Together into One Single Table

package edu.xmu.tablejoin.TableJoin_Core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import edu.xmu.tablejoin.TableJoin_Vo.TableUnionVo;
import edu.xmu.tablejoin.TableJoin_Vo.TableKey;
import edu.xmu.tablejoin.TableJoin_Vo.TableOneVo;
import edu.xmu.tablejoin.TableJoin_Vo.TableTwoVo;

public class App
{
	public List<TableUnionVo> joinTable(List<TableOneVo> tableOneVoList,
			List<TableTwoVo> tableTwoVoList)
	{
		Map<TableKey, TableUnionVo> globalMap = new HashMap<TableKey, TableUnionVo>();

		for (TableOneVo tableOneVo : tableOneVoList)
		{
			String tableOneCategory = tableOneVo.getCategory();
			String tableOneFdl = tableOneVo.getFdl();
			String tableOneBu = tableOneVo.getBu();
			String tableOneFrsValue = tableOneVo.getFrsValue();

			TableUnionVo globalVo = new TableUnionVo();
			globalVo.setCategory(tableOneCategory);
			globalVo.setFdl(tableOneFdl);
			globalVo.setBu(tableOneBu);
			globalVo.setFrsValue(tableOneFrsValue);

			TableKey key = new TableKey();
			key.setCategory(tableOneCategory);
			key.setFdl(tableOneFdl);

			globalMap.put(key, globalVo);
		}

		for (TableTwoVo tableTwoVo : tableTwoVoList)
		{
			String tableTwoCategory = tableTwoVo.getCategory();
			String tableTwoFdl = tableTwoVo.getFdl();
			String tableTwoLeaValue = tableTwoVo.getLeaValue();

			TableKey key = new TableKey();
			key.setCategory(tableTwoCategory);
			key.setFdl(tableTwoFdl);

			TableUnionVo globalVo = null;

			if (null != (globalVo = globalMap.get(key)))
			{
				globalVo.setLeaValue(tableTwoLeaValue);
			}
		}

		Set<TableKey> keySet = globalMap.keySet();
		List<TableUnionVo> tableGlobalVoList = new ArrayList<TableUnionVo>();
		for (TableKey key : keySet)
		{
			tableGlobalVoList.add(globalMap.get(key));
		}

		return tableGlobalVoList;
	}
}

 

6. The Test Case for Validating the main function works.

package edu.xmu.tablejoin.TableJoin_Core;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import edu.xmu.tablejoin.TableJoin_Vo.TableUnionVo;
import edu.xmu.tablejoin.TableJoin_Vo.TableOneVo;
import edu.xmu.tablejoin.TableJoin_Vo.TableTwoVo;

public class AppTest
{
	@Test
	public void testJoin()
	{
		TableOneVo tableOneVo1 = new TableOneVo("AAA", "aaa", "111", "111");
		TableOneVo tableOneVo2 = new TableOneVo("AAA", "bbb", "111", "222");
		TableOneVo tableOneVo3 = new TableOneVo("AAA", "ccc", "111", "222");
		TableOneVo tableOneVo4 = new TableOneVo("BBB", "aaa", "111", "123");
		TableOneVo tableOneVo5 = new TableOneVo("BBB", "bbb", "222", "333");
		TableOneVo tableOneVo6 = new TableOneVo("CCC", "aaa", "333", "444");

		List<TableOneVo> tableOneVoList = new ArrayList<TableOneVo>();

		tableOneVoList.add(tableOneVo1);
		tableOneVoList.add(tableOneVo2);
		tableOneVoList.add(tableOneVo3);
		tableOneVoList.add(tableOneVo4);
		tableOneVoList.add(tableOneVo5);
		tableOneVoList.add(tableOneVo6);

		TableTwoVo tableTwoVo1 = new TableTwoVo("AAA", "aaa", "111", "111");
		TableTwoVo tableTwoVo2 = new TableTwoVo("AAA", "bbb", "111", "222");
		TableTwoVo tableTwoVo3 = new TableTwoVo("AAA", "ccc", "111", "222");
		TableTwoVo tableTwoVo4 = new TableTwoVo("BBB", "aaa", "111", "123");
		TableTwoVo tableTwoVo5 = new TableTwoVo("BBB", "bbb", "222", "333");
		TableTwoVo tableTwoVo6 = new TableTwoVo("DDD", "aaa", "333", "444");

		List<TableTwoVo> tableTwoVoList = new ArrayList<TableTwoVo>();

		tableTwoVoList.add(tableTwoVo1);
		tableTwoVoList.add(tableTwoVo2);
		tableTwoVoList.add(tableTwoVo3);
		tableTwoVoList.add(tableTwoVo4);
		tableTwoVoList.add(tableTwoVo5);
		tableTwoVoList.add(tableTwoVo6);

		App app = new App();
		List<TableUnionVo> tableGlobalVoList = app.joinTable(tableOneVoList,
				tableTwoVoList);

		for (TableUnionVo vo : tableGlobalVoList)
		{
			System.out.println(vo);
		}
	}
}

 

分享到:
评论

相关推荐

    Learn Java for Android Development 3rd Edition mobi格式

    Learn Java for Android Development, Third Edition, is an update of a strong selling book that now includes a primer on Android app development (in Chapter 1 and Appendix C, which is distributed in the...

    Learn Java for Android Development, 3rd Edition

    Learn Java for Android Development, 3rd Edition, is an update of a strong selling book that now includes a primer on Android app development (in Chapter 1 and Appendix C, which is distributed in the ...

    Learn.Java.for.Android.Development_Apress.2010+src

    Android development is hot, and many programmers are interested in joining the fun. However, because this technology is based on Java, you should first obtain a solid grasp of the Java language and ...

    java8实战:使用流收集数据之toList、joining、groupBy(多字段分组)

    在Java 8中,Stream API的引入为处理集合数据提供了新的、更为强大的方式。这篇实战指南将探讨如何利用Stream API的`toList`、`joining`和`groupBy`方法来高效地收集和操作数据。让我们深入了解一下这些方法以及它们...

    SCJP6 Sun Certificated Programmer for Java 6 Study Guide (Exam 310-065) 英文原版

    - **Joining Threads**: Waiting for a thread to complete before continuing. #### 10. Development The final chapter covers advanced topics related to software development: - **Unit Testing**: Writing...

    Java 8 Stream API中的`Collectors.joining()`:字符串连接的艺术

    Collectors.joining()是Java 8 Stream API中一个非常实用的工具,它为字符串连接提供了极大的灵活性。通过本文的详细介绍,你应该能够理解Collectors.joining()的工作原理,并能够在实际开发中灵活运用它。如果你有...

    Matlab Tutorial - 41 - Joining Matrices Together

    本教程“Matlab Tutorial - 41 - Joining Matrices Together”着重讲解了如何在Matlab环境中进行矩阵的连接操作。这些操作主要包括水平连接(Horizontal Concatenation)、垂直连接(Vertical Concatenation)以及堆...

    java基础面试题目,常见的几个java面试题目:说一下java类集;JDK 和 JRE 有什么区别

    .collect(Collectors.joining("")); ``` 总结一下,这些面试题目涵盖了Java基础知识的多个方面,包括类集框架的理解,Spring框架的工作原理,源码阅读的重要性,动态代理的实现,JDK与JRE的区别,==与equals的区别...

    ElasticSearch Java API 中文文档

    标题《ElasticSearch Java API 中文文档》表明本篇文档的主要内容是关于ElasticSearch的Java API的中文使用说明和相关知识点介绍。ElasticSearch是一个基于Lucene构建的开源搜索引擎,它提供了一个分布式、多用户...

    Java8 将List<Integer> 转换成以逗号分割的String字符串

    在Java编程中,将一个`List&lt;Integer&gt;`转换成以逗号分隔的`String`字符串是一种常见的需求,尤其是在处理数据展示或格式化输出时。Java 8引入了新的特性和方法,使得这种转换变得更加简洁和高效。下面我们将深入探讨...

    DB - A String Adaptive Hash Table for Analytical Databases.pdf

    Hash tables are the fundamental data structure for analytical database workloads, such as aggregation, joining, set filtering and records deduplication. The performance aspects of hash tables differ ...

    Spark_for_Python

    "Spark for Python Developers" is a comprehensive resource for anyone interested in leveraging Apache Spark's capabilities using Python. It covers the essential concepts and practical aspects of using ...

    String manipulation operations in java.zip

    本压缩包"String manipulation operations in java.zip"中的内容可能是一个关于Java字符串操作的项目或教程,其中特别提到了`underscore.string.java-master`这个子文件,暗示了它可能使用了`underscore.string`库来...

    Java线程:创建与启动

    Java线程是并发编程的核心部分,它允许程序同时执行多个任务。在Java中,有两种主要方式来创建线程:扩展`java.lang.Thread`类或实现`java.lang.Runnable`接口。 1. **扩展Thread类**: 当创建一个新的类继承自`...

    java插值.rar

    Java插值是一种编程技术,主要用于字符串模板处理,它允许开发者在字符串中动态插入变量或表达式的值。在Java中,虽然不像某些语言如Ruby或Python那样内置了原生的插值功能,但我们可以通过多种方式实现类似的效果。...

    java8stream源码-java8:Java8特性

    Java 8 是一个重要的Java平台版本,引入了许多创新特性,其中最引人注目的就是Stream API。Stream API为处理集合提供了全新的方式,使得数据处理更加高效、简洁且易读。让我们深入探讨一下Java 8 Stream API及其源码...

    groovy_in_action_draft_ch_01.pdf

    #### Groovy: A Feature-Rich Language for the Java Platform Groovy is a versatile programming language that runs on the Java Virtual Machine (JVM). It offers several advanced features such as closures...

    java实现的组播聊天室

    Java实现的组播聊天室是一种基于UDP协议的通信系统,它允许用户在特定的组内进行实时交流。本文将深入探讨这一系统的核心概念、设计原理以及实现细节。 首先,我们来了解一下UDP(User Datagram Protocol)协议。...

Global site tag (gtag.js) - Google Analytics