Home » Android » java – how to find the length of a set?

java – how to find the length of a set?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m trying to find the lentgh of a set from a preference.
I tired set.length, but it doesnt work. Anyone know another way to find a length of a set?

   set = prefs.getStringSet("key", null);
            int X = set.length; //it doesn't like length....
How to&Answers:

I believe instead of set.length, you’ll want to call set.size()

Answer:

Your set is a Set<String>, not a String[]. So you have to call:

int x = set.size();