1. Print Mostly occurred string in Hello String import java.util.Map ; import java.util.Optional ; import java.util.stream.Collectors ; public class MostOccuringCharacter { // print most occuring character in hello string and write program to use optional public static void main ( String [] args ) { String str = "Hello" ; Optional < Map . Entry < Character , Long >> character = str .chars() .mapToObj( x -> ( char ) x ) .collect( Collectors . groupingBy ( x -> x , Collectors . counting ())) .entrySet() .stream() .max( Map . Entry . comparingByValue ()); if ( character .isPresent()){ System . out .println( "Most occurring character: " + character .get().getKey()); } //or character .ifPresent( c -> System . out .println( "Most occurring character: " + c .getKey())); } }
A HashMap is a part of Java's Collections Framework , and it stores data in key-value pairs. Internally, it uses a hash table to store entries, making it efficient for fast lookups, insertions, and deletions. The underlying structure of a HashMap is based on a dynamic array of buckets (or "bins") and the hashing technique for quick access. Here’s an overview of how HashMap works internally: 1. Structure of HashMap A HashMap consists of the following key components: Buckets : A bucket is essentially an index in an array where hash collisions are stored. A HashMap is backed by an array, and each entry (a key-value pair) is stored in one of the array's indices (buckets). Entries (Key-Value Pairs) : Each entry is a key-value pair, stored in the buckets. Hashing : A hash function is used to determine the index (bucket) where a key-value pair will be stored. 2. Hashing Process When a key-value pair is added to a HashMap , the key undergoes a hashing process to de...