-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString Program using java 8
More file actions
74 lines (52 loc) · 2.82 KB
/
String Program using java 8
File metadata and controls
74 lines (52 loc) · 2.82 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.stream.Collectors;
//Count occurrence of each character
// find dublicate Character in string
//Count words in string -> "java is good" -> 3 word
//Count the word Occurance -> " java is good but java is hard" -> {but=1, =1, java=2, is=2, hard=1, good=1}
//count the character in words -> "java is good"=> 4-> [java,good], 2->[is]
//----------------need to more practice-------------------------
//Count vowels
//Find longest word -> max(Comparator.comparing(String::length)).get()
//First non-repeating character
class Codechef
{
public static void main(String[] args) throws java.lang.Exception
{
String str = "Programming";
// Map < Character, Long > map = str.chars()
// .mapToObj(c -> (char) c)
// .map(Character::toLowerCase) // ⭐ important line
// .collect(Collectors.groupingBy(c -> c, Collectors.counting()));
// System.out.println(map);
// ⭐ // find dublicate Character in string
// Set<Character>set = new HashSet<>();
// str.chars().mapToObj(c-> (char) c).filter(c-> !set.add(c)).forEach(c-> System.out.print(c+" "));
// System.out.println("");
// ⭐ //Count vowels
// str.chars().mapToObj(c-> (char)c).filter(c-> "aeiou".contains(String.valueOf(c))).forEach(n-> System.out.print(n+ " "));
//⭐ reverse string -----------------------
//StringBuilder sb = new StringBuilder(str);
//String reverse= sb.reverse().toString();
// System.out.println(reverse);
//⭐ Palindrome string-------------------------& reverse string
// StringBuilder sb2 = new StringBuilder(str);
// Boolean palindrome = str.equals(sb2.reverse().toString());
// System.out.println(palindrome);
////⭐ count the word Occurance or frequesncy--------------------
String str2 = " java is good but java language is hard";
Map < String, Long > map4 = Arrays.stream(str2.split(" ")).collect(Collectors.groupingBy(s -> s, Collectors.counting()));
System.out.println(map4);
//⭐ Count words in string -> "java is good" -> 3 word
// Long count = str.chars().mapToObj(c-> (char)c).filter(c-> "aeiou".contains(String.valueOf(c))).count();
// System.out.println("Count: "+count);
//⭐ count the character in words -> "java is good"=> 4-> [java,good], 2->[is]
String str3 = "java is good language is";
//Find longest word ⭐ important
String longest = Arrays.stream(str2.split(" ")).max(Comparator.comparing(String::length)).get();
long longestCount = longest.length();
System.out.println("Longest: " + longest + " Count: " + longestCount);
}
}