首页 > 代码库 > Map接口的一些常用方法

Map接口的一些常用方法

public class NewTestMap {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
        
        map.put("qqq", "淄博张店");
        map.put("www", "潍坊");
        map.put("eee", "桓台");
        map.put("rrr", "枣庄");
        
        Set<String> keySet = map.keySet();
        for(Object o : keySet) {
            System.out.println(o);
        }
        Collection<String> c = map.values();
        System.out.println(keySet);
        System.out.println(c);
        
        List<String> list = new ArrayList<String>();
        Map<String, Integer> map1 = new HashMap<String, Integer>();
        Set<String> set = new HashSet<String>();
      System.out.println(map.get("rrr"));
        //Object o = map.remove("qqq");
        Object o = map.remove("qqq", new String("淄博张店"));
        System.out.println(o);
        System.out.println(map);
        System.out.println("是否包含某个键: "+map.containsKey("fff"));
        System.out.println("是否包含某个键的值: "+map.containsValue("桓台"));
        System.out.println(map.size());

  例题:用集合(List, Set, Map中任意一种)的知识把星座的题目再写一下

public class PractiseNew {
    public static void main(String[] args) {
      String _info = "天蝎座,金牛座";    Map
<String, String> map = new HashMap<String, String>(); String[] xzName = { "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "双鱼座" }; String[] xzDate = { "03月21日─04月20日", "04月21日─05月20日", "05月21日─06月21日", "06月22日─07月22日", "07月23日─08月22日", "08月23日─09月22日", "09月23日─10月22日", "10月23日─11月21日", "11月22日─12月21日", "12月22日─01月19日", "01月20日─02月18日", "02月19日─03月20日" }; for (int i = 0; i < xzName.length; i++) { map.put(xzName[i], xzDate[i]); } String[] infos = _info.split(","); for (String s : infos) { System.out.println(map.get(s) == null ? "没有找到该星座的日期" : s + ":" + map.get(s)); } } }

 

Map接口的一些常用方法