-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFindVowels.java
More file actions
38 lines (33 loc) · 1.12 KB
/
Copy pathFindVowels.java
File metadata and controls
38 lines (33 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package interviewQuestions;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
public class FindVowels {
public static void main(String[] args) {
String str="AlertEnterprise";
findVowels(str);
// findVowels1(str);
}
static void findVowels(String s) {
Map<Character,Integer> map= new HashMap<>();
for (int i=0;i<s.length();i++) {
Character c=s.charAt(i);
if((c.equals('A')) || (c.equals('E')) || (c.equals('I')) || (c.equals('O')) || (c.equals('U'))) {
map.put(c, map.getOrDefault(c,0)+1);
}
}
for (Entry<Character,Integer> entry: map.entrySet()){
System.out.println("Character: " + entry.getKey()
+ " occurred in the string: " + entry.getValue() + " times" );
}
}
static void findVowels1(String s) {
s.chars()
.map(Character::toUpperCase)
.mapToObj(c -> (char) c)
.filter(c -> "AEIOU".indexOf(c) >= 0)
.collect(Collectors.groupingBy(c -> c, Collectors.counting()))
.forEach((vowel, count) -> System.out.println(vowel + "=" + count));
}
}