Skip to main content

Implement Maps in java

 Implement Maps in java

It contains values on the basis of key i.e. key and value pair. Each key and value pair is known as an entry. It contains only unique keys. It is useful if you have to search, update or delete elements on the basis of key.

 

Methods of Map interface

·           put(Object key,Object value)-inserts an entry into map

·           get(Object key)-returns value for specified key

·           remove(Object key)-removes an entry for the specified key

·           containsKey(Object key)-search specified key from the map

·           keySet()-returns Set view containing all keys

·           entrySet()-returns Set view containing all keys and values

·           putAll(Map m)-inserts specified map in this map.

Implementation of Map interface

·              HashMap

·              LinkedHashMap

·              TreeMap

TreeMap

It implements Map interface using a tree which provides efficient way of storing key/value pairs in sorted order

 Features

·         It contains values based on key

·         It contains only unique elements

·         It is similar to HashMap instead it store the key/value pairs in sorted order.

         Constructors

·      TreeMap()-constructs an empty treemap that will be sorted based on key

·      TreeMap(Map m)-initializes treemap with m which will be sorted based on key

        TreeMap program

import java.util.Map;//Map is interface

import java.util.Map.Entry;//Map.Entry is sub interface of Map

import java.util.TreeMap;

import java.util.Scanner;//Scanner class reads data from input stream

class Treemapnew

{

 public static void main(String[] args)

 {

  Map<Integer,String> obj=new TreeMap<Integer,String>();//Map is interface and TreeMap is class

  Scanner s=new Scanner(System.in);

  while(true)

  {

   System.out.println("1.Add");

   System.out.println("2.Remove");

   System.out.println("3.Display");

   System.out.println("4.Quit");

   System.out.println("Enter choice");

   int choice=s.nextInt();

   switch(choice)

   {

     case 1:System.out.println("Enter the key(id) to be added to treemap");

            int key=s.nextInt();

            System.out.println("Enter the value(name) to be added to treemap");

            String value=s.next();

           obj.put(key,value);//place <key,value> into treemap

            break;

     case 2:if(obj.isEmpty())//checks whether treemap is empty or not

            System.out.println("Treemap is empty");

            else

            {

            System.out.println("Enter the key to be removed from treemap");

            key=s.nextInt();

            if(obj.containsKey(key))//containsKey() checks whether treemap contains key or not

            {

            value=obj.get(key);

            obj.remove(key);

            System.out.println("<"+key+","+value+"> pair is removed from treemap successfully");

            }

            else

            System.out.println("Key not found in treemap to be deleted");

            }

            break;

     case 3:if(obj.isEmpty())

             System.out.println("treemap is empty");

            else

            {

            System.out.println("Treemap elements");

            for( Map.Entry<Integer,String> m:obj.entrySet())

          //Map.entry interface and entrySet() returns <key,value> pair

            System.out.println(m.getKey()+","+m.getValue());

         //Object[] a=obj.keySet().toArray();//obj.keySet() returns set of keys and converts the given set into array

        //now using get(key) of TreeMap retrieve value

   /*for(int i=0;i<obj.size();++i)

   {

     System.out.println(obj.get(a[i]));

   }  */       

          }

            break;

     case 4:System.exit(0);

     default:System.out.println("Wrong choice");

    }

   }

  }

}

Expected Output

1.Add

2.Remove

3.Display

4.Quit

Enter choice

1

Enter the key(id) to be added to treemap

100

Enter the value(name) to be added to treemap

raju

1.Add

2.Remove

3.Display

4.Quit

Enter choice

1

Enter the key(id) to be added to treemap

95

Enter the value(name) to be added to treemap

ravi

1.Add

2.Remove

3.Display

4.Quit

Enter choice

1

Enter the key(id) to be added to treemap

98

Enter the value(name) to be added to treemap

venu

1.Add

2.Remove

3.Display

4.Quit

Enter choice

3

Treemap elements

95,ravi

98,venu

100,raju

1.Add

2.Remove

3.Display

4.Quit

Enter choice

2

Enter the key to be removed from treemap

96

Key not found in treemap to be deleted

1.Add

2.Remove

3.Display

4.Quit

Enter choice

2

Enter the key to be removed from treemap

95

<95,ravi> pair is removed from treemap successfully

1.Add

2.Remove

3.Display

4.Quit

Enter choice

3

Treemap elements

98,venu

100,raju

1.Add

2.Remove

3.Display

4.Quit

Enter choice

2

Enter the key to be removed from treemap

100

<100,raju> pair is removed from treemap successfully

1.Add

2.Remove

3.Display

4.Quit

Enter choice

3

Treemap elements

98,venu

1.Add

2.Remove

3.Display

4.Quit

Enter choice

2

Enter the key to be removed from treemap

98

<98,venu> pair is removed from treemap successfully

1.Add

2.Remove

3.Display

4.Quit

Enter choice

3

treemap is empty

1.Add

2.Remove

3.Display

4.Quit

Enter choice

2

Treemap is empty

1.Add

2.Remove

3.Display

4.Quit

Enter choice

4

Comments

Popular posts from this blog

Array of Objects

An array can be of any data type including struct. Similarly, we can also have arrays of variables of the type class. Such variables are called arrays of objects. Class Definition: class employee {           char name[30];          float age;     public:          void getdata(void);           void putdata(void); }; The identifier employee is a user-defined data type and can be used to create objects related to different employee categories. employee manage[3];          //aray of managers employee foreman[15];       //array of foreman employee worker[75];        // array of worker the array manager contains three objects(managers), namely, manager[0],  manager[1], and manager[2], of type employee class similarly, the foreman array contains 15 objects. and the worker array contains 75 objectives.(work...

Binning Method by Data smoothing in python

 Binning Method Binning is a technique for smoothing data or dealing with noisy data. The data is sorted first, and then the sorted values are dispersed into a number of buckets or bins in this approach. Binning methods provide local smoothing since they consult the vicinity of values.  Smoothing can be accomplished in three ways: Bin smoothing entails:  Each value in a bin is replaced by the bin's mean value when smoothing by bin means is used.  Smoothing by bin median:  Each bin value is replaced by its bin median value in this method.  Smoothing by bin borders:  In smoothing by bin boundaries, the bin boundaries are determined as the minimum and maximum values in a given bin. The nearest boundary value is then used to replace each bin value. Example: Sorted data for price (in dollars): 4, 8, 9, 15, 21, 21, 24, 25, 26, 28, 29, 34 Smoothing by bin means:       - Bin 1: 9, 9, 9, 9       - Bin 2: 23, 23, 23, 23   ...

Hadoop file Management Tasks

  Implement the following file management tasks in Hadoop: a) Adding files and directories b) Retrieving files c) Deleting files Hint: A typical Hadoop workflow creates data files (such as log files) elsewhere and copies them into HDFS using one of the above command line utilities. Program:  The most common file management tasks in Hadoop includes: Adding files and directories to HDFS Retrieving files from HDFS to local filesystem Deleting files from HDFS Hadoop file commands take the following form:     hadoop fs - cmd Where cmd is the specific file command and <args> is a variable number of arguments. The command cmd is usually named after the corresponding Unix equivalent. For example, the command for listing files is ls as in Unix. a) Adding Files and Directories to HDFS Creating Directory in HDFS    $ hadoop fs - mkdir foldername (syntax)  $ ha...