In this article, we will learn how to convert duplicate characters/occurrences to Uppercase letter in a String
In one of the previous article, we already discussed about finding/converting duplicates in a String using Java version below 8, read Java – How to uppercase every duplicate character in String ?
Uppercase duplicate characters in a String :
Steps :
- Convert given String to String[] by splitting based on the space delimiter using Arrays.stream() method
- Then map/transform String[] Array into Character[] Array using Stream.map() method
- Convert Split-ed String into IntStream using String.chars() method
- Convert IntStream to Stream of Character using Stream.mapToObject() method
- Again convert all characters in Stream of Character to lowercase using Stream.map() method
- At last convert lowercase Stream of Characters into Character[] Array
- Then invoke method by Method-reference by passing above obtained Character[] Array as argument which will convert duplicate characters/occurrences into uppercase and return the result
- Collect converted String using Stream.collect() method by passing Collectors.joining(” “) as argument
- Finally pretty print both original/converted Strings to the console
- Note: here finding/converting duplicate characters/occurrences in each word of a given String rather than entire String, if the requirement is to find/convert duplicates in entire String then slightly modify below code
UpperCaseDuplicateCharacterInString.java
package in.bench.resources.java.collections;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;
public class UpperCaseDuplicateCharacterInString {
// main() method
public static void main(String[] args) {
// 1. Original String
String str = "madam sss gg fff rr malayalam";
System.out.println("Original String :- \n" + str);
// 2. process String using different Stream methods
String result = Arrays.stream(str.split("\\s")) // split String based on space delimiter
.map( // map
str1 -> str1.chars() // convert each split-ed String into IntStream
.mapToObj(e -> (char)e) // map IntStream to Stream<Character>
.map(Character::toLowerCase) // convert Stream<Character> into lower case
.toArray(Character[]::new) // convert Stream<Character> to Character[] Array
)
.map(UpperCaseDuplicateCharacterInString::convert) // invoke method for each Character Arr
.collect(Collectors.joining(" ")); // finally, join with space delimiter
// 3. print result to the console
System.out.print("\nConverted String :- \n" + result);
}
/**
* This method converts duplicates in a String to Upper case
*
* @param chArray
* @return
*/
private static String convert(Character[] chArray) {
// 1. local variables
Set<Character> linkedHashSet = new LinkedHashSet<Character>();
StringBuilder stringBuilder = new StringBuilder();
// 2. iterate through Character[] array
for(Character ch : chArray) {
// 2.1 check whether each character contains in LinkedHashSet
boolean checkChar = linkedHashSet.contains(ch);
// 2.2 if it is not present in LinkedHashSet, then add
if(!checkChar) {
linkedHashSet.add(ch);
stringBuilder.append(ch);
}
else {
// 2.3 if present in LinkedHashSet, then store duplicate Character
// after converting to UPPER-CASE in StringBuilder
stringBuilder.append(Character.toUpperCase(ch));
}
}
// 3. return the result after conversion
return stringBuilder.toString();
}
}
Output:
Original String :-
madam sss gg fff rr malayalam
Converted String :-
madAM sSS gG fFF rR malAyALAM
Related Articles :
- Java 8 – Count and print number of lines and words in a text file
- Java 8 – Count and print number of repeated word occurrences in a text file
- Java 8 – Count and print number of repeated character occurrences in a String
- Java 8 – Count and print number of Vowels and Consonants in a String
- Java 8 – Reverse each words in a String using Stream and Collectors
- Java 8 – Reverse complete/entire String using Stream and Collectors
- Java 8 – Remove input-ted Character from a given String
- Java 8 – How to split a String and Collect to any Collection ?
- Java 8 – How to check whether given String contains only Alphabets or Letters ?
- Java 8 – How to check whether given String contains only Digits ?
- Java 8 – How to check whether given String contains Alphanumeric characters only ?
- Java 8 – How to convert first character of every word to Uppercase ?
- Java 8 – How to convert duplicate characters to Uppercase in a String ?
- Java 8 – How to remove special characters from String ?
References :
- https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Set.html
- https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashSet.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html
Happy Coding !!
Happy Learning !!