Home » Android » keymapping – How to use Dictionary (like iphone NSDictionary) in android?

keymapping – How to use Dictionary (like iphone NSDictionary) in android?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have worked in soap message, and to parse the value from Webservice, the values are stored in ArrayList.

Example:
values are Employee name (Siva) and Employee id (3433fd), these two values are stored in arraylist, but I want to stored in Dictionary, How?

How to&Answers:

you can use HashMap like this

Map <String,String> map =  new HashMap<String,String>();
//add items 
map.put("3433fd","Siva");
//get items 

String employeeName =(String) map.get("3433fd");

Answer:

You can use Bundle.

as it offers String to various types of Mapping.

Bundle b = new Bundle();
b.putInt("identifier", 121);
b.putString("identifier", "Any String");
b.putStringArray("identifier", stringArray);

int i = b.getInt("identifier");
...

Answer:

 public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        EditText textview = (EditText) findViewById(R.id.editTextHash);
        EditText textviewNew = (EditText) findViewById(R.id.editTextHash2);

        Map<String, String> map = new HashMap<String,String>();
        map.put("iOS", "100");
        map.put("Android", "101");
        map.put("Java", "102");
        map.put(".Net", "103");

        String TextString = "";
       // Set<E> keys = map.keySet();

       Set keys = map.keySet();
       System.out.println("keys "+keys);
        for (Iterator i = keys.iterator(); i.hasNext();) 
        {
            String key = (String) i.next();
            System.out.println("key "+key);
            String value = (String) map.get(key);
            System.out.println("value "+value);
            textview.append(key + " = " + value);
            TextString+=(key + " = " + value); 
        }
        //textviewNew.setText(TextString);

//        Iterator iterator = map.keySet().iterator();
//        
//        while (iterator.hasNext()) 
//        {
//          String object = (String) iterator.next();
//          textview.setText(object);
//      }
//        
//        //textview.setText(map.get("Siva"));
//        
//        System.out.println(map);

    }
}

Answer:

A Dictionary is an abstract class that maps keys to values and there is Dictionary Class in android refer link you can find a note at Class Overview

Do not use this class since it is obsolete. Please use the Map
interface for new implementations.

An attempt to insert either a null key or a null value to a dictionary will result to a NullPointerException,but you can use Map interface,it provides the exact same functionality of a dictionary.you can use it as bellow

//put values
Map Message = new HashMap();
Message.put("title", "Test message");
Message.put("description", "message description");
Message.put("email", "[email protected]");
//get values
Log.d("get Message","Message title -"+Message.get("title"));

you can also use A custom class as below

public class MessageObject {
    String title;
    String description;
    String email;
}

you can use Getters and Setters to get and put values,not needed to remember the key names every time you may get some other advantages by this way.