Reputation: 191
I have next Java class:
public class Process {
private String id;
private String name;
private Date date;
private String status;
// getterr and setters and constructor
In main class I need to inicialize objects like this:
List<Process> processList = new ArrayList<>();
Process process1 = new Process();
process1.setId("1111");
process1.setName("Step one under first status");
process1.setDate(new Date());
process1.setStatus("");
Process process2 = new Process();
process2.setId("1111");
process2.setName("Step two under first status");
process2.setDate(new Date());
process2.setStatus("");
Process process3 = new Process();
process3.setId("1111");
process3.setName("");
process3.setDate(new Date());
process3.setStatus("Status One");
Process process4 = new Process();
process4.setId("1111");
process4.setName("Step one under second status");
process4.setDate(new Date());
process4.setStatus("");
Process process5 = new Process();
process5.setId("1111");
process5.setName("");
process5.setDate(new Date());
process5.setStatus("Status two");
Process process6 = new Process();
process6.setId("2222");
process6.setName("Step one under first status");
process6.setDate(new Date());
process6.setStatus("");
Process process7 = new Process();
process7.setId("2222");
process7.setName("Step two under first status");
process7.setDate(new Date());
process7.setStatus("");
Process process8 = new Process();
process8.setId("2222");
process8.setName("");
process8.setDate(new Date());
process8.setStatus("Status One");
processList.add(process1);
processList.add(process2);
processList.add(process3);
processList.add(process4);
processList.add(process5);
processList.add(process6);
processList.add(process7);
processList.add(process8);
From that list i need to get a new list with objects like this:
first object (id = "1111", name = "Step one under first status", date, "Status One")
second object (id = "1111", name = "Step two under first status", date, "Status One")
third object(id = "1111", name = "Step one under second status", date, "Status two")
fourth object(id = "2222", name = "Step one under first status", date, "Status One")
fifth object(id = "2222", name = "Step two under first status", date, "Status One")
Thank you for help and sorry for bad English (it's not my native language) :)
EDIT:
Hi. I dont need to remove items. I need to combine them (with foreach loop for example) to get items with all data. For example: process1 (with id = "1111") have name but it doesnt have status. process3 (id ="1111") have status "Status One" but doesnt have name. So the result of that need to be: first object (id = "1111", name = "Step one under first status", date, "Status One")
process2 (id = "1111") also have name, but doesnt have status. So i need to combine them with process3 (id ="1111"). The result will be: second object (id = "1111", name = "Step two under first status", date, "Status One")...
etc.
process7 have another id = "2222". It have a name = "Step two under first status" but doesnt hava a status, so i need to combine them with process8 (id ="2222") who's got status. Result of that will be: fifth object(id = "2222", name = "Step two under first status", date, "Status One")
Upvotes: 0
Views: 80
Reputation: 491
ok thanks for the clarification , it seems pretty easy :
List<Process> resultList = new ArrayList<Process>();
Iterator<Process> i = processList.iterator();
while (i.hasNext()) {
Process p = i.next(); // must be called before you can call i.remove()
if(p.getStatus().isEmpty()) {
for (Process currentProcess : processList) {
if(!currentProcess.getStatus().isEmpty()) {
p.setStatus(currentProcess.getStatus());
resultList.add(p);
break;
}
}
}
i.remove();
}
System.out.println(resultList);
Then you should have :
Process [name=Step one under first status, id=1111, date=Tue Feb 09 19:32:04 CET 2021, status=Status One], Process [name=Step two under first status, id=1111, date=Tue Feb 09 19:32:04 CET 2021, status=Status One], Process [name=Step one under second status, id=1111, date=Tue Feb 09 19:32:04 CET 2021, status=Status two], Process [name=Step one under first status, id=2222, date=Tue Feb 09 19:32:04 CET 2021, status=Status One], Process [name=Step two under first status, id=2222, date=Tue Feb 09 19:32:04 CET 2021, status=Status One]
Upvotes: 0
Reputation: 491
I'm not sure to understand what you mean , but if you want to remove some items (Status = "")from your list :
processList.removeIf(e -> e.getStatus().equals("");
Hi. I dont need to remove items. I need to combine them (with foreach loop for example) to get item with all data. For example: process1 (with id = "1111#) have name but it doesnt have status. process3 have status "Status One" but doesnt have name. So the result of that need to be: first object (id = "1111", name = "Step one under first status", date, "Status One")
process2 (id = "1111") also have name, but doesnt have status. So i need to combine them with process3. The result will be: second object (id = "1111", name = "Step two under first status", date, "Status One")...
etc.
process7 have another id = "2222". It have a name = "Step two under first status" but doesnt hava a status, so i need to combine them with process8 who's got status. Result of that will be: fifth object(id = "2222", name = "Step two under first status", date, "Status One")
Upvotes: 1