• Map接口


      实现Map接口的类用来存储键—值 对。

    Map接口的实现类有HashMap和TreeMap等。

    Map类中存储的键—值通过键来标识,所以键值不能重复

    Object put (Object key, Object value)//返回值是旧的value值, 如果传入的key已经存在的话,新的value会替换旧的value

    Object get (Object key) ;//拿到相对应的value对象

    Object remove (Object key) ;//移除相对应的value对象

    boolean containsKey (Object key) ;//是否包含这个key

    boolean containsValue(Object value)//是否包含这个值

    int size ();//一共多少对对象

    boolean isEmpty();是不是空的

    void putAll(Map t);把另外一个Map全部的东西都加进来

    void clear();//清除

    Map集合遍历的方式1

    package com.liushuaishuai;
    /*
    Map集合的遍历(方式1)
        1获取所有键的集合用keySet
        2遍历键的集合,获取每一个键,用增强for
        3根据键去找值,用get(Object key)方法实现
     */
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    public class MapDemo01 {
        public static void main(String[] args) {
            //创建集合对象
            Map<String,String> map = new HashMap<>();
            
            //添加元素
            map.put("杨过","小龙女");
            map.put("郭靖","黄蓉");
            map.put("张无忌","周芷若");
            //获取所有的键组成的集合,用keySet()方法实现
            Set<String> keyset = map.keySet();
            
    //        遍历上述得到的键的集合,使用增强for
            for(String s:keyset) {
                //根据键找值,使用get(Object key)方法实现
                String s1 = map.get(s);
                System.out.println(s+","+s1);
            }
    
        }
    }
    

     Map集合遍历方式2

     

    例子如下:

    import java.util.List;
    import java.util.LinkedList;
    import java.util.Collections;
    import java.util.*;
    public class Test {
    	public static void main(String[] args) {
    		Map m1 = new HashMap();Map m2 = new TreeMap();
    		m1.put("one",new Integer(1));
    		m1.put("two",new Integer(2));
    		m1.put("three",new Integer(3));
    		m2.put("A",new Integer(1));
    		m2.put("B",new Integer(2));
    		System.out.println(m1.size());
    		System.out.println(m1.containsKey("one"));
    		System.out.println(m2.containsValue(new Integer(1)));
    		if(m1.containsKey("two")) {
    			int i = ((Integer)m1.get("two")).intValue();
    			System.out.println(i);
    		}
    		Map m3 = new HashMap(m1);
    		m3.putAll(m2);
    		System.out.println(m3);
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		/*List l1 = new LinkedList();
    		l1.add(new Name("Karl","M"));
    		l1.add(new Name("Steven","Lee"));
    		l1.add(new Name("John","O"));
    		l1.add(new Name("Tom","M"));
    		System.out.println(l1);
    		Collections.sort(l1);
    		System.out.println(l1);*/
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		/*
    		Set s = new HashSet();
    		s.add("hello");
    		s.add("world");
    		s.add(new Name("f1","11"));
    		s.add(new Integer(100));
    		*/
    		
    		/*
    		
    		s.add("hello");
    		s.add("hello");
    		*/
    		//Set 
    		/*
    		Set s1 = new HashSet();
    		Set s2 = new HashSet();
    		s1.add("a");s1.add("b");s1.add("c");
    		s2.add("d");s2.add("a");s2.add("b");
    		Set sn = new HashSet(s1);
    		sn.retainAll(s2);
    		Set su = new HashSet(s1);
    		su.addAll(s2);
    		
    		System.out.println(sn);
    		
    		System.out.println(su);
    		*/
    		
    		
    		
    		
    		
    		
    		
    		
    		/*
    		Collection c = new HashSet();
    		c.add("hello");
    		c.add(new Name("f1","11"));
    		c.add(new Name("f2","12"));
    		c.add(new Name("f3","13"));
    		c.add(new Integer(100));
    		c.remove("hello");
    		c.remove(new Integer(100));
    		
    		Iterator i = c.iterator();
    		while(i.hasNext()) {
    			Name n = (Name)i.next();
    			System.out.print(n.getfirstName()+" ");
    		}*/
    		/*System.out.println(c.remove(new Name("f1","11")));
    		System.out.println(c);*/
    	}
    }
    class Name implements Comparable {
    	private String firstName,secondName;
    	public Name(String firstName,String secondName) {
    		this.firstName = firstName;
    		this.secondName = secondName;
    	}
    	public String getfirstName() {return firstName;}
    	public String getsecondName() {return secondName;}
    	public String toString() {
    		return firstName+" "+secondName;
    	}
    	
    	public boolean equals(Object obj) {
    		if(obj instanceof Name) {
    			Name name = (Name) obj;
    			return (firstName.equals(name.firstName))&&(secondName.equals(name.secondName));
    		}
    		return super.equals(obj);
    	}
    	
    	public int hashCode() {
    		return firstName.hashCode();
    	}
    	
    	public int compareTo(Object o) {
    		Name n = (Name) o;
    		int lastCmp = 
    				secondName.compareTo(n.secondName);
    		return
    					(lastCmp!=0 ? lastCmp:firstName.compareTo(n.firstName));
    	}
    }
    

     用hashmap来统计字符串中字母出现的次数

    package com.maptest01;
    
    import java.util.HashMap;
    import java.util.Scanner;
    import java.util.Set;
    
    public class staticchar {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入一个字符串");
            String s = sc.nextLine();
            HashMap<Character, Integer> hm1 = new HashMap<Character, Integer>();
    
    
            for (int i = 0; i < s.length(); i++) {
                char key = s.charAt(i);
                Integer value = hm1.get(key);
                if (value == null) {
                    hm1.put(key, 1);
    
                } else {
                    value++;
                    hm1.put(key, value);
                }
            }
            Set<Character> keyset = hm1.keySet();
            for (Character x : keyset) {
                Character key = x;
                Integer value = hm1.get(key);
                System.out.print(key + "(" + value + ")");
            }
        }
    }
    
  • 相关阅读:
    poj 1035 (Spell checker )
    poj 3080 (暴力 strstr)
    kmp 模版
    匈牙利算法模版
    poj 1274 The Perfect Stall (最大匹配)
    hdu 1083 Courses(二分图 )
    pku 3363(内部测试赛)
    Linux 下联网脚本文件
    Qt 多国语言
    引用和引用参数
  • 原文地址:https://www.cnblogs.com/lsswudi/p/11366011.html
Copyright © 2020-2023  润新知