I have a random unsorted ArrayList of objects A, which have common fields with object B, and another ArrayList of objects B.
I want to order the elements in ArrayList of objects A based on those common fields.
My code:
public class Protocol {
@Override
public String toString() {
return "Protocol [referenceName=" + referenceName + ", value=" + value + "]";
}
private String referenceName = "314596";
private String value = "12345";
public Protocol(String referenceName, String value) {
super();
this.referenceName = referenceName;
this.value = value;
}
public String getValue() {
return value;
}
public String getReferenceName() {
return referenceName;
}
// other stuff
}
public class Sensor {
private String referenceName = "314596";
private String value = "12345";
public Sensor(String referenceName, String value) {
this.referenceName = referenceName;
this.value = value;
}
public String getValue() {
return value;
}
public String getReferenceName() {
return referenceName;
}
@Override
public String toString() {
return "Sensor [referenceName=" + referenceName + ", value=" + value + "]";
}
// other stuff
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class SortingTest {
public static void main(String[] args) {
Protocol protocol1 = new Protocol("31543678", "09866546534");
Protocol protocol2 = new Protocol("343443678", "09866567897874334");
Protocol protocol3 = new Protocol("41563678", "0663846546534");
Protocol protocol4 = new Protocol("9876543", "009876546546534");
List<Protocol> protocolList = new ArrayList<>();
protocolList.add(protocol4);
protocolList.add(protocol1);
protocolList.add(protocol3);
protocolList.add(protocol2);
for (int i =0; i < protocolList.size(); i++) {
System.out.println(protocolList.get(i));
}
System.out.println("*******************************************************");
Sensor sensor1 = new Sensor("31543678", "09866546534");
Sensor sensor2 = new Sensor("343443678", "09866567897874334");
Sensor sensor3 = new Sensor("41563678", "0663846546534");
Sensor sensor4 = new Sensor("9876543", "009876546546534");
List<Sensor> sensorList = new ArrayList<>();
sensorList.add(sensor1);
sensorList.add(sensor3);
sensorList.add(sensor2);
sensorList.add(sensor4);
List<Sensor> sensorList1 = new ArrayList<>(sensorList);
for(int i =0; i < sensorList.size(); i++) {
System.out.println(sensorList.get(i));
}
System.out.println("*******************************************************");
for (int i = 0; i < sensorList.size(); i++) {
for (int j = 0; j < protocolList.size(); j++) {
if (sensorList.get(i).getReferenceName() == protocolList.get(j).getReferenceName()) {
if (sensorList.get(i).getValue() == protocolList.get(j).getValue()) {
sensorList1.set(j, sensorList.get(i));
}
}
}
}
for (int i = 0; i < sensorList1.size(); i++) {
System.out.println(sensorList1.get(i));
}
}
}
Expected output :
Protocol [referenceName=9876543, value=009876546546534]
Protocol [referenceName=31543678, value=09866546534]
Protocol [referenceName=41563678, value=0663846546534]
Protocol [referenceName=343443678, value=09866567897874334]
*******************************************************
Sensor [referenceName=31543678, value=09866546534]
Sensor [referenceName=41563678, value=0663846546534]
Sensor [referenceName=343443678, value=09866567897874334]
Sensor [referenceName=9876543, value=009876546546534]
*******************************************************
Sensor [referenceName=9876543, value=009876546546534]
Sensor [referenceName=31543678, value=09866546534]
Sensor [referenceName=41563678, value=0663846546534]
Sensor [referenceName=343443678, value=09866567897874334]
Based on the common fields, I want to sort ObjectBList so that the sequence matches ObjectAList.
This logic works correctly, but I feel there might some faster or easier way to do this.
Only issue with the map approach is :
Given:
Protocol [referenceName=343443678, value=09866567897874334, random=TEST]
Protocol [referenceName=9876543, value=009876546546534, random=TEST]
Protocol [referenceName=31543678, value=09866546534, random=TEST]
Protocol [referenceName=41563678, value=0663846546534, random=TEST]
Protocol [referenceName=343443678, value=09866567897874334, random=TEST]
Protocol [referenceName=343443678, value=09866567897874334, random=TEST]
Sensor [referenceName=31543678, value=09866546534]
Sensor [referenceName=41563678, value=0663846546534]
Sensor [referenceName=343443678, value=09866567897874334]
Sensor [referenceName=9876543, value=009876546546534]
Sensor [referenceName=343443678, value=09866567897874334]
Sensor [referenceName=343443678, value=09866567897874334]
Sensor [referenceName=41563678, value=0663846546534]
Sensor [referenceName=41563678, value=0663846546534]
Sensor [referenceName=67534567, value=66565656]
expected output:
Sensor [referenceName=343443678, value=09866567897874334]
Sensor [referenceName=9876543, value=009876546546534]
Sensor [referenceName=31543678, value=09866546534]
Sensor [referenceName=41563678, value=0663846546534]
Sensor [referenceName=343443678, value=09866567897874334]
Sensor [referenceName=343443678, value=09866567897874334]
Sensor [referenceName=41563678, value=0663846546534]
Sensor [referenceName=41563678, value=0663846546534]
Sensor [referenceName=67534567, value=66565656]
Output:
Sensor [referenceName=9876543, value=009876546546534]
Sensor [referenceName=67534567, value=66565656]
Sensor [referenceName=31543678, value=09866546534]
Sensor [referenceName=41563678, value=0663846546534]
Sensor [referenceName=41563678, value=0663846546534]
Sensor [referenceName=41563678, value=0663846546534]
Sensor [referenceName=343443678, value=09866567897874334]
Sensor [referenceName=343443678, value=09866567897874334]
Sensor [referenceName=343443678, value=09866567897874334]
The issue is it's grouping the same objects at the end not really preserving the order for duplicates.
One more thing would it work in a case where: protocols has suppose 4 fields instead of two referenceName and value as well as referenceName1 and value1. And Sensors has same as before two. Example: Protocol(String referenceName,String value,String referenceName1 and String value1) And Sensor(String referenceName,String value)
Sensor can match protocols with combination of (referenceName and value) or (referenceName1 and value1). Would it work if pass those two fields into getKey method? There will be always a match between sensors and protocols either on combination of (referenceName and value) or (referenceName1 and value1) bearing in mind that either of them can be null.
==, always compare them withequals. (The actual problem can be solved in linear time and space by creating a map from the field value to the "sensor", then going through the "protocols", look up the "sensor" for that respective field value in the map, and put that sensor into a list. Apparently, there might be two field values here, so you might need aPairas the key, but that's a detail)I feel there might some faster ...- That's right, we can make it faster. Your code is based on a brute-force approach and has a time complexity of O(n ^ 2). It can be improved by indexing the positions of in the list ofProtocolobjects. I will share more details in a minute.