Jetpack Compose - Row、Column
阿新 • • 發佈:2020-12-13
Map
- Map和Collection沒有繼承關係
- Map以鍵值對的方式儲存資料
- Map的常用方法
public class MapTest { public static void main(String[] args) { /** Map的常用方法 * 1.V put(K key, V value) 新增元素 * 2.V get(Object key) 根據key獲取value * 3.void clear() 清空集合 * 4.boolean containsKey(Object key) 判斷是否包含key,底層呼叫equals * 5.boolean containsValue(Object value) 判斷是否包含value,底層呼叫equals * 6.int size() 集合包含鍵值對元素的個數 * 7.boolean isEmpty() 集合是否為空 * 8.Set<K> keySet() 獲取所有的key,並存入set集合 * 9.Collection<V> values() 獲取所有的value,並存入Collection集合 * 10.Set<Map.Entry<K,V>> entrySet() 獲取所有的key和value,並存入set集合 */ Map<Integer,String> map = new HashMap<>(); map.put(1, "jqc"); map.put(3, "jqc2"); System.out.println(map.get(3));//jqc2 System.out.println(map.containsKey(1));//true System.out.println(map.containsValue("jqc"));//true System.out.println(map.size());//2 System.out.println(map.isEmpty());//false map.clear(); System.out.println(map.size());//0 //Map的遍歷 map.put(1,"士大夫"); map.put(3,"sdf"); //方法一:先獲取所有的key,再根據key獲取value Set<Integer> set = map.keySet(); //迭代器遍歷 Iterator<Integer> it = set.iterator(); while (it.hasNext()){ Integer next = it.next(); String v = map.get(next); System.out.println(next+"="+v); } System.out.println("====================="); //foreach遍歷(效率高) for(Integer i : set){ System.out.println(i+"="+map.get(i)); } System.out.println("====================="); //方法二,先獲取Set<Map.Entry<K,V>>集合 Set<Map.Entry<Integer, String>> entrySet = map.entrySet(); //迭代器遍歷 Iterator<Map.Entry<Integer, String>> it2 = entrySet.iterator(); while (it2.hasNext()){ Map.Entry<Integer, String> entry = it2.next(); System.out.println(entry.getKey()+"="+entry.getValue()); } System.out.println("===================="); //foreach遍歷 for (Map.Entry<Integer, String> en : entrySet) { System.out.println(en.getKey()+"="+en.getValue()); } } }