Peter Penzov
Peter Penzov

Reputation: 1668

Pass List of Strings to Java method

I have a question how to use this Java method boolean[] hasRoles(List<String> roleIdentifiers).

How I can send it list of Strings?

Upvotes: 0

Views: 3830

Answers (2)

Andrew
Andrew

Reputation: 49606

You could pass an implementation of the List<T> interface, for example:

1) instance.hasRoles(new ArrayList<String>()); // the empty list
2) instance.hasRoles(Arrays.asList("s1", "s2", ...)); // the list with values
3) instance.hasRoles(new ArrayList<String>() { // look at @Sam's comment
       {
           add(...);
           ...
       }
   });

Instead of List<String> data = null;, you should initialize your list.

It seems you are trying to do something like booleanresult = instance.hasRoles(...);, but the type that returns from the method is different. boolean and boolean[] are not the same.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533492

The method returns a boolean[] not a boolean The error is complaining about how you use the result

List<String> data = ...;
if (currentUser.hasRoles(data)) // will not work as a boolean[] if not a boolean

You need to check the element of the boolean[]

One alternative is to check hasAllRoles which does return a boolean or check for the specific roles you are interested in by index.

Upvotes: 2

Related Questions