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.
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.
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;
}
}
Book: A class that represents book information.
Genre: A class that represents a genre. It has List
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;
}
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;
}
}
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());
}
}
}
}
}
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.
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