Java 8 – How to convert duplicate characters to Uppercase in a String ?

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 :

References :

Happy Coding !!
Happy Learning !!

Java 8 – How to remove special characters from String ?
Java 8 – How to convert first character of every word to Uppercase ?