JAVA学习--集合Map的使用

来源:互联网 发布:2016美国大选结果数据 编辑:程序博客网 时间:2024/04/29 08:55

 * Map接口
 *       |-----HashMap:Map的主要实现类
 *       |-----LinkedHashMap:使用链表维护添加进Map中的顺序。故遍历Map时,是按添加的顺序遍历的。
 *       |-----TreeMap:按照添加进Map中的元素的key的指定属性进行排序。要求:key必须是同一个类的对象!
 *             针对key:自然排序   vs定制排序
 *       |-----Hashtable:古老的实现类,线程安全,不建议使用。
 *          |----Properties:常用来处理属性文件。键和值都为String类型的




----------------------------------------------------------------------------------------------------------------
     Object put(Object key,Objectvalue):向Map中添加一个元素
     Object remove(Objectkey):按照指定的key删除此key-value
     void putAll(Map t)
     void clear():清空
     Object get(Objectkey):获取指定key的value值。若无此key,则返回null
     boolean containsKey(Object key)
     boolean containsValue(Object value)
     int size():返回集合的长度
     boolean isEmpty() boolean equals(Objectobj)
     HashMap:1.key是用Set来存放的,不可重复。value是用Collection来存放的,可重复
    一个key-value对,是一个Entry。所有的Entry是用Set存放的,也是不可重复的。
    2.向HashMap中添加元素时,会调用key所在类的equals()方法,判断两个key是否相同。若相同则只能添加 进后添加的那个元素。


   
   
    @Test
    public voidtest1() {
       Map map =new HashMap();
      map.put("AA", 213);
      map.put("BB", 456);
      map.put("BB", 45);
       map.put(123,"CC");
      map.put(null, null);
       map.put(newPerson("DD", 23), 89);
       map.put(newPerson("DD", 23), 87);
      System.out.println(map.size());
      System.out.println(map);
      map.remove("BB");
      System.out.println(map);
       Object value= map.get(1234);
      System.out.println(value);
    }

--------------------------------------------------------------------------------------------------------
    * 如何遍历Map Set keySet() Collection values() SetentrySet()

    @Test
    public voidtest2() {
       Map map =new HashMap();
      map.put("AA", 213);
      map.put("BB", 45);
       map.put(123,"CC");
      map.put(null, null);
       map.put(newPerson("DD", 23), 89);

       //1.遍历key集。
       Set set =map.keySet();
       for (Objectobj : set) {
         System.out.println(obj);
       }
       //2.遍历value集
       Collectionvalues = map.values();
       Iterator i =values.iterator();
       while(i.hasNext()) {
         System.out.println(i.next());
       }
       //3.如何遍历key-value对。
       //方式一:
       Set set1 =map.keySet();
       for (Objectobj : set1) {
         System.out.println(obj + "----->" + map.get(obj));
       }
       //方式二:
       Set set2 =map.entrySet();
       for (Objectobj : set2) {
          Map.Entryentry = (Map.Entry) obj;
          //System.out.println(entry.getKey() + "---->" +entry.getValue());
         System.out.println(entry);
       }
    }

-------------------------------------------------------------------------------------------------------------
**LinkedHashMap用法

    @Test
    public voidtest3() {
       Map map =new LinkedHashMap();
      map.put("AA", 213);
      map.put("BB", 45);
       map.put(123,"CC");
      map.put(null, null);
       map.put(newPerson("DD", 23), 89);

       Set set1 =map.keySet();
       for (Objectobj : set1) {
         System.out.println(obj + "----->" + map.get(obj));
       }
    }

------------------------------------------------------------------------------------------------------

    //自然排序
    @Test
    public voidtest4() {
       Map map =new TreeMap();
       map.put(newPerson("AA", 23), 89);
       map.put(newPerson("MM", 22), 79);
       map.put(newPerson("GG", 23), 99);
       map.put(newPerson("JJ", 13), 69);

       Set set1 =map.keySet();
       for (Objectobj : set1) {
         System.out.println(obj + "----->" + map.get(obj));
       }
    }

--------------------------------------------------------------------------------------------------------

    //定制排序
    @Test
    public voidtest5() {
       Comparatorcom = new Comparator() {
          public intcompare(Object o1, Object o2) {
             if (o1instanceof Customer && o2 instanceof Customer) {
                Customer c1= (Customer) o1;
                Customer c2= (Customer) o2;
                int i =c1.getId().compareTo(c2.getId());
                if (i == 0){
                   returnc1.getName().compareTo(c2.getName());
                }
                returni;
             }
             return0;
          }
       };
       TreeMap map= new TreeMap(com);
       map.put(newCustomer("AA", 1001), 87);
       map.put(newCustomer("CC", 1001), 67);
       map.put(newCustomer("MM", 1004), 77);
       map.put(newCustomer("GG", 1002), 97);
      
       Set set1 =map.keySet();
       for (Objectobj : set1) {
         System.out.println(obj + "----->" + map.get(obj));
       }
    }


-----------------------------------------------------------------------------------以下为填充集合的类

class Customer {
 
  private String name;
   private Integer id;
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public Integer getId() {
      return id;
   }
   public void setId(Integer id) {
      this.id = id;
   }
   public Customer(String name, Integer id) {
      super();
      this.name = name;
      this.id = id;
   }
   public Customer() {
      super();
   }
   @Override
   public String toString() {
      return "Customer [name=" + name + ", id=" + id + "]";
   }
   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((id == null) ? 0 : id.hashCode());
      result = prime * result + ((name == null) ? 0 :name.hashCode());
      return result;
   }
   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      Customer other = (Customer) obj;
      if (id == null) {
         if (other.id != null)
            return false;
      } else if (!id.equals(other.id))
         return false;
      if (name == null) {
         if (other.name != null)
            return false;
      } else if (!name.equals(other.name))
         return false;
      return true;
   }
   
}

class Person implementsComparable{
   private String name;
   private Integer age;
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public Integer getAge() {
      return age;
   }
   public void setAge(Integer age) {
      this.age = age;
   }
   public Person() {
      super();
   }
   public Person(String name, Integer age) {
      super();
      this.name = name;
      this.age = age;
   }
   @Override
   public String toString() {
      return "Person [name=" + name + ", age=" + age + "]";
   }
   //static int init = 1000;
   @Override
   public int hashCode() {//return age.hashCode() +name.hashCode();没下述的健壮性好。
      final int prime = 31;
      int result = 1;
      result = prime * result + ((age == null) ? 0 :age.hashCode());
      result = prime * result + ((name == null) ? 0 :name.hashCode());
      return result;
      //return init++;//不能这样用
   }
   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      Person other = (Person) obj;
      if (age == null) {
         if (other.age != null)
            return false;
      } else if (!age.equals(other.age))
         return false;
      if (name == null) {
         if (other.name != null)
            return false;
      } else if (!name.equals(other.name))
         return false;
      return true;
   }
   //当向TreeSet中添加Person类的对象时,依据此方法,确定按照哪个属性排列。
   @Override
   public int compareTo(Object o) {
      if(o instanceof Person){
         Person p = (Person)o;
         //return this.name.compareTo(p.name);
         //return -this.age.compareTo(p.age);
         int i = this.age.compareTo(p.age);
         if(i == 0){
            return this.name.compareTo(p.name);
         }else{
            return i;
         }
      }
      return 0;
   }
   
}


0 0
原创粉丝点击