A solution for Java 7 - you can use the for
loop structure with a single index to advance into the 2 arrays at the same time.
As for the assumption labelNames.length == labelValues.length
it is usually better to always check it to make sure you didn't introduce a bug and get unexpected results:
if(labelNames.length == labelValues.length) {
int i;
for(i = 0; i < labelNames.length; i++) {
labels.put(labelNames[i], labelValues[i]);
}
}
In Java 8 you could use Streams
as an alternative:
Map<String, String> map =
IntStream.range(0, names.length)
.boxed()
.collect(Collectors.toMap(i -> labelNames[i], i -> labelValues[i]));