黑马程序员---Map集合笔记

Map

/*

Map:该集合存储键值对,一对一的往里存

增:

put(K key, V value)   将指定的值与此映射中的指定键相关联(可选操作)。

 void putAll(Map<? extends K,? extends V> t) 从指定映射中将所有映射关系复制到此映射中(可选操作)。 

删:

clear() 从此映射中移除所有映射关系(可选操作)。

remove(Object key)   如果存在此键的映射关系,则将其从映射中移除(可选操作)。

判断:

containsValue(Object value) 如果此映射为指定值映射一个或多个键,则返回 true

containsKey(Object key) 如果此映射包含指定键的映射关系,则返回 true

isEmpty()   如果此映射未包含键-值映射关系,则返回 true

获取:get(Object key)

values()    返回此映射中包含的值的 collection 视图。

size()   返回此映射中的键-值映射关系数。

entrySet()     返回此映射中包含的映射关系的 set 视图。

  keySet()     返回此映射中包含的键的 set 视图。

 

 

Map       

  |--HashTable:底层是哈希表数据结构,不可以存入null键和null值,该集合是线程同步的:效率不高

  |--hashMap:底层是哈希表数据结构,运行使用null值和null键,线程不同步,效率高

  |--TreeMap:底层是二叉树数据结构 线程不同步。可以用map中的键进行排序

 

 

 

*/

import java.util.*;

class Mapdemo 

{

public static void main(String[] args) 

{

HashMap<String,String> hm=new HashMap<String,String>();

//添加元素有相同的键,那么后添加的值会覆盖原有的对应值,覆盖后并put返回原有的覆盖的值

hm.put("01","lishi1");

    hm.put("02","lishi2");

hm.put("03","lishi3");

hm.put("04","lishi4");

hm.put("05","lishi5");

hm.put("07",null);

hm.put(null,null);

sop(hm);

sop("=====");

Collection cll=hm.values();//返回值的所有集合

sop(hm.get("02"));//获取键对应的值

sop(cll);

//sop(hm.containsKey("02"));

//sop(hm.values());

}

public static void sop(Object obj)

{

System.out.println(obj);

 

}

}

 

/*

Keyset:map所有的 键存入到set集合中,因为set具有迭代器可以取出所有的键

   所有的迭代方式取出所有键根据get方法获取每个键对应的值

   map 集合取出原理,键map集合转成set集合,在通过迭代器取出每个键对应的值

 

 

entryset:是将map集合中映射关系存入到set集合中

           而这个关系的数据类型是map.Entry

 

 

   Map,Entry 其实entry也是一个接口,他是map接口中的一个内部接口

 

*/

import java.util.*;

 

 

class Keysetdemo 

{

public static void sop(Object obj)

{

System.out.println(obj);

}

public static void main(String[] args) 

{

Map<String,String> hm=new HashMap<String,String>();

hm.put("01","lishi1");

    hm.put("02","lishi2");

hm.put("03","lishi3");

hm.put("04","lishi4");

hm.put("05","lishi5");

Set<Map.Entry<String,String>> map=hm.entrySet();//通过map.entry获取hm里面的映射关系

 for (Iterator<Map.Entry<String,String>> it=map.iterator();it.hasNext() ; )

 {

 HashMap.Entry<String,String> mapkey=it.next();

 String key=mapkey.getKey();

 String val=mapkey.getValue();

 sop(key+"--"+val);

 

 }

 

}

}

 

Map中两种取证方式

import java.util.*;

class Person implements Comparable<Person>

{

private String name;

private int age;

Person(String name,int age)

{

this.name=name;

this.age=age;

}

public String getName()

{

return name;

}

public int getAge()

{

return age;

}

public int hashCode()

{

return name.hashCode()+age*23;

}

public boolean equals(Object obj)

{

if(!(obj instanceof Person))

throw new ClassCastException("类型错误");

Person p=(Person)obj;

return this.name.equals(p.name)&&this.age==p.age;

}

public int compareTo(Person p)

{

int num=new Integer(this.age).compareTo(new Integer(p.age));

if(num==0)

return this.name.compareTo(p.name);

return num; 

}

public String toString()

{

return name+".."+age;

}

 

}

 

class Maptest 

{

public static void main(String[] args) 

{

HashMap<Person,String> hm=new HashMap<Person,String>();

hm.put(new Person("lsi1",35),"beijin");

hm.put(new Person("lsi2",34),"sichuan");

hm.put(new Person("lsi3",33),"gauangz");

hm.put(new Person("lsi4",32),"tianjing");

hm.put(new Person("lsi5",38),"jiling");

hm.put(new Person("lsi6",64),"cahgcun");

//第一种取值方式Keyset

Set<Person> set=hm.keySet();//hm中取出所有的键

 

for (Iterator<Person> it=set.iterator();it.hasNext() ; )

{

Person p=it.next();//it中获取键

String val=hm.get(p);

System.out.println(p+"----"+val);

 

}

 

System.out.println("========");

//第二中取值方式

Set<Map.Entry<Person,String>> setmap=hm.entrySet();//获取映射中的映射关系

for (Iterator<Map.Entry<Person,String>> it=setmap.iterator();it.hasNext() ; )

{

Map.Entry<Person,String> map=it.next();

Person p=map.getKey();

String addr=map.getValue();

System.out.println(p+"---"+addr);

}

 

 

}

}

   TreeMap的练习

import java.util.*;

class Treemap 

{

public static void sop(Object obj)

{

System.out.println(obj);

}

public static void main(String[] args) 

{

TreeMap<Person,String> hm=new TreeMap<Person,String>(new Comparator<Person>(){

public int compare(Person p1,Person p2)

{

int num=p1.getName().compareTo(p2.getName());

if(num==0)

return new Integer(p1.getAge()).compareTo(new Integer(p2.getAge()));

return num;

}

});

hm.put(new Person("lsi1",35),"beijin");

hm.put(new Person("dlsi2",34),"sichuan");

hm.put(new Person("lsi3",33),"gauangz");

hm.put(new Person("lsi3",33),"3gauangz");

hm.put(new Person("alsi4",32),"tianjing");

hm.put(new Person("lsi5",38),"jiling");

hm.put(new Person("glsi6",64),"cahgcun");

Set<Map.Entry<Person,String>> setmap=hm.entrySet();//获取映射中的映射关系

for (Iterator<Map.Entry<Person,String>> it=setmap.iterator();it.hasNext() ; )

{

Map.Entry<Person,String> map=it.next();

Person p=map.getKey();

String addr=map.getValue();

sop(p+"---"+addr);

}

 

}

 

}

         Treemap练习er

/*

dfdsgdfgdfghdfh”获取该字符出现的次数

打印 d1g3

1,将字符串转成字符数组因为要对每一个字母进行操作

2,定义一个emap集合,

3遍历字符数组

  将每一个字母作为键查map集合

  如果返回null,就将该字母存入到map

  如果不是null ,说明该字符有了,那么就获取次数并自增,然后将该字母和自增后的次数存入到map集合中

  4map集合中的 数据变成指定字符串形式

*/

import java.util.*;

 

class Treetest 

{

public static void sop(Object obj) 

{

System.out.println(obj);

}

public static String chars(String str)

{

int count=0;

          char[] ch=str.toCharArray();

  TreeMap<Character,Integer> tm=new TreeMap<Character,Integer>();

  for (int x=0;x<ch.length;x++ )

  {

  if(!(ch[x]>='a'&&ch[x]<='z'||ch[x]>='A'&&ch[x]<='Z'))

  continue;

 

  Integer val=tm.get(ch[x]); 

 if(val!=null)

 count=val;

 count++;

 tm.put(ch[x],count);

 count=0;

 

 

  }

  StringBuilder sb=new StringBuilder();

  Set<Map.Entry<Character,Integer>> setmap=tm.entrySet();

  for (Iterator<Map.Entry<Character,Integer>> it=setmap.iterator();it.hasNext() ; )

  {

  Map.Entry<Character,Integer> me=it.next();

  Character key=me.getKey();

  Integer val=me.getValue();

  sb.append(key+"("+val+")");

  }

  return sb.toString();

}

public static void main(String[] args) 

{

sop(chars("ddjfj+ertytry+677+,,.,5,5@djekjj"));

}

 

 

 

posted @ 2012-11-30 21:19  杰的博客  阅读(239)  评论(0编辑  收藏  举报