LinkedHashSet JSONArray Android

0

This is the first time that I have considered using LinkedHashSet in my Android code and I am not sure if it can be used and how to use it.

I have a class (Class_1) where it is extracted from a JSONObject, using the loopj library for the connection and transference of json, an array which has an indeterminate number of objects inside each of them composed of 4 elements:

Id (unique)

Temperature

Moisture

Inserted (this is timestamp)

This data comes from my server, where in PHP and through a JOIN of tables prepares a json. It is for this reason that duplicate objects exist in said received element.

{"result":[{"Id":"621","temperatura":"35","humedad":"45","Insertado":"2016-08-30 12:53:36"},{......},{.....}...]}

These are stored in an Object "parametrosdht11" and then it is used through an interface in a second class (Class_2) where they will be processed to create a graph.

for (int i=0; i<cast.length(); i++) {
                JSONObject parametrosdht11 = cast.getJSONObject(i);
                loopjListener.onLoopjTaskCompletedBarometro(parametrosdht11, i);

            }
            loopjListener.onLoopCompleteBarometro();

So far everything works well, for this reason I do not put all the code, now, I see the need to delete duplicate objects (leaving only one of them) within the Json Array that I send to class_2. For this reason I consider using LinkedHashSet.

I would appreciate if you could help me, because I'm not sure I can use this interface and secondly I do not know how to implement it, so I would appreciate an example in code, as the subject says, I'm in Java but in Android. Thanks.

Update:

Example of the Json, as the repetitions are observed ....

{"result":[{"Id_temp":"1","temperatura":"20","Insertado_temp":"2016-08-16 12:30:29","Id_press":"1","presion":"34","Insertado_press":"2016-08-16 16:18:36","Id_alt":"1","altitud":"11","Insertado_alt":"2016-08-16 16:37:57"},

{"Id_temp": "1", "temperature": "20", "Insert_temp": "2016-08-16 12:30:29", "Id_press": "3", "pressure": " 55 "," Insert_press ":" 2016-08-16 16:22:14 "," Id_alt ":" 1 "," altitude ":" 11 "," Insert_alt ":" 2016-08-16 16:37: 57 "}, {" Id_temp ":" 1 "," temperature ":" 20 "," Insert_temp ":" 2016-08-16 12:30:29 "," Id_press ":" 4 "," pressure ": "55.45", "Insert_press": "2016-08-16 16:22:42", "Id_alt": "1", "altitude": "11", "Insert_alt": "2016-08-16 16:37 : 57 "}, {" Id_temp ":" 1 "," temperature ":" 20 "," Insert_temp ":" 2016-08-16 12:30:29 "," Id_press ":" 6 "," pressure " : "50", "Insert_press": "2016-08-16 18:26:27", "Id_alt": "1", "altitude": "11", "Insert_alt": "2016-08-16 16: 37:57 "}, {" Id_temp ":" 1 "," temperature ":" 20 "," Insert_temp ":" 2016-08-16 12:30:29 "," Id_press ":" 7 "," pressure ":" 50 "," Insert_press ":" 2016-08-16 18:28:13 "," Id_alt ":" 1 "," altitude ":" 11 "," Insert_alt ":" 2016-08-16 16 : 37: 57 "}, {" Id_temp ":" 1 "," temperature ":" 20 "," Insert_temp ":" 2016-08-16 12:30:29 "," Id_press ":" 8 "," pressure ":" 50 "," Insert_press ":" 2016-08-16 18:28:45 "," Id_alt ":" 1 "," altitude ":" 11 "," Insert_alt ":" 2016-08-16 16:37:57 "}, {" Id_temp ":" 1 "," temperature ":" 20 "," Insert_temp ":" 2016-08-16 12:30:29 "," Id_press ":" 1 "," pressure ":" 34 "," Insert_press ":" 2016-08-16 .blabla .....

    
asked by Oscar C. 02.09.2016 в 14:13
source

1 answer

1

You need to first have a class where you will store the information that you receive from the interaction with the web service. This class must implement the methods equals and hashCode , since LinkedHashSet uses these methods to verify the uniqueness of the object to be added in the set.

Here is a brief example of how this might work for you. (It is brief and to illustrate, you must adapt this code to your needs )

public class Temperatura {
    //nombre obtenido en base a la temperatura
    private int idTemperatura;
    private int temperatura;
    private Date insertadoTemp;
    private int idPresion;
    private int presion;
    private Date insertadoPresion;
    private int idAltitud;
    private int altitud;
    private Date insertadoAltitud;

    //getters y setters...

    @Override
    public int hashCode() {
        //hashcode basado en la fecha de inserción de temperatura
        return insertadoTemp != null ? insertadoTemp.hashCode() : idTemperatura;
    }

    @Override
    public boolean equals(Object o) {
        boolean result = false;
        if (o != null && o.getClass().equals(this.getClass())) {
            Temperatura otro = (Temperatura)o;
            //asumiendo que utilizas Java 7 usa el código de abajo
            //result = Objects.equals(this.insertadoTemp, otro.insertadoTemp);
            //si usas Java 6 y otros
            result = (this.insertadoTemp != null && otro.insertadoTemp != null
                && this.insertadoTemp.equals(otro.insertadoTemp))
                || (this.insertadoTemp == null && otro.insertadoTemp == null);
        }
        return result;
    }

    @Override
    public String toString() {
        return String.format("%d-%s", id, insertadoTemp.toString());
    }
}

//...

//Agregando elementos de tipo Temperatura a LinkedHashSet
Set<Temperatura> setTemperaturas = new LinkedHashSet<>();
Temperatura t1 = new Temperatura();
t1.setId(1);
t1.setDate(stringToDate("2016-09-02 04:00:00"));
Temperatura t2 = new Temperatura();
t2.setId(2);
t2.setDate(stringToDate("2016-09-03 04:00:00"));
Temperatura t3 = new Temperatura();
t3.setId(3);
//tiene la misma fecha que t1, no será agregado al set
t3.setDate(stringToDate("2016-09-02 04:00:00"));
setTemperaturas.add(t1);
setTemperaturas.add(t2);
setTemperaturas.add(t3);
System.out.println(setTemperaturas);

//método stringToDate
//deberías manejar las excepciones de otra manera
static Date stringToDate(String s) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        return sdf.parse(s);
    } catch (ParseException ex) {
        throw new RuntimeException("Problema al parsear fecha " + s, ex);
    }
}

Exit:

[ 1-Fri Sep 02 04:00:00 GMT -5 2016, 2-Fri Sep 03 04:00:00 GMT -5 2016 ]
    
answered by 03.09.2016 / 00:56
source