5

Can someone please guide me on how to achieve the below using Java 8. I don't know how to get that counter as the key

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
}
Naman
  • 27,789
  • 26
  • 218
  • 353
Rahul Gupta
  • 1,079
  • 2
  • 15
  • 27
  • Possible duplicate of [Java 8: How to convert String to Map?](https://stackoverflow.com/questions/52695410/java-8-how-to-convert-string-to-mapstring-string) – Vishwa Ratna Feb 20 '19 at 06:23
  • You are preferably looking for a `Map` accessing which again is mostly accessible via `String.charAt`. What's the use case around building this Map if you could share the details? – Naman Feb 20 '19 at 06:53

5 Answers5

5

You can use IntStream to get this thing done. Use the integer value as the key, and the relevant value in the string array at that index as the value of the map.

Map<Integer, String> counterToStr = IntStream.range(0, strings.length)
    .boxed()
    .collect(Collectors.toMap(Function.identity(), i -> strings[i]));

Another alternative that obviates the need of split would be,

Map<Integer, String> counterToStr = IntStream.range(0, strings.length)
    .boxed()
    .collect(Collectors.toMap(Function.identity(), i -> str.charAt(i) + "")); 
Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
2

You can write like

    String str = "abcd";
    Map<Integer, Character> map = IntStream.range(0, str.length()).boxed()
        .collect(Collectors.toMap(Function.identity(), pos -> str.charAt(pos)));

No need to split the String with String[] strings = str.split(""); A simple one-liner.

Mohamed Anees A
  • 4,119
  • 1
  • 22
  • 35
1

You can do it without the counter as:

String str = "abcd";
Map<Integer,String> map = new HashMap<>();
String[] strings = str.split("");
for(int i=0;i<strings.length;i++) {
    map.put(i, strings[i]);
}
map.forEach((k,v)->System.out.println(k+" "+v));

Another way around, credit @Holger

for(String s: strings) {
     map.put(map.size(), s);
 }
Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
0

There are many solution: some of them would be like this:

Map<Integer,Character> map = new HashMap<>();

AtomicInteger atomicInteger = new AtomicInteger(0); 
map = str.chars()
            .mapToObj(i->new AbstractMap.SimpleEntry<>(atomicInteger.getAndAdd(1),(char)i))
            .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));

or even use simple forEach

int count =0;
for (Character c:str.toCharArray()) {
  map.putIfAbsent(count++,c);
}
Hadi J
  • 16,989
  • 4
  • 36
  • 62
0

This solution will help you to create a linked hash map. The keys are characters and the values are the count of each character in the string.

 String str = "preethi";

 Map<Character, Integer> lhm = new LinkedHashMap<>();

 str.chars().mapToObj(x -> (char)x).forEach(ch -> {
          lhm.put(ch, lhm.get(ch) != null ? lhm.get(ch) + 1: 1);
 });

enter image description here

swyrik
  • 144
  • 2
  • 7