[JAVA] A memo that was unexpectedly difficult even though it seemed easy to exchange between two Entity

It was difficult or long

It's a complete memorandum

There are two models Eliminate the data that PK is wearing Keep the rest of the data as a list

package test;

public class Models {
	class Model1 {
		private String id;
		private String name;

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
	}

	class Model2 {
		private String id;
		private String name;

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
	}
}

package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import test.Models.Model1;
import test.Models.Model2;

/**
 * test
 *
 * @author me
 *
 */
public class Test2 {

	/**
	 * main
	 * @param args
	 */
	public static void main(String[] args) {
		distinct();
	}

	/**
	 *The guy who eliminates PK fog from the model
	 */
	private static void distinct() {
		Models models = new Models();
		List<Model1> list1 = Collections.synchronizedList(new ArrayList<Model1>());
		List<Model2> list2 = Collections.synchronizedList(new ArrayList<Model2>());
		Models.Model1 model1 = models.new Model1();
		model1.setId("1");
		model1.setName("asdasdasdas");
		list1.add(model1);
		Models.Model1 model11 = models.new Model1();
		model11.setId("2");
		model11.setName("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
		list1.add(model11);
		Models.Model2 model2 = models.new Model2();
		model2.setId("1");
		list2.add(model2);
		Models.Model2 model22 = models.new Model2();
		model22.setId("2");
		list2.add(model22);
		Models.Model2 model222 = models.new Model2();
		model222.setId("3");
		list2.add(model222);

		/** -----This function----- */
		//PK list of list1 excluding the ones that list2 and PK are wearing
		List<String> uniKeyList = list1.stream().map(data1 -> data1.getId())
				.filter(key1 -> list2.stream().map(data2 -> data2.getId()).anyMatch(key2 -> key1.equals(key2)))
				.collect(Collectors.toList());
		//Search list1 with uniKeyList
		List<Model1> uniData = list1.stream()
				.filter(data -> uniKeyList.stream().anyMatch(key -> data.getId().equals(key)))
				.collect(Collectors.toList());
		System.out.println(uniData);

	}

}

First, make a list of PKs without fog

Search from the list of original models

I took the step of losing the data of the cover PK

Absolutely wasteful ... I think we should be able to make it smarter

Recommended Posts

A memo that was unexpectedly difficult even though it seemed easy to exchange between two Entity
A memo that was soberly addicted to the request of multipart / form-data
Created a library that makes it easy to handle Android Shared Prefences
It was surprisingly easy. How to update user information without entering a password