java基础--16

来源:互联网 发布:java string 字符集 编辑:程序博客网 时间:2024/04/24 21:37

1、Map概述
 该集合存储键值对。一对一对往里存,而且要保证键的唯一性
 添加
  V put(K,V)
  void putall
 删除
  clear()
  V remove(Object key)
 判断
  boolean containsKey(Object key)
  boolean containsValue(Object value)
  isEmpty()
 获取
  V get(Object K)
  size()
  vlaues()

  entrySet()
  keySet()
2、Map集合子类对象特点
 Map
  Hashtable:底层是哈希表数据结构,不可以存入null键null值。该集合是线程同步的。
  HashMap:底层是哈希表数据结构,运行使用null键和null值,该集合是不同步的。
  TreeMap:底层是二叉树数据结构。线程不同步,可以用于给map集合中的键进行排序
 和Set很像,其实Set底层就是使用了Map集合
3、Map共性方法
 V put(K,V)//添加元素,如果出现添加时,相同的值,那么后添加的值会覆盖原有的键对应值,并put方法返回被覆盖的值
 V remove(Object key)
 boolean containsKey(Object key)
4、Map-KeySet
 map集合的两种取出方式:
  Set<k> keySet:将Map中所有的键存入到Set集合,因为Set具备迭代器,所以可以用迭代方式取出所有的键,再根据get方法获取每个键对应的值
  Set<Map.Entry<k,v>> entrySet
5、Map-entrySet
 Map集合的取出原理:将Map集合转成Set集合,再通过迭代器取出
 Set<Map.Entry<k,v>> entrySet:将map集合中的映射关系存入到了Set集合中,而这个关系的数据类型就是:Map.Entry

 Entry其实就是Map中的一个static内部接口。
 为什么要定义在内部呢?
 因为只有有了Map集合,有了键值对,才会有键值的映射关系。
 关系属于Map集合中的一个内部事物。
    而且该事物在直接访问Map集合中的元素。
6、Map练习
 import java.util.*;

public class MapTest
{
 public static void main(String[] args) 
 {
  HashMap<Student,String> hm=new HashMap<Student,String>();
  
  hm.put(new Student("lisi1",21),"beijing");
  hm.put(new Student("lisi1",21),"tianjin");
  hm.put(new Student("lisi2",22),"shanghai");
  hm.put(new Student("lisi3",23),"nanjing");
  hm.put(new Student("lisi4",24),"wuhan");
  
  /*Set<Student> keySet=hm.keySet();
  Iterator<Student> it=keySet.iterator();
  while(it.hasNext())
  {
   Student stu=it.next();
   String addr=hm.get(stu);
   
   System.out.println(stu+":"+addr);
  }*/
   Set<Map.Entry<Student,String>> entrySet=hm.entrySet();
   Iterator<Map.Entry<Student,String>> it=entrySet.iterator();
   while(it.hasNext())
   {
    Map.Entry<Student,String> me=it.next();
    Student stu=me.getKey();
    String addr=me.getValue();
    System.out.println(stu+":"+addr);
   }   
 }
}

class Student implements Comparable<Student>
{
 private String name;
 private int age;
 
 Student(String name,int age)
 {
  this.name=name;
  this.age=age;
 }
 
 public String getName()
 {
  return name;
 }
 
 public int getAge()
 {
  return age;
 }
 
 public void setName(String name)
 {
  this.name=name;
 }
 
 public void setAge(int age)
 {
  this.age=age;
 }
 
 public int compareTo(Student s)
 {
  int num=this.name.compareTo(s.name);
  
  if(num==0)
  {
   return new Integer(this.age).compareTo(new Integer(s.age));
  }
  return num;
 }
 
 public int hashCode()
 {
  return name.hashCode()+age*29;
 }
 
 public boolean equals(Object obj)
 {
  if(!(obj instanceof Student))
  {
   throw new ClassCastException("不是Student对象");
  }
  
  Student s=(Student)obj;
  return this.name.equals(s.name) && this.age==s.age;
 }
 
 public String toString()
 {
  return name+":"+age;
 }

7、TreeMap练习
 import java.util.*;

public class MapTest2
{
 public static void main(String[] args) 
 {
  //TreeMap<Student,String> tm=new TreeMap<Student,String>();//Student类的描述中,继承了Comparable,默认的排序方式为以姓名为主要原因
  TreeMap<Student,String> tm=new TreeMap<Student,String>(new StuAgeComparator());//在StuAgeComparator()中,以年龄为主要排序原因
  
  tm.put(new Student("lisi2",22),"shanghai");
  tm.put(new Student("lisi1",21),"beijing");
  //tm.put(new Student("lisi1",21),"tianjin");  
  tm.put(new Student("alisi3",23),"nanjing");
  tm.put(new Student("lisi4",24),"wuhan");
  
  Set<Map.Entry<Student,String>> entrySet=tm.entrySet();
   Iterator<Map.Entry<Student,String>> it=entrySet.iterator();
   while(it.hasNext())
   {
    Map.Entry<Student,String> me=it.next();
    Student stu=me.getKey();
    String addr=me.getValue();
    System.out.println(stu+":"+addr);
   }
 }
}

class StuAgeComparator implements Comparator<Student>
{
 public int compare(Student s1,Student s2)
 {
  int num=new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
  if(num==0)
  {
   return s1.getName().compareTo(s2.getName());
  }
  return num;
 }
}
8、TreeMap练习--字母出现的次数
 import java.util.*;

public class MapTest3
{
 public static void main(String[] args)
 {
  String s= charCount("akabfcdkabcdefa");
  System.out.println(s);
 }
 
 public static String charCount(String str)
 {
  char[] chs=str.toCharArray();
  
  TreeMap<Character,Integer> tm=new TreeMap<Character,Integer>();
  
  int count=0;
  for(int i=0;i<chs.length;i++)
  {
   Integer value=tm.get(chs[i]);
   
   if(value==null)
   {
    count=1;
   }
   else
   {    
    count=value+1;
   }
   tm.put(chs[i],count);
   
   /*if(value==null)
   {
    tm.put(chs[i],1);
   }
   else
   {
    value=value+1;
    tm.put(chs[i],value);
   }*/
  }
  //System.out.println(tm);
  
  StringBuilder sb=new StringBuilder();
  Set<Map.Entry<Character,Integer>> entrySet=tm.entrySet();
  Iterator<Map.Entry<Character,Integer>> it=entrySet.iterator();
  while(it.hasNext())
  {
   Map.Entry<Character,Integer> me=it.next();
   Character ch=me.getKey();
   Integer value=me.getValue();
   sb.append(ch+"("+value+")");
  }
  
  return sb.toString();
 }
}
9、Map扩展
 Map集合被使用是因为具备映射关系。
 

原创粉丝点击