Is there something like NSDictionary in android?

I am new to Android. I searched everything but couldn't find anything like iOS NSdictionary in Android. For example, on iOS, I can create a SIMPLE array such as this format

Array idx1: [objA1 for keyA],[objB1 for keyB],[objB1 for keyC] idx2: [objA2 for keyA],[objB2 for keyB],[objB2 for keyC] idx3: [objA3 for keyA],[objB3 for keyB],[objB3 for keyC] 

I know I can create a string array that works similarly in android

 <string-array name="list_obj1"> <item>ObjA1</item> <item>ObjB2</item> <item>ObjC3</item> </string-array> <string-array name="list_obj2"> <item>ObjB1</item> <item>ObjB2</item> <item>ObjB3</item> </string-array> <string-array name="list_obj3"> <item>ObjC1</item> <item>ObjC2</item> <item>ObjC3</item> </string-array> 

My question is is there anything else that is used to create an AN dictionary array on Android, like iOS.

Thank you for your help.

+6
source share
3 answers

Firstly, I think there are many tutorials on this material, then you can find additional information. Since you are new to android, you may not know the "name" to search. For this case, “HashMap” is what you are looking for. It works like NSdictionary.

 //Create a HashMap Map <String,String> map = new HashMap<String,String>(); //Put data into the HashMap map.put("key1","Obj1"); map.put("key2","Obj2"); map.put("key3","Obj3"); // Now create an ArrayList of HashMaps ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); //Add the HashMap to the ArrayList mylist.add(map); 

Now you have st, like an array of vocabulary.

I hope for this help.

+14
source

You can specify what @ user1139699 said in an ArrayList.

 ArrayList<HashMap> list = new ArrayList(); Map <String, String> map = new HashMap<String,String>(); map.put("key","Obj"); list.add(map); 
+2
source

also if you want to download the file as follows:

 property.key.1=value of 1st key property.key.2=value of 2nd key prompt.alert = alert 

etc. you can use java Properties (); Then you can instantly get the value of each key.



Explanation:

You have myTranslations.txt file. In this file, you write key / value pairs in the format:

 property.key.1=value of 1st key property.key.2=value of 2nd key prompt.alert = alert 

where the part before the "=" symbol is the key, and the part after is the value.

Then in your code you do:

 Properties properties = new Properties(); File propertiesFile = new File (filePath); FileInputStream inputStream = null; try { inputStream = new FileInputStream(propertiesFile); properties.load(inputStream); } catch (IOException ioe) { ioe.printStackTrace(); } 

where filePath is the file path above.

Then you can get the value of each key as:

 properties.get("prompt.alert") 

which will return a string:

 alert 

as in the txt file.

If this helps, please refrain.

0
source

Source: https://habr.com/ru/post/922224/


All Articles