Print characters and their frequencies in order of occurrence using a LinkedHashMap in Java Last Updated : 27 Jan, 2021 Comments Improve Suggest changes Like Article Like Report Given a string str containing only lowercase characters. The task is to print the characters along with their frequencies in the order of their occurrence in the given string.Examples: Input: str = "geeksforgeeks" Output: g2 e4 k2 s2 f1 o1 r1Input: str = "helloworld" Output: h1 e1 l3 o2 w1 r1 d1 Approach: Traverse the given string character by character and store the frequencies of all the strings in a LinkedHashMap which maintains the order of the elements in which they are stored. Now, iterate over the elements of the LinkedhashMap and print the contents.Below is the implementation of the above approach: Java // Java implementation of the approach import java.util.LinkedHashMap; public class GFG { // Function to print the characters and their // frequencies in the order of their occurrence static void printCharWithFreq(String str, int n) { // LinkedHashMap preserves the order in // which the input is supplied LinkedHashMap<Character, Integer> lhm = new LinkedHashMap<Character, Integer>(); // For every character of the input string for (int i = 0; i < n; i++) { // Using java 8 getorDefault method char c = str.charAt(i); lhm.put(c, lhm.getOrDefault(c, 0) + 1); } // Iterate using java 8 forEach method lhm.forEach( (k, v) -> System.out.print(k + " " + v)); } // Driver code public static void main(String[] args) { String str = "geeksforgeeks"; int n = str.length(); printCharWithFreq(str, n); } } Output: g2 e4 k2 s2 f1 o1 r1 Comment More infoAdvertise with us Next Article Print characters and their frequencies in order of occurrence using a LinkedHashMap in Java S samarthbais255 Follow Improve Article Tags : Strings Java Hash Data Structures DSA +1 More Practice Tags : Data StructuresHashJavaStrings Similar Reads Find Character Frequencies in Order of Occurrence Given string s containing only lowercase characters, the task is to print the characters along with their frequency in the order of their occurrence and in the given format explained in the examples below.Examples: Input: s = "geeksforgeeks"Output: g2 e4 k2 s2 f1 o1 r1Input: str = "elephant"Output: 13 min read Maximum repeated frequency of characters in a given string Given a string S, the task is to find the count of maximum repeated frequency of characters in the given string S.Examples: Input: S = "geeksgeeks" Output: Frequency 2 is repeated 3 times Explanation: Frequency of characters in the given string - {"g": 2, "e": 4, "k": 2, "s": 2} The frequency 2 is r 6 min read Differences between TreeMap, HashMap and LinkedHashMap in Java Prerequisite: HashMap and TreeMap in Java TreeMap, HashMap and LinkedHashMap: What's Similar? All offer a key->value map and a way to iterate through the keys. The most important distinction between these classes is the time guarantees and the ordering of the keys.All three classes HashMap, TreeM 5 min read Sort the linked list in the order of elements appearing in the array Given an array of size N and a Linked List where elements will be from the array but can also be duplicated, sort the linked list in the order, elements are appearing in the array. It may be assumed that the array covers all elements of the linked list.arr[] = list = Sorted list = Asked in Amazon Fi 9 min read Difference Between LinkedList and LinkedHashSet in Java In this article you will learn difference between LinkedList and LinkedHashSet in java. Prerequisite: LinkedList : LinkedHashSet LinkedList class implements the List and Deque interface and extends from AbstractSequentialList class. LinkedList class uses doubly linked list to store the elements. It 3 min read Count pairs in an array such that frequency of one is at least value of other Given an array A[] of integers. The task is to find the total number of ordered pairs of positive integers (X, Y) such that X occurs in A[] at least Y times and Y occurs in A at least X times. Examples: Input : A[] = { 1, 1, 2, 2, 3 } Output : 4 Ordered pairs are -> { [1, 1], [1, 2], [2, 1], [2, 5 min read LinkedHashMap get() Method in Java with Examples In Java, get() method of LinkedHashMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key. --> java.util Package --> LinkedHashMap Class --> get() Method Syntax: Linked_Hash_Ma 2 min read LinkedHashMap containsKey() Method in Java with Examples The java.util.LinkedHashMap.containsKey() method is used to check whether a particular key is being mapped into the LinkedHashMap or not. It takes the key element as a parameter and returns True if that element is mapped in the map.Syntax: Linked_Hash_Map.containsKey(key_element) Parameters: The met 2 min read Sort elements by frequency | Set 5 (using Java Map) Given an integer array, sort the array according to the frequency of elements in decreasing order, if the frequency of two elements are same then sort in increasing order Examples: Input: arr[] = {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12} Output: 3 3 3 3 2 2 2 12 12 4 5 Explanation : No. Freq 2 : 3 3 : 4 4 3 min read Maven Project - LinkedHashMap and LinkedHashSet usage Along with JUnit Testing In software programming, there are many instances where we need the usage of LinkedHashMap and LinkedHashSet. LinkedHashSet Though HashSet is available, if we need the ordered version of HashSet, we need the LinkedHashSet. The advantage is when we iterate the LinkedHashSet, we get the display of the 7 min read Like