Jo Jo Roid Jo Jo Roid - 3 months ago 9
Android Question

Android - Loop and filter on List?

I have a

List
of data like this :

enter image description here

I need to get first and last records are same :

enter image description here

And my model is :

public class Modeltest {
private String lat;
private String lng;
private String date;
private String firstTime;
private String lasttime;
private String counts;
private String userCode;

public String getLat() {
return lat;
}

public void setLat(String lat) {
this.lat = lat;
}
.......
.....
.....
}


I can getting first and last :

List<Unprocessed_DistanceTime_D> listCDM = QDB.DistanceSelect_D();
int count = 1;
boolean flag = true;
int index;
for (int i = 0; i < listCDM.size() - 1; i++) {
if (listCDM.get(count).getDate().equalsIgnoreCase(listCDM.get(i).getDate()) &&
listCDM.get(count).getLat().equalsIgnoreCase(listCDM.get(i).getLat()) &&
listCDM.get(count).getLng().equalsIgnoreCase(listCDM.get(i).getLng()) &&
listCDM.get(count).getCounts().equalsIgnoreCase(listCDM.get(i).getCounts())) {
if (flag){
index = i;
flag = false;
}
}else {
index = i;
flag = true;
}
count++;
}
}


But my problem is here. How I can add these in another list and add single records in it ?

I get the first and last record then add in another list (I wrote the model above) .

enter image description here

I should fill my list here :

if (flag){
index = i;
///HERE
flag = false;
}
}else {
index = i;
///HERE
flag = true;
}

Answer

Here's a brute force method that first groups all the entries by a locally defined Identity and than merge those groups into a single Modeltest object.

There is probably a better way to do this though, since this disregards the fact that the input data is sorted.

List<Unprocessed_DistanceTime_D> list = ...;    

class Identity {
    private final String lat;
    private final String lng;
    private final String date;
    private final String counts;

    public Identity(Unprocessed_DistanceTime_D model) {
        this.lat = model.getLat();
        this.lng = model.getLng();
        this.date = model.getDate();
        this.counts = model.getCounts();
    }

    @Override
    public int hashCode() {
        int code = 0;
        code ^= lat.hashCode();
        code ^= lng.hashCode();
        code ^= date.hashCode();
        code ^= counts.hashCode();              
        return code;
    }

    @Override
    public boolean equals(Object other) {
        if(other == null)
            return false;
        if(!(other instanceof Identity))
            return false;

        Identity io = (Identity) other;

        return lat.equalsIgnoreCase(io.lat)
            && lng.equalsIgnoreCase(io.lng)
            && date.equalsIgnoreCase(io.date)
            && counts.equalsIgnoreCase(io.counts);              
    }
}

List<Modeltest> models = list.stream()
    .collect(Collectors.groupingBy(Identity::new))
    .values().stream()
    .map(group -> { // Convert groups into single Modeltest
        if(group.size() == 1) { // Case for single entries
            Unprocessed_DistanceTime_D e = group.get(0);
            return new Modeltest(e.getLat(), e.getLng(), e.getDate(), e.getTime(),
                e.getTime(), e.getCounts(), e.getUserCode());
        }

        // Case for more entries 
        group.sort(Comparator.comparing(Unprocessed_DistanceTime_D::getTime));

        Unprocessed_DistanceTime_D first = group.get(0);
        Unprocessed_DistanceTime_D last = group.get(group.size() - 1);

        return new Modeltest(first.getLat(), first.getLng(), first.getDate(),
            first.getTime(), last.getTime(), first.getCounts(), first.getUserCode());
    })
    .collect(Collectors.toList());
Comments