Converting Binary To Octal number using HashMap in Java Last Updated : 02 Nov, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Remember while we were converting octal to decimal we were taking 3 binary digits at a time. A similar approach will be used where here for every 3 digits we are having a corresponding number as in octal system we have numbers from 0 to 'R-1' where R represents base value of number system. As the name suggests, in an octal number system, R is equivalent to 8. Hence, the number is as follows: 0,1,2,3,4,5,6,7. Now going by virtue of HashMaps in converting one thing is clear from here that one way or other binary equivalent and octal equivalent are going to act as key-value pairs in our HashMap. Illustration: OctalBinary00001001201030114100510161107111Algorithm: Convert the binary number into a decimal number.Using HashMap we map every bit with its decimal values.When the bit is found it maps with the decimal numberPrints and display the octal equivalent of the number.Example: Input 1 : 1011Output 1 : 13Input 2 : 1000Output 2 : 10Implementation: Java // Java Program to Convert Binary Number to Octal Number // Importing all utility classes import java.util.*; // Main class public class GFG { // Method 1 // Helper method public static void octal(String s) { // Here binary number is represented by string 's' // over which standard length() method is computed // to get the length of binary number // Appending 2 zeros if binary numbers leaves // remainder as 1 after dividing with 3 if (s.length() % 3 == 1) { // Append two zeros to it s = "00" + s; } // If binary string number length after equals 2 else if (s.length() % 3 == 2) { // Concatenate string by adding 1 zero to it s = "0" + s; } // Creating an object of HashMap // Declaring object of String and Integer types HashMap<String, Integer> hm = new HashMap<>(); // Adding elements to the object created above // using the put() method // Adding elements(key-value) pairs to given object // 000 in binary system -> 0 in octal system // 001 in binary system -> 1 in octal system // Similarly adding for 0 to N-1 (N=8 for octal) hm.put("000", 0); hm.put("001", 1); hm.put("010", 2); hm.put("011", 3); hm.put("100", 4); hm.put("101", 5); hm.put("110", 6); hm.put("111", 7); // Creating and declaring a string array String[] part = new String[3]; int t = 0; // Iterating over the binary number digits for (int i = 0; i < s.length(); i = i + 3) { // Checking for substring in an binary number // digit array String bypart = s.substring(i, i + 3); part[t] = bypart; // If found if (hm.containsKey(part[t])) { // Getting the part to be matched for // its corresponding octal numbers System.out.print(hm.get(part[t])); } // Incrementing the counter t++; } } // Method 2 // Main driver method public static void main(String[] args) { // Display message System.out.print("Enter the binary number to be converted : "); // Binary number to be converted // Custom entry String s = 011; // Calling the method1 octal() over the // above input entered number octal(s); // Display message System.out.print("Octal equivalent : "); } } Output: Enter the binary number to be converted : 011Octal equivalent : 3Time Complexity: O(N) Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article IdentityHashMap hashCode() Method in Java S subratanandy Follow Improve Article Tags : Java Java-Collections Java-HashMap Practice Tags : JavaJava-Collections Similar Reads HashMap entrySet() Method in Java The entrySet() method of the HashMap class in Java is used to create a set view of the mappings contained in the HashMap. This method allows us to iterate over the key-value pairs in the map or convert them into a set.Example 1: Here, we will use the entrySet() method to view the mappings in a HashM 2 min read IdentityHashMap values() Method in Java The java.util.IdentityHashMap.values() method of IdentityHashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the Map. Syntax: Identity_Hash_Map.values() Parameters: The method does not accept any parameters. Retur 2 min read HashMap size() Method in Java The size() method of the Java HashMap class is used to retrieve the number of key-value pairs currently stored in the HashMap.Example 1: This example demonstrates the use of the size() method to get the number of elements in the HashMap.Java// Java program to demonstrates the working of size() impor 2 min read HashMap values() Method in Java The java.util.HashMap.values() method of HashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the HashMap. Syntax: Hash_Map.values() Parameters: The method does not accept any parameters. Return Value: The method i 2 min read IdentityHashMap hashCode() Method in Java The java.util.IdentityHashMap.hashCode() method in Java is used to fetch the hash code value of a particular this IdentityHashMap. A map consists of a number of buckets to store the key-value pair. Each bucket has a unique identity and when a key-value pair is inserted into a bucket, the key's hashc 2 min read ConcurrentHashMap values() method in Java with Examples The values() method of ConcurrentHashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the ConcurrentHashMap. Syntax: ConcurrentHashMap.values() Parameters: The method does not accept any parameters. Return Value: T 2 min read Like