Map最常用的两个:Hashmap和Treemap
Map的方法
put(key:k,valu:v)返回值是v类型的
get(key:k)返回值是v
keyset
remove
put的使用
public class HashMapPractice { public static void main(String[] args) { HashMap<String ,String > map = new HashMap<>(); map.put("name","shangsan"); map.put("gender","clae"); map.put("mobile","12312313215"); System.out.println(map); } }
get的使用
public class HashMapPractice { public static void main(String[] args) { HashMap<String ,String > map = new HashMap<>(); map.put("name","shangsan"); map.put("gender","clae"); map.put("mobile","12312313215"); System.out.println(map.get("name")); } }
remove的使用
public class HashMapPractice { public static void main(String[] args) { HashMap<String ,String > map = new HashMap<>(); map.put("name","shangsan"); map.put("gender","clae"); map.put("mobile","12312313215"); map.remove("name"); System.out.println(map); } }
keyset
public class HashMapPractice { public static void main(String[] args) { HashMap<String ,String > map = new HashMap<>(); map.put("name","shangsan"); map.put("gender","clae"); map.put("mobile","12312313215"); Set<String>keySet = map.keySet(); System.out.println(keySet); }
}
entries的使用
public class HashMapPractice { public static void main(String[] args) { HashMap<String ,String > map = new HashMap<>(); map.put("name","shangsan"); map.put("gender","clae"); map.put("mobile","12312313215"); Set<Map.Entry<String, String>> entries = map.entrySet(); for (Map.Entry<String, String> entry : entries) { System.out.println(entry.getValue()); System.out.println(entry.getKey()); } } }
在map中(key,valu)key代表赋值的类型 valu代表赋值