黑马程序员---Map集合笔记
------- android培训、java培训、期待与您交流! ----------
Map
/*
Map:该集合存储键值对,一对一的往里存
增:
put(Kkey,Vvalue)将指定的值与此映射中的指定键相关联(可选操作)。
voidputAll(Map<?extendsK,?extendsV>t)从指定映射中将所有映射关系复制到此映射中(可选操作)。
删:
clear()从此映射中移除所有映射关系(可选操作)。
remove(Objectkey)如果存在此键的映射关系,则将其从映射中移除(可选操作)。
判断:
containsValue(Objectvalue)如果此映射为指定值映射一个或多个键,则返回true。
containsKey(Objectkey)如果此映射包含指定键的映射关系,则返回true。
isEmpty()如果此映射未包含键-值映射关系,则返回true
获取:get(Objectkey)
values()返回此映射中包含的值的collection视图。
size()返回此映射中的键-值映射关系数。
entrySet()返回此映射中包含的映射关系的set视图。
keySet()返回此映射中包含的键的set视图。
Map
|--HashTable:底层是哈希表数据结构,不可以存入null键和null值,该集合是线程同步的:效率不高
|--hashMap:底层是哈希表数据结构,运行使用null值和null键,线程不同步,效率高
|--TreeMap:底层是二叉树数据结构线程不同步。可以用map中的键进行排序
*/
importjava.util.*;
classMapdemo
{
publicstaticvoidmain(String[]args)
{
HashMap<String,String>hm=newHashMap<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("=====");
Collectioncll=hm.values();//返回值的所有集合
sop(hm.get("02"));//获取键对应的值
sop(cll);
//sop(hm.containsKey("02"));
//sop(hm.values());
}
publicstaticvoidsop(Objectobj)
{
System.out.println(obj);
}
}
/*
Keyset:将map所有的键存入到set集合中,因为set具有迭代器可以取出所有的键
所有的迭代方式取出所有键根据get方法获取每个键对应的值
map集合取出原理,键map集合转成set集合,在通过迭代器取出每个键对应的值
entryset:是将map集合中映射关系存入到set集合中
而这个关系的数据类型是map.Entry
Map,Entry其实entry也是一个接口,他是map接口中的一个内部接口
*/
importjava.util.*;
classKeysetdemo
{
publicstaticvoidsop(Objectobj)
{
System.out.println(obj);
}
publicstaticvoidmain(String[]args)
{
Map<String,String>hm=newHashMap<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();
Stringkey=mapkey.getKey();
Stringval=mapkey.getValue();
sop(key+"--"+val);
}
}
}
Map中两种取证方式
importjava.util.*;
classPersonimplementsComparable<Person>
{
privateStringname;
privateintage;
Person(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicStringgetName()
{
returnname;
}
publicintgetAge()
{
returnage;
}
publicinthashCode()
{
returnname.hashCode()+age*23;
}
publicbooleanequals(Objectobj)
{
if(!(objinstanceofPerson))
thrownewClassCastException("类型错误");
Personp=(Person)obj;
returnthis.name.equals(p.name)&&this.age==p.age;
}
publicintcompareTo(Personp)
{
intnum=newInteger(this.age).compareTo(newInteger(p.age));
if(num==0)
returnthis.name.compareTo(p.name);
returnnum;
}
publicStringtoString()
{
returnname+".."+age;
}
}
classMaptest
{
publicstaticvoidmain(String[]args)
{
HashMap<Person,String>hm=newHashMap<Person,String>();
hm.put(newPerson("lsi1",35),"beijin");
hm.put(newPerson("lsi2",34),"sichuan");
hm.put(newPerson("lsi3",33),"gauangz");
hm.put(newPerson("lsi4",32),"tianjing");
hm.put(newPerson("lsi5",38),"jiling");
hm.put(newPerson("lsi6",64),"cahgcun");
//第一种取值方式Keyset
Set<Person>set=hm.keySet();//从hm中取出所有的键
for(Iterator<Person>it=set.iterator();it.hasNext();)
{
Personp=it.next();//从it中获取键
Stringval=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();
Personp=map.getKey();
Stringaddr=map.getValue();
System.out.println(p+"---"+addr);
}
}
}
TreeMap的练习
importjava.util.*;
classTreemap
{
publicstaticvoidsop(Objectobj)
{
System.out.println(obj);
}
publicstaticvoidmain(String[]args)
{
TreeMap<Person,String>hm=newTreeMap<Person,String>(newComparator<Person>(){
publicintcompare(Personp1,Personp2)
{
intnum=p1.getName().compareTo(p2.getName());
if(num==0)
returnnewInteger(p1.getAge()).compareTo(newInteger(p2.getAge()));
returnnum;
}
});
hm.put(newPerson("lsi1",35),"beijin");
hm.put(newPerson("dlsi2",34),"sichuan");
hm.put(newPerson("lsi3",33),"gauangz");
hm.put(newPerson("lsi3",33),"3gauangz");
hm.put(newPerson("alsi4",32),"tianjing");
hm.put(newPerson("lsi5",38),"jiling");
hm.put(newPerson("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();
Personp=map.getKey();
Stringaddr=map.getValue();
sop(p+"---"+addr);
}
}
}
Treemap练习er
/*
“dfdsgdfgdfghdfh”获取该字符出现的次数
打印d(1)g(3)
1,将字符串转成字符数组因为要对每一个字母进行操作
2,定义一个emap集合,
3遍历字符数组
将每一个字母作为键查map集合
如果返回null,就将该字母存入到map中
如果不是null,说明该字符有了,那么就获取次数并自增,然后将该字母和自增后的次数存入到map集合中
4将map集合中的数据变成指定字符串形式
*/
importjava.util.*;
classTreetest
{
publicstaticvoidsop(Objectobj)
{
System.out.println(obj);
}
publicstaticStringchars(Stringstr)
{
intcount=0;
char[]ch=str.toCharArray();
TreeMap<Character,Integer>tm=newTreeMap<Character,Integer>();
for(intx=0;x<ch.length;x++)
{
if(!(ch[x]>='a'&&ch[x]<='z'||ch[x]>='A'&&ch[x]<='Z'))
continue;
Integerval=tm.get(ch[x]);
if(val!=null)
count=val;
count++;
tm.put(ch[x],count);
count=0;
}
StringBuildersb=newStringBuilder();
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();
Characterkey=me.getKey();
Integerval=me.getValue();
sb.append(key+"("+val+")");
}
returnsb.toString();
}
publicstaticvoidmain(String[]args)
{
sop(chars("ddjfj+ertytry+677+,,.,5,5@djekjj"));
}
优质内容筛选与推荐>>