Store the Java object received in table format in the related model

About this article

Receives tabular objects from the database in Java I was sick when storing that object in a structured object ...

A memo of the solution that doubles as a memorandum.

Introduction

Publisher name(Publisher) Genre(Genre) Books(Book)
Company A programming Basic summary
Company A programming Even monkeys can understand
Company A Shogi Basic summary
Company A Shogi Even monkeys can understand
Company B programming Basic summary
Company B Shogi Even monkeys can understand
Company A programming Out of order

I got this kind of table data from BD

|
|--Company A---------programming---------Basic summary
|           |                    |
|           |                    |----Even monkeys can understand
|           |                    |
|           |                    |----Out of order
|           |    
|           |        
|           |----Shogi------------Basic summary
|                                |
|                                 |----Even monkeys can understand
|    
|    
|--Company B---------programming---------Basic summary
|           |                    
|           |----Shogi---------Basic summary

I want to map to a Java object like this.

When you select a publisher, the corresponding genre will appear, When you select a genre, the corresponding book information will come out ...

Finally I want a list of publishers.

Java object

Java object to get table information

BookResultMap


package dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class BookResultMap {
	private String publisherName;
	private String genreName;
	private String bookTitle;

	public BookResultMap() {}

	public BookResultMap(String publisherName, String genreName, String bookTitle) {
		this.publisherName = publisherName;
		this.genreName = genreName;
		this.bookTitle = bookTitle;
	}
}

Java object that finally stores the data

Book: A class that represents book information. Genre: A class that represents a genre. It has List as an element. Publisher: A class that represents publisher information. It has List as an element.

Book.java


package model;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Book {
	private String bookTitle;
}

Genre.java


package model;

import java.util.List;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Genre {
	private String genreName;
	private List<Book> bookList;
}

Publisher.java


package model;

import java.util.List;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class Publisher {
	private String publisherName;
	private List<Genre> genreList;
}

Implementation

DataConvert.java


package logic;

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

import dto.BookResultMap;
import model.Book;
import model.Genre;
import model.Publisher;

public class DataConvert {
    public static List<Publisher> convertObject(List<BookResultMap> bookResultMaps) {

		/*A map that stores each relationship.
		 *The value of the map with publishName as the key,
		 *Set to the map with genreName as the key.
		*/
		Map<String, Map<String, List<Book>>> convertMap = new HashMap<>();
		for(int i = 0; i < bookResultMaps.size(); i++) {
			String publisherName = bookResultMaps.get(i).getPublisherName();
			String genreName = bookResultMaps.get(i).getGenreName();
			Book book = new Book();
			book.setBookTitle(bookResultMaps.get(i).getBookTitle());

			/*1.If publisherName doesn't exist as a key in convertMap
			 *Create a HashMap with publishName as the key in convertMap.
			*/
			if(!convertMap.containsKey(publisherName)) {
				convertMap.put(publisherName, new HashMap<>());
			}
			
			/*2.If geneName is convertMap.get(publisherName)If it didn't exist as a key to
			 * convertMap.get(publisherName)Create a HashMap with publishName as the key.
			*/
			if(!convertMap.get(publisherName).containsKey(genreName)) {
				convertMap.get(publisherName).put(genreName, new ArrayList<>());
			}
			/*1,By doing the work of 2
			 *Because you can get the ArrayList in the Genre related to the publisherName
			 *Store the Book there.
			 * */
			convertMap.get(publisherName).get(genreName).add(book);
		}

		//Replace the information stored in convertMap with a Java object.
		List<Publisher> publisherList = new ArrayList<>();
		for(String publisherName: convertMap.keySet()) {
			Publisher publisher = new Publisher();
			List<Genre> genreList = new ArrayList<>();
			publisher.setPublisherName(publisherName);
			publisher.setGenreList(genreList);
			publisherList.add(publisher);

			for(String genreName: convertMap.get(publisherName).keySet()) {
				Genre genre = new Genre();
				genre.setGenreName(genreName);
				genre.setBookList(convertMap.get(publisherName).get(genreName));
				genreList.add(genre);
			}
		}

		return publisherList;
	}
}


Verification

input

DataConvert.java


public class DataConvert {

	public static void main(String[] args) {

		//Object received as is from the database in the form of a table
		List<BookResultMap> bookResultMaps = new ArrayList<>();
		BookResultMap brm1_1_1 = new BookResultMap("Company A", "programming", "Basic summary");
		BookResultMap brm1_1_2 = new BookResultMap("Company A", "programming", "Even monkeys can understand");
		BookResultMap brm1_2_1 = new BookResultMap("Company A", "Shogi", "Basic summary");
		BookResultMap brm1_2_2 = new BookResultMap("Company A", "Shogi", "Even monkeys can understand");
		BookResultMap brm2_1_1 = new BookResultMap("Company B", "programming", "Basic summary");
		BookResultMap brm2_2_1 = new BookResultMap("Company B", "Shogi", "Basic summary");
		BookResultMap brm1_1_3 = new BookResultMap("Company A", "programming", "Out of order");
		bookResultMaps.add(brm1_1_1);
		bookResultMaps.add(brm1_1_2);
		bookResultMaps.add(brm1_2_1);
		bookResultMaps.add(brm1_2_2);
		bookResultMaps.add(brm2_1_1);
		bookResultMaps.add(brm2_2_1);
		bookResultMaps.add(brm1_1_3);

		//Data conversion
		List<Publisher> publisherList = convertObject(bookResultMaps);

		//Verification
		System.out.println("publisherList size:" + publisherList.size());
		for(Publisher publisher: publisherList) {
			System.out.println("-------------------"+ publisher.getPublisherName() +"-----------------------");
			System.out.println("size of genreList:" + publisher.getGenreList().size());
			for(Genre genre: publisher.getGenreList()) {
				System.out.println("-------"+ genre.getGenreName() +"--------");
				System.out.println("bookList size:"+  + genre.getBookList().size());
				for(Book book: genre.getBookList()) {
					System.out.println("----");
					System.out.println(publisher.getPublisherName());
					System.out.println(genre.getGenreName());
					System.out.println(book.getBookTitle());
				}
			}
		}
	}
}

Output result

publisherList size:2
-------------------Company B-----------------------
size of genreList:2
-------programming--------
bookList size:1
----
Company B
programming
Basic summary
-------Shogi--------
bookList size:1
----
Company B
Shogi
Basic summary
-------------------Company A-----------------------
size of genreList:2
-------programming--------
bookList size:3
----
Company A
programming
Basic summary
----
Company A
programming
Even monkeys can understand
----
Company A
programming
Out of order
-------Shogi--------
bookList size:2
----
Company A
Shogi
Basic summary
----
Company A
Shogi
Even monkeys can understand

I was able to associate this information with the publisher and genre.

At the end

I think there are other ways, but for the time being, I wrote my own solution. If you want to keep the order of the table, you can change HashMap to LinkedHashMap. I would be grateful if you could let me know if there are any better methods or improvements.

By sorting by SQL or changing the structure, I want you to get the table data in a form that is easier to process ...

Recommended Posts

Store the Java object received in table format in the related model
Format XML in Java
twitter4j java Store the searched tweets in DB (MySQL).
[Java] I want to perform distinct with the key in the object
Access the network interface in Java
Implement Table Driven Test in Java 14
Guess the character code in Java
Specify the java location in eclipse.ini
Unzip the zip file in Java
Parsing the COTOHA API in Java
Call the super method in Java
Get the result of POST in Java
Check the contents of the Java certificate store
Java reference to understand in the figure
Try using the Stream API in Java
Call the Windows Notification API in Java
About the new Java release model @ Seki Java (2018/07/20)
I tried the new era in Java
Organized memo in the head (Java --Array)
Java creates a table in a Word document
Try using JSON format API in Java
Try calling the CORBA service in Java 11+
Output Date in Java in ISO 8601 extended format
What is the main method in Java?
Immutable (immutable) List object conversion function in Java8
How to get the date in java
The story received by Java SE11 silver
The story of writing Java in Emacs
Console input in Java (understanding the mechanism)
I received the data of the journey (diary application) in Java and visualized it # 001