How to find first Non-Repeated Character from String using Linked Hashmap

LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements can be accessed in their insertion order.

package testjava.javatest;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class FirstNonRepeatedHashMap
{
	public static char getFirstNonRepeatedChar(String str) 
	{
		Map<Character, Integer> counts = new LinkedHashMap<Character, Integer>(str.length());
		
		for (char c : str.toCharArray())
		{
			// If key already exist then increment by one otherwise set it to 1
			counts.put(c, counts.containsKey(c) ? counts.get(c) + 1 : 1); 
		} 
		
		for (Entry<Character,Integer> entry : counts.entrySet()) 
		{ 
			if (entry.getValue() == 1) 
			{ 
				return entry.getKey(); 
			} 
		} 
		
		return '0'; 
	}


    public static void main(String[] args)
    { 
    	System.out.println("First non repeated Char: " + getFirstNonRepeatedChar("Programs Buzz Programs"));
    	System.out.println("First non repeated Char: " + getFirstNonRepeatedChar("Hello World Hey World"));
    	System.out.println("First non repeated Char: " + getFirstNonRepeatedChar("Hey Hey"));
    }
} 

Output

First non repeated Char: B

First non repeated Char: y

First non repeated Char: