라훌 굽타 :
누군가가 자바 (8)를 사용하여 아래를 달성하는 방법에 나를 인도시겠습니까 모르겠어요 키로하는 카운터를 얻는 방법
String str = "abcd";
Map<Integer,String> map = new HashMap<>();
String[] strings = str.split("");
int count =0;
for(String s:strings){
map.put(count++, s);// I want the counter as the key
}
라빈 드라 Ranwala :
당신이 사용할 수있는 IntStream
이 일을 끝내야 할 수 있습니다. 지도의 값으로 해당 인덱스에 문자열 배열의 키와 정수 값과 관련 값을 사용합니다.
Map<Integer, String> counterToStr = IntStream.range(0, strings.length)
.boxed()
.collect(Collectors.toMap(Function.identity(), i -> strings[i]));
의 필요 미연에 방지 또 다른 대안 split
이 될 것를,
Map<Integer, String> counterToStr = IntStream.range(0, strings.length)
.boxed()
.collect(Collectors.toMap(Function.identity(), i -> str.charAt(i) + ""));