I have two list of data one list contain only id, and second list contains all the details of those id, but second list contain many other id details that not present in first list, I want to store all the id details to create a third list as per list one then after store remaining id details. but I tried many time i am not able to store remaining id details in third list. Here below my code what I tried to solve this issue. StoreDragData.java
package task;
import java.util.ArrayList;
import java.util.List;
public class StoreDragData {
private int id;
StoreDragData() {
}
public StoreDragData(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<StoreDragData> demo() {
List<StoreDragData> ll = new ArrayList<>();
ll.add(new StoreDragData(6));
ll.add(new StoreDragData(4));
ll.add(new StoreDragData(7));
ll.add(new StoreDragData(1));
ll.add(new StoreDragData(5));
ll.add(new StoreDragData(2));
ll.add(new StoreDragData(3));
return ll;
}
}
ServerList.java
package task;
import java.util.ArrayList;
import java.util.List;
public class ServerList {
private int id;
private String name;
private String subject;
ServerList() {
}
public ServerList(int id, String name, String subject) {
this.id = id;
this.name = name;
this.subject = subject;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public static List<ServerList> addStudentDetails() {
List<ServerList> lls = new ArrayList<>();
lls.add(new ServerList(1, "alex", "Math"));
lls.add(new ServerList(2, "andrew", "Physic"));
lls.add(new ServerList(3, "alok", "Chemistry"));
lls.add(new ServerList(4, "Boby", "Botany"));
lls.add(new ServerList(5, "Soumya", "Zoology"));
lls.add(new ServerList(6, "Rwitik", "Ornithology"));
lls.add(new ServerList(7, "Bikash", "Behavior of Money"));
lls.add(new ServerList(8, "Sipun", "Economy"));
lls.add(new ServerList(9, "Suresh", "CS"));
return lls;
}
}
BigTask.java
package task;
import java.util.ArrayList;
import java.util.List;
public class Bigtask {
public static void main(String[] args) {
StoreDragData sdd = new StoreDragData();
System.out.print(" Student id record");
sdd.demo();
System.out.println("=========================================");
for (StoreDragData st:sdd.demo()){
System.out.println(st.getId() );
}
System.out.println("=========================================");
ServerList std = new ServerList();
System.out.println(" Student record details");
ServerList.addStudentDetails();
for (ServerList st : ServerList.addStudentDetails()) {
System.out.println(st.getId() + " " + st.getName() + " " + st.getSubject());
}
System.out.println("=========================");
System.out.println(" Final Result ");
searchData(sdd.demo(), ServerList.addStudentDetails());
System.out.println("---------------------------");
remainingRecord(std);
}
static void searchData(List<StoreDragData> userList, List<ServerList> studentList) {
List<ServerList> finalList = new ArrayList();
for (int i = 0; i < userList.size(); i++) {
Integer existIndex = isDataExist(userList.get(i).getId());
if (existIndex != -1) {
finalList.add(studentList.get(existIndex));
}
}
/**print final list*/
for (ServerList st : finalList) {
System.out.println(st.getId() + " " + st.getName() + " " + st.getSubject());
}
}
static Integer isDataExist(Integer id) {
List<ServerList> list = (new ServerList()).addStudentDetails();
int index = -1;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId() == id) {
index = i;
break;
}
}
return index;
}
static ServerList remainingRecord(ServerList std){
List<ServerList> finalList = (new ServerList()).addStudentDetails();//final list
//Student details list
for (ServerList sls : finalList){
if (!finalList.contains(sls)){
System.out.println("Hai Iam unique");
}else{
System.out.println("Hai Iam duplicate");
}
}
return std;
}
static void search(){}
/**Function for check exist id*/
public Integer isExistIdInFinalList(Integer id){
List<ServerList> findList = (List<ServerList>) new ServerList();
int index=-1;
for (int i=0;i< findList.size();i++){
if(findList.get(i).getId() == id){
index =i;
break;
}
}
return index;
}
}
Output of my code is
Student id record=========================================
6
4
7
1
5
2
3
=========================================
Student record details
1 alex Math
2 andrew Physic
3 alok Chemistry
4 Boby Botany
5 Soumya Zoology
6 Rwitik Ornithology
7 Bikash Behavior of Money
8 Sipun Economy
9 Suresh CS
=========================
Final Result
6 Rwitik Ornithology
4 Boby Botany
7 Bikash Behavior of Money
1 alex Math
5 Soumya Zoology
2 andrew Physic
3 alok Chemistry
---------------------------
I get the result in final list as per student id list one , but I miss the id 7,8,9 details in final list. First I want to store id details records as per list one in final result then store remaining items. how can I do it.