Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Add equal values in a ArrayList in Android

I am storing scores in a ArrayList, but when the user has a score equal to a previous score, the second one is not added to the ArrayList. How can I add equal values to a ArrayList (or use another type of List)?

Code:

[...]
ArrayList<String> savedArray = new ArrayList<>();
Set<String> gandula = new HashSet<>();
[...]
public Preferencias(Context contextoParametro) {
    contexto = contextoParametro;
    preferences = contexto.getSharedPreferences(NOME_ARQUIVO, MODE);
    editor = preferences.edit();
    }

public void addMediaGeral(double mediaGeral) {
   //Save Score
   Set<String> set = new HashSet<String>();
   if (!getMediaGeral().get(0).equals("0")) {
       savedArray.addAll(getMediaGeral());
   }
   savedArray.add(String.valueOf(mediaGeral));
   set.addAll(savedArray);
   editor.putStringSet(KEY_MEDIA_GERAL, set);
   editor.commit();
}

public ArrayList<String> getMediaGeral(){
    //get score
    Set<String> set = preferences.getStringSet(KEY_MEDIA_GERAL, gandula);
    ArrayList<String> list = new ArrayList<String>(set);
    return list;
}
[...]

Retrieving in my activity:

[...]
mediaGeral = score;
preferencias.addMediaGeral(Double.parseDouble(mediaGeral));
[...]
ArrayList<String> string_medias = new ArrayList<>();
    string_medias = preferencias.getMediaGeral();
    ArrayList<Float> float_medias = new ArrayList<>();
    for (int j = 0; j < string_medias.size(); j++){
        float_medias.add(Float.parseFloat(string_medias.get(j)));
    }
    Log.i("TESTANDO PRF", "TAMANHO DA LISTA: "+float_medias.size());
    Float[] dataObjects = float_medias.toArray(new 
    Float[float_medias.size()]);
    doChart(float_medias);
    [...]

I scored 0.33333 three times (I'll config decimal places) but it is showing only one score 0.3333.

Printscreen: http://prntscr.com/f1vx3h

But the chart accepts duplicates. Printscreen: http://prntscr.com/f1vxng (i manipulated the scores in the last printscreen).

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

ArrayList accept duplicate this should not be your problem but I saw that your store yours score in a HashSet uses a hashMap instead because:

In the case of HashMap, it replaces the old value with the new one.

In the case of HashSet, the item isn't inserted.

over 4 years ago · Santiago Trujillo Report

0

A Set does not allow for duplicate values.

Doing this:

set.addAll(savedArray);

discards all your duplicates.

These answers may be of help.

Saving a JSON object as a string in preferences would probably be the way to go.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!