Android hashmap question!

Asked 2 years ago, Updated 2 years ago, 33 views

HashMap<String,ArrayList> mapList = HashMap<String,ArrayList>();
ArrayList f_time = new ArrayList();
ArrayList f_lat = new ArrayList();
ArrayList f_log = new ArrayList();

f_time ={1,2,3,4}
f_lat - {1.1, 1.2, 1.3, 1.4}
f_log - {2.1, 2.2, 2.3, 2.4}
key = {1,2};

Assume that it is stored like this.

On a hash map called mapList,

key=1;
mapList.put(key,f_time);
mapList.put(key,f_lat);
mapList.put(key,f_log);

Can I save it like this and select f_time, f_lat, and f_log by key value? For example, can we take the 3rd value of f_time for key value 1?

android

2022-09-22 14:36

1 Answers

In the case of key, I didn't understand the example, so I changed it properly.

The above code only stores data from mapList.put(key,f_log) because the map does not allow duplicate key values.

Get the ArrayList value, which is the value value, as the key value, and then access the index of the ArrayList. In addition to keySet, map data can be imported using entrySet and iterator.

HashMap<String, ArrayList> mapList = new HashMap<String, ArrayList>();

ArrayList f_time = new ArrayList(Arrays.asList(1, 2, 3, 4));
ArrayList f_lat = new ArrayList(Arrays.asList(1.1, 1.2, 1.3, 1.4));
ArrayList f_log = new ArrayList(Arrays.asList(2.1, 2.2, 2.3, 2.4));

mapList.put("1", f_time);
mapList.put("2", f_lat);
mapList.put("3", f_log);

for (String key : mapList.keySet()) {
    System.out.println("key ::: " + key);
    ArrayList list = mapList.get(key);
    System.out.println("value ::: " + list.get(2));
    System.out.println("values ::: " + list.toString());
}

This is a ++.

http://tryhelloworld.co.kr/courses/ Java-Intermediate/lessons/Collection-Framework

http://tryhelloworld.co.kr/courses/ Java-Intermediate/lessons/set

http://tryhelloworld.co.kr/courses/ Java-Intermediate/lessons/list

http://tryhelloworld.co.kr/courses/ Java-Intermediate/lessons/map


2022-09-22 14:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.