简体   繁体   中英

incompatib le types: java.lang.String cannot be converted to java.util.List<java.lang.String>

How do i convert this?

Sort sort = new Sort(Sort.Direction.DESC, MTS_DATE_CREATED_STRING);
private Sort(Sort.Direction direction, List<String> properties) {       
}

As compiler suggest error implies that the Java compiler is unable to resolve a value assigned to a variable or returned by a method, because its type is incompatible with the one declared on the variable or method and it can not convert String to a List<String> ie

private Sort(Sort.Direction direction, List<String> properties)

Into

private Sort(Sort.Direction direction, String properties)

The method accepts a List but you are passing a single String. Wrap that String in a List and it can be passed to the existing method.

Sort sort = new Sort(Sort.Direction.DESC, Arrays.asList(MTS_DATE_CREATED_STRING));

The method in question is private, which implies that you may have control over the class in which it resides. In that case, you may add overloaded versions of the method to accept a single String.

private Sort(Sort.Direction direction, String property) { ... }

And/or a varargs version that accepts any number of Strings without a collection...

private Sort(Sort.Direction direction, String... properties) { ... }

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