Write a Java program To create a map from a array of strings where the key is the string and the value is its length
Categories: Java 8(JDK1.8) Java Java Examples
package r4r.co.in;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CreateMapStringAsKeyAndLengthAsValue {
public static void main(String[] args) {
// Write a Java program To create a map from a array of strings where the key is the string and the value is its length
String[] str= {"Rajesh","Kumar"};
Map<String, Integer> map=Stream.of(str).collect(Collectors.toMap(s->s, String::length));
System.out.print(map);
}
}
Output:-
{Kumar=5, Rajesh=6}