Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Quearn

Quearn Logo Quearn Logo

Quearn Navigation

  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Quearn Academy
  • About Us
  • Blog
  • Contact Us
Home/Questions/Q 803881
Next
In Process
codemania
  • 0
codemania
Asked: May 27, 20232023-05-27T09:25:36+00:00 2023-05-27T09:25:36+00:00In: Education

Java Program to Implement Associate Array

  • 0
Java Program to Implement Associate Array
codejava
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    1. codemania
      2023-05-27T09:25:52+00:00Added an answer on May 27, 2023 at 9:25 am
      This is a Java Program to implement Associate Array. An associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection. 

      Here is the source code of the Java Program to implement Associate Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

      1. /**
      2.  ** Java Program to implement Associate Array
      3.  **/
      4. import java.util.Scanner;
      5. import java.util.HashMap;
      6. /** class AssociateArray */
      7. class AssociateArray
      8. {
      9.     private HashMap<String, String> keyVal;
      10.     /** constructor **/
      11.     public AssociateArray()
      12.     {
      13.         keyVal = new HashMap<String, String>();
      14.     }
      15.     /** function to clear **/
      16.     public void clear()
      17.     {
      18.         keyVal.clear();
      19.     }
      20.     /** function to get size **/
      21.     public int size()
      22.     {
      23.         return keyVal.size();
      24.     }
      25.     /** function to insert element **/
      26.     public void insert(String key, String val)
      27.     {
      28.         keyVal.put(key, val);
      29.     }
      30.     /** function to get element **/
      31.     public String get(String ele)
      32.     {
      33.         return keyVal.get(ele);
      34.     }
      35.     /** function to remove element **/
      36.     public void remove(String key)
      37.     {
      38.         keyVal.remove(key);
      39.     }
      40.     /** function to modify **/
      41.     public void modify(String key, String val)
      42.     {
      43.         keyVal.put(key, val);
      44.     }
      45. }
      46. /** Class AssociateArrayTest **/
      47. public class AssociateArrayTest
      48. {
      49.     public static void main(String[] args)
      50.     {
      51.         Scanner scan = new Scanner(System.in);
      52.         System.out.println("Associate Array Test\n");
      53.         AssociateArray aa = new AssociateArray();
      54.         char ch;
      55.         /*  Perform Associate Array operations */
      56.         do
      57.         {
      58.             System.out.println("\nAssociate Array <String, String> Operations\n");
      59.             System.out.println("1. put ");
      60.             System.out.println("2. get");
      61.             System.out.println("3. remove");
      62.             System.out.println("4. modify");
      63.             System.out.println("5. clear");
      64.             System.out.println("6. size");
      65.             int choice = scan.nextInt();
      66.             switch (choice)
      67.             {
      68.             case 1 :
      69.                 System.out.println("Enter key and value");
      70.                 aa.insert(scan.next(), scan.next() );
      71.                 break;
      72.             case 2 :
      73.                 System.out.println("Enter element");
      74.                 String ele = scan.next();
      75.                 String str = aa.get(ele);
      76.                 if (str != null)
      77.                     System.out.println("Result : "+ str);
      78.                 else
      79.                     System.out.println("\nError : Not found\n");
      80.                 break;
      81.             case 3 :
      82.                 System.out.println("\nEnter key to be removed");
      83.                 aa.remove(scan.next() );
      84.                 break;
      85.             case 4 :
      86.                 System.out.println("\nEnter key, value to be modified");
      87.                 aa.modify(scan.next(), scan.next() );
      88.                 break;
      89.             case 5 :
      90.                 System.out.println("\nAssociate Array Cleared");
      91.                 aa.clear();
      92.                 break;
      93.             case 6 :
      94.                 System.out.println("\nSize = "+ aa.size() );
      95.                 break;
      96.             default :
      97.                 System.out.println("Wrong Entry \n ");
      98.                 break;
      99.             }
      100.             System.out.println("\nDo you want to continue (Type y or n) \n");
      101.             ch = scan.next().charAt(0);
      102.         } while (ch == 'Y'|| ch == 'y');
      103.     }
      104. }

       

      Associate Array Test
       
       
      Associate Array <String, String> Operations
       
      1. put
      2. get
      3. remove
      4. modify
      5. clear
      6. size
      1
      Enter key and value
      fruit mango
       
      Do you want to continue (Type y or n)
       
      y
       
      Associate Array <String, String> Operations
       
      1. put
      2. get
      3. remove
      4. modify
      5. clear
      6. size
      1
      Enter key and value
      vegetable tomato
       
      Do you want to continue (Type y or n)
       
      y
       
      Associate Array <String, String> Operations
       
      1. put
      2. get
      3. remove
      4. modify
      5. clear
      6. size
      1
      Enter key and value
      drink water
       
      Do you want to continue (Type y or n)
       
      y
       
      Associate Array <String, String> Operations
       
      1. put
      2. get
      3. remove
      4. modify
      5. clear
      6. size
      2
      Enter element
      drink
      Result : water
       
      Do you want to continue (Type y or n)
       
      y
       
      Associate Array <String, String> Operations
       
      1. put
      2. get
      3. remove
      4. modify
      5. clear
      6. size
      2
      Enter element
      fruit
      Result : mango
       
      Do you want to continue (Type y or n)
       
      y
       
      Associate Array <String, String> Operations
       
      1. put
      2. get
      3. remove
      4. modify
      5. clear
      6. size
      4
       
      Enter key, value to be modified
      fruit apple
       
      Do you want to continue (Type y or n)
       
      y
       
      Associate Array <String, String> Operations
       
      1. put
      2. get
      3. remove
      4. modify
      5. clear
      6. size
      2
      Enter element
      fruit
      Result : apple
       
      Do you want to continue (Type y or n)
       
      y
       
      Associate Array <String, String> Operations
       
      1. put
      2. get
      3. remove
      4. modify
      5. clear
      6. size
      6
       
      Size = 3
       
      Do you want to continue (Type y or n)
       
      y
       
      Associate Array <String, String> Operations
       
      1. put
      2. get
      3. remove
      4. modify
      5. clear
      6. size
      3
       
      Enter key to be removed
      fruit
       
      Do you want to continue (Type y or n)
       
      y
       
      Associate Array <String, String> Operations
       
      1. put
      2. get
      3. remove
      4. modify
      5. clear
      6. size
      2
      Enter element
      fruit
       
      Error : Not found
       
       
      Do you want to continue (Type y or n)
       
      y
       
      Associate Array <String, String> Operations
       
      1. put
      2. get
      3. remove
      4. modify
      5. clear
      6. size
      5
       
      Associate Array Cleared
       
      Do you want to continue (Type y or n)
       
      y
       
      Associate Array <String, String> Operations
       
      1. put
      2. get
      3. remove
      4. modify
      5. clear
      6. size
      6
       
      Size = 0
       
      Do you want to continue (Type y or n)
       
      n
      • 0
      • Share
        Share
        • Share onFacebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    Related Questions

    • Codility Passing Car Problem in Java
    • Dutch National Flag Problem in Java
    • Java Program to Check if an Array is Decreasing
    • Java Program to Check if an Array is Increasing
    • Smallest Sum Contiguous Subarray in Java

    Sidebar

    Ask A Question
    • Popular
    • Answers
    • imran

      Point out the wrong statement. A. The standard instances are ...

      • 278 Answers
    • imran

      Point out the correct statement. A. A volume is mounted ...

      • 256 Answers
    • VARISHA PARVEZ

      Which type of e‐commerce focuses on consumers dealing with ...

      • 181 Answers
    • Khushi Jadhav
      Khushi Jadhav added an answer b) Efficiency decreases June 9, 2023 at 7:29 am
    • Khushi Jadhav
      Khushi Jadhav added an answer b) small size and less cost June 9, 2023 at 7:28 am
    • Khushi Jadhav
      Khushi Jadhav added an answer a) Pulsational emf June 9, 2023 at 7:27 am

    Related Questions

    • 30. A noun that dandies neither a male or a ...

      • 2 Answers
    • What does grade 33 cement indicate?

      • 1 Answer
    • . In Unix, which system call creates the new process? ...

      • 1 Answer
    • What are the types of distributed operating systems? a) Zone ...

      • 1 Answer
    • Network operating system runs on ___________ a) every system in ...

      • 8 Answers

    Top Members

    Divya

    Divya

    • 0 Questions
    • 0 Answers
    Aditya sharma

    Aditya sharma

    • 0 Questions
    • 0 Answers
    Vipin verma

    Vipin verma

    • 0 Questions
    • 0 Answers

    Trending Tags

    account algorithm analytics answer c computer computer science python cs e commerce electric electronics it language mcq mechanical physics poll programming python question

    Explore

    • Home
    • Quearn Academy
    • Add group
    • Groups page
    • Communities
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Polls
    • Tags
    • Badges
    • Users
    • Help

    Footer

    Quearn

    Quearn is a social questions & Answers Engine which will help you establish your community and connect with other people, Also you Can Earn Money.

    About Us

    • Meet The Team
    • Blog
    • About Us
    • Contact Us

    Legal Stuff

    • Terms of Use
    • Privacy Policy
    • Cookie Policy

    Help

    • Knowledge Base
    • Support

    Follow

    © 2018-2023 Quearn®. All Rights Reserved | Developed by ASHAS Industries Proudly 🇮🇳