0

Getting type safety conversion warning from below code.

List<Map<String, Object>> batchValues = new ArrayList<>(entity.size());

int[] noOfRecords = namedParameterJDBCTemplate.batchUpdate(updateQuery, batchValues.toArray(new Map[entity.size()]));

batchValues.toArray(new Map[entity.size()]) Type safety: The expression of type Map[] needs unchecked conversion to conform to Map[]

If I used @SuppressWarnings("unchecked") this annotation on method level then it's resolved the issue.

What is the best solution to resolve the issue?

Shiladittya Chakraborty
  • 3,982
  • 7
  • 35
  • 74
  • Maybe you could adapt: https://stackoverflow.com/a/2848268/180100 but using the annotation is OK if you know what you do. You could also use an array for batchValues (you know the size so..) –  Dec 11 '17 at 12:43
  • Dupliicate of https://stackoverflow.com/questions/7131652/generic-array-creation-error (which is marked as a duplicate, but actually isn't). – Piotr Wilkin Dec 11 '17 at 12:45

1 Answers1

0

The method declaration is <T> T[] toArray(T[] to)

The assignment for a variable is typed as Map[] but as reference in methods it is Object[]

Since the declaration accepts <T> you can simple do when as parameter:

batchValues.<Map<String, Object>>toArray(new Map<String, Object>[entity.size()])
Marcos Vasconcelos
  • 17,773
  • 29
  • 104
  • 165