简体   繁体   中英

Java declaring a String array in method invocation

I'm trying to declare a String array within a method invocation, like so:

if (emailYaml.keySet().containsAll(new String[]{"mailto","subject","text"}))

I'm not entirely sure if this is the best approach in any case (i'm pretty new to programming), but in any case Eclipse tells me:

The method containsAll(Collection<?>) in the type Set<String> is not applicable for the arguments (String[])

Could anyone help with a solution?

Many thanks

An array isn't a subtype of Collection . Convert it to a List for example, before passing it to containsAll :

.containsAll(Arrays.asList(new String[] {"mailto", "subject", "text"}))

Or even simplier, since asList takes a vararg as parameter:

.containsAll(Arrays.asList("mailto", "subject", "text"))

Contains all accepts a collection.

From the docs :

containsAll(Collection<?> c) 

Try this insead:

if (emailYaml.keySet().containsAll(Arrays.asList("mailto","subject","text"))

containsAll() expects a Collection and you are passing an array, thus your compiler complains.

if (emailYaml.keySet().containsAll(Arrays.asList(new String[]
  {"mailto","subject","text"})))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM