Repeated values in JSON

0

I'm loading some values in JSON and I would like to control and delete the duplicates:

    public void loadEventos(){
    SharedPreferences prefs = getSharedPreferences(getString(R.string.gamedata), Context.MODE_PRIVATE);
    String data=prefs.getString("eventos","");
    eventos.clear();
    if(!data.isEmpty()) {
        try {
            JSONObject jo = new JSONObject(data);
            if(jo.has("eventos")){
                JSONArray ja=jo.getJSONArray("eventos");
                for (int i = 0; i < ja.length(); i++) {
                    if (!ja.getString(i).isEmpty()) {
                        Evento ev=new Evento(ja.getString(i));
                        Log.d(GLOBALES.TAG,"eventosString="+eventos.get(i).toString());
                        Log.d(GLOBALES.TAG,"eventosArray="+ja.getString(i));
                        if(ev.getFinalizacion()>System.currentTimeMillis()) {
                            eventos.add(ev);
                        }
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
    
asked by PiledroMurcia 29.06.2017 в 10:01
source

1 answer

2

Since events must necessarily be a List, the best thing you can do is determine if the item you are inserting exists between the items in the list before inserting it. The fastest way to search for items within a fairly large size list is a binary search. But he has the problem that the lists must be ordered. Order spends time in execution. So you should ask yourself a question:

eventos can have many records?

If the answer is yes. The most optimal thing is to sort the list once, then search for the new record in your ordered list every time you are going to make an insertion and if it is not, insert it in an orderly manner. In this way the list is always ordered.

  • Sort the list: You can sort a list using the Collections.sort(List<T>) . However, the Event class must implement Comparable . It is an interface in which a method is implemented, in that method you indicate how to sort the list. To show an example:

    /** {@inheritDoc} */
    @Override
    public int compareTo(NodeElement arg0) {
        return this.getId().compareTo(arg0.getId());
    }
    

    Once the interface is implemented you could call the method in the first line of the method:

    Collections.sort(eventos);
    
  • Element Search: You can search for the element by calling the following method binarySearch (List & gt ; list, T key) that returns the position in which the element is found or - (correct insertion point) if it can not be found

    int i = Collections.binarySearch(eventos,ev);
    if(i < 0){
       // Inserción ordenada
    }
    
  • Ordered insertion: Finally, it is only necessary to insert the element in an orderly manner. Since binarySearch returns the negative position in which the element minus one should be. Simply save that integer and subtract 1:

     eventos.add((-i)-1, ev);
    
  • Example

    I'll give you an example with strings:

    public class MainTest {
        public static void main(String[] args) {
            List<String> strings = new ArrayList();
            strings.add("b");
            strings.add("d");
            strings.add("h");
    
            Collections.sort(strings);
    
            int i = Collections.binarySearch(strings, "a");
    
            strings.add((-i)-1,"a");
    
            i = Collections.binarySearch(strings, "c");
            strings.add((-i)-1,"c");
    
            i = Collections.binarySearch(strings, "y");
            strings.add((-i)-1,"y");
    
            System.out.println(strings);
        }
    }
    
  • If the answer is No, you can overwrite the equals method of Event

  • Overwrite the Event%% co method to identify which element is the same as what another.

  • Create the if:

    if(!eventos.contains(ev)){
       eventos.add(ev);
    }
    
  • answered by 29.06.2017 / 10:47
    source