0

I got some issues with Set<String> and I don't know why - it seems that data isn't completly inserted to data.

This is my code:

private Set<String> spinnersPosToData() {
    Set<String> data = new HashSet<>();

    data.add(String.valueOf(CHURCH_SPINNER_POS));
    data.add(String.valueOf(MEMBER_BY_SPINNER_POS));
    data.add(String.valueOf(BIRTH_DAY_SPINNER_POS));
    data.add(String.valueOf(BIRTH_MONTH_SPINNER_POS));
    data.add(String.valueOf(GENDER_SPINNER_POS));
    data.add(String.valueOf(SALUATION_SPINNER_POS));
    data.add(String.valueOf(BIRTH_YEAR_RANGER_SPINNER_POS));
    data.add(String.valueOf(REGISTERED_TO_VOTE_SPINNER_POS));
    data.add(CELL_PHONE_TXT);

    Log.i("chu", String.valueOf(CHURCH_SPINNER_POS));
    Log.i("memBy", String.valueOf(MEMBER_BY_SPINNER_POS));
    Log.i("bDay", String.valueOf(BIRTH_DAY_SPINNER_POS));
    Log.i("bMon", String.valueOf(BIRTH_MONTH_SPINNER_POS));
    Log.i("gen", String.valueOf(GENDER_SPINNER_POS));
    Log.i("sal", String.valueOf(SALUATION_SPINNER_POS));
    Log.i("bYea", String.valueOf(BIRTH_YEAR_RANGER_SPINNER_POS));
    Log.i("reg2Vot", String.valueOf(REGISTERED_TO_VOTE_SPINNER_POS));
    Log.i("celPho", CELL_PHONE_TXT);

    Log.i("dat2Str", data.toString());

    return data;
}

And here is what I found in the logs:

04-07 14:28:22.388 7309-7309/app.com.mmm I/chu: 11
04-07 14:28:22.388 7309-7309/app.com.mmm I/memBy: 1
04-07 14:28:22.388 7309-7309/app.com.mmm I/bDay: 3
04-07 14:28:22.388 7309-7309/app.com.mmm I/bMon: 4
04-07 14:28:22.388 7309-7309/app.com.mmm I/gen: 2
04-07 14:28:22.388 7309-7309/app.com.mmm I/sal: 2
04-07 14:28:22.388 7309-7309/app.com.mmm I/bYea: 2
04-07 14:28:22.389 7309-7309/app.com.mmm I/reg2Vot: 1
04-07 14:28:22.389 7309-7309/app.com.mmm I/celPho: 123456789
04-07 14:28:22.389 7309-7309/app.com.mmm I/dat2Str: [4, 1, 11, 123456789, 3, 2]

As you see data isn't complete and is in a random order. Why is that?

Vincenzo Maggio
  • 3,787
  • 26
  • 42
y07k2
  • 1,898
  • 4
  • 20
  • 36

3 Answers3

2

Definition of Set:

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

Definition of HashSet:

This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

If you want to keep duplicate values and the insertion order, use an ArrayList instead.

Dmitri Timofti
  • 2,428
  • 1
  • 22
  • 25
  • KK, I saw a moment ago that my values don't duplicate :P But I want to keep it in `SharedPreferences` how can I do it? – y07k2 Apr 07 '16 at 12:41
  • 1
    check [this](http://stackoverflow.com/a/7057858/4605725) post on how to serialize an ArrayList and save it to SharedPreferences, although i would recommend saving non-trivial data to a database or at least a file. – Dmitri Timofti Apr 07 '16 at 12:46
1

As you see data isn't completly and is in a random order. Why is that?

That's the expected behaviour. HashSet is a collection which doesn't preserver the order of its items and not contains duplicates. You can read more here

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

A set is a collection that contains no duplicate elements. As you can see you are adding some values multiple time. Also HashSet is not keeping insert order. If the order is import you should use: LinkedHashSet If you need all elements use ArrayList

Liviu Stirb
  • 5,876
  • 3
  • 35
  • 40