Wardruna
Wardruna

Reputation: 308

How to check if set exists in java?

I am looping through some strings and i want to add each string to the appropriate set using java treeset.How can i check if treeset exists?My code should look similiar to this:

if (set exists)
set.add(string)
else{
    TreeSet<Integer> tree = new TreeSet<Integer>();
    tree.add(string)    
}

Upvotes: 0

Views: 355

Answers (1)

bmscomp
bmscomp

Reputation: 777

Somewher in your code you will have a variable called set of type TreeSet or Set

if (set != null ){
   set.add(string)
}else{
    set = new TreeSet<String>(); // it's not set if integers but Strings 
    tree.add(string)    
}

Upvotes: 3

Related Questions