您的位置:首页 > 职场人生

黑马程序员————集合框架中Map集合复习笔记

2014-09-20 09:38 106 查看


-----------android培训java培训、java学习型技术博客、期待与您交流!------------

Map集合

该集合存储键值对。一对一对往里存。而且要保证键的唯一性。

    1,添加。

        put(K key, V value)

        putAll(Map<? extends K,? extends V> m)

    2,删除。

        clear()

        remove(Object key)

    3,判断。

        containsValue(Object value)

        containsKey(Object key)

        isEmpty()

    4,获取。

        get(Object key)

        size()

        values()

        entrySet()

        keySet()

Map

    |--Hashtable:底层是哈希表数据结构,不可以存入null键null值。该集合是线程同步的。jdk1.0.效率低。

    |--HashMap:底层是哈希表数据结构,允许使用 null 值和 null 键,该集合是不同步的。将hashtable替代,jdk1.2.效率高。

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

和Set很像。

其实大家,Set底层就是使用了Map集合。

知识点一:可以通过get方法的返回值来判断一个键是否存在。通过返回null来判断。

//可以通过get方法的返回值来判断一个键是否存在。通过返回null来判断。
//需求:计算出字符串"afafklfoliojftiwtjsljgs;"各个字母字符出现的次数
//分析:将字符串转变为字符数组
//		将字符数组存入HashMap集合中,利用put()方法的返回值是否为null来判断映射中是否存在字母的键值
//		如果存在value++将原键值覆盖

import java.util.*;
class  MapDemo2
{
public static void main(String[] args)
{
String str="afafklfoliojftiwtjsljgs;";
char[] ch=str.toCharArray();
HashMap<Character,Integer> hs=new HashMap<Character,Integer>();
int value=1;
for(int i=0;i<ch.length;i++){
if (!(hs.put(ch[i],value)==null))
{
value++;

}
hs.put(ch[i],value);value=0;
}
System.out.println(hs);
}
}


Map集合中的学习重点:map 集合的两种取出方式:

1.Set<k> keySet:将map中所有的键存入到Set集合。因为Set具备迭代器。所以可以以迭代方式取出所有的键

,再根据get方法。获取每一键对应的值

map集合的取出原理:将map集合转成set集合。再通过迭代器取出

//map集合中的keyset()的方法练习
import java.util.*;
class  MapDemo3
{
public static void main(String[] args)
{
Map<String,String> map=new HashMap<String,String>();
String[] str={"asd","sdf","kkiu","fe"};
for(String str0:str){
map.put(str0,"haha");
}
//1.用keySet()方法取出映射中key的Set视图
Set<String> key=map.keySet();
//2.用迭代器取出映射中的每一个key
Iterator<String> it=key.iterator();
//System.out.print("["+it.next()+"]");
while (it.hasNext())
{
//System.out.print("["+it.next()+"]");
//System.out.print("["+map.get(it.next())+"]");
String str2=it.next();

System.out.print("["+str2+"="+map.get(str2)+"]");
}

}
}
2.set<Map.Entry<k,v>> entrySet:将map集合中的映射关系存入到set集合中,而这个关系的数据类型就是Map.Entry.Entry其实就是Map中的一个stat内部接口。为什么要定义在内部呢? 因为只有有了Map集合,有了键值对,才会有键值的映射关系。关系属于Map集合中的一个内部事物。而且该事物在直接访问Map集合中的元素。
/<span style="color:#FF0000;">/用entrySet方法取出Map集合中的
Set<Map.Entry<String,String>> set=map.entrySet();
sop(set);
//可以通过Map.Entry<k,v>的getKey()和getValue()方法取出集合中的键和值
</span>
一切具有映射关系的事物都可以用Map<K,v>
//map集合加强
/*
需求:描述一个公司,一个公司有市场部、技术部、售后
市场部有张三、张流、张琦
技术部有王五、王柳、王琦
售后有大傻、二狗、三毛
分析:用Map映射进行描述
*/
import java.util.*;
class  MapDemo4
{
public static void main(String[] args)
{
//描述市场部

HashMap<String,Integer> shichang=new HashMap<String,Integer>();
shichang.put("张三",20);
shichang.put("张流",21);
shichang.put("张琦",22);
HashMap<String,Integer> jishu=new HashMap<String,Integer>();
jishu.put("王五",15);
jishu.put("王柳",16);
jishu.put("王琦",18);
HashMap<String,Integer> souhou=new HashMap<String,Integer>();
souhou.put("大傻",19);
souhou.put("二狗",20);
souhou.put("三毛",22);
HashMap<HashMap<String,Integer>,String> com =new HashMap<HashMap<String,Integer>,String>();
com.put(shichang,"hongxing");
com.put(jishu,"hongxing");
com.put(souhou,"hongxing");
Set<Map.Entry<HashMap<String,Integer>,String>>  set=com.entrySet();
Iterator it=set.iterator();
while (it.hasNext())
{
System.out.println(it.next());
}
System.out.println(com);
Set<Map.Entry<String,Integer>> set1=jishu.entrySet();
Iterator<Map.Entry<String,Integer>> it1=set1.iterator();
while (it1.hasNext())
{
System.out.println(it1.next());
}
Set<Map.Entry<String,Integer>> set2=jishu.entrySet();
Fun(set2);
}

public static void Fun(Set<Map.Entry<String,Integer>> set)
{
Iterator it=set.iterator();
while (it.hasNext())
{
System.out.println(it.next());
}
return;
}

//System.out.println("Hello World!");

}


上一节忘记的泛型这一节补上
import java.util.*;
class  MapDemo5
{
public static void main(String[] args)
{
new Student();

System.out.println("Hello World!");
}
}
//泛型的应用
class Tool<Q>
{
public void getName(Q q)
{
System.out.println(q);
}
public <T> void  getAge(T t)
{
System.out.println(t);
}
public void  getAdress(Q q)
{
System.out.println(q);
}

}
//定义一个学生类
class Student
{
Student()
{
Tool<String> tool=new Tool<String>();
tool.getName("zhangsan");
tool.getAdress("henan");
tool.getAge(21);
}

}
错误分析:
System.out.println("["+it1.next().getKey().getName()+":"+it1.next().getKey().getAge()+":"+it1.next().getValue()+"]");

Exception in thread "main" java.util.NoSuchElementException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1431)
at java.util.HashMap$EntryIterator.next(HashMap.java:1463)
at java.util.HashMap$EntryIterator.next(HashMap.java:1461)
at MapDemo5.main(MapDemo5.java:41)
 
这种错误是最常见的一句话中连续几个next()它会一直向下取值知道抛出异常

练习:
/*
每一个学生都有对应的归属地。
学生Student,地址String。
学生属性:姓名,年龄。
注意:姓名和年龄相同的视为同一个学生。
保证学生的唯一性。

1,描述学生。

2,定义map容器。将学生作为键,地址作为值。存入。

3,获取map集合中的元素。

*/
import java.util.*;
class  MapDemo5
{
public static void main(String[] args){
HashMap<Student,String> hs=new HashMap<Student,String>();
hs.put(new Student("张三",21),"henan");
hs.put(new Student("张四",22),"henan");
hs.put(new Student("张三",21),"henan");
hs.put(new Student("张五",23),"天津");
//第一种用方式用keySet()方法来取
4000

Set<Student> set=hs.keySet();
Iterator<Student> it=set.iterator();
while (it.hasNext())
{
Student key=it.next();
System.out.println("["+key.getName()+":"+key.getAge()+":"+hs.get(key)+"]");

}
//第二种用entrySet()方法来取
Set<Map.Entry<Student,String>> set1=hs.entrySet();
Iterator<Map.Entry<Student,String>> it1=set1.iterator();
while (it1.hasNext())
{
Map.Entry<Student,String> map1=it1.next();
//System.out.println(it1.next().getKey().getName());
System.out.println("["+map1.getKey().getName()+":"+map1.getKey().getAge()+":"+map1.getValue()+"]");

}
}

}
class Student implements Comparable<Student>
{
private String name;
private Integer age;
Student(String name,Integer age)
{
this.name=name;
this.age=age;
}
public int compareTo(Student stu)
{
if (!(stu instanceof Student))
{
throw new RuntimeException("不是学生对象");
}
if (this.name.compareTo(name)==0&&this.age==age)
{
return 0;
}return -1;

}
public int hashCode(){
//System.out.println(name+"--------------"+(this.name.hashCode()+this.age*25));

return this.name.hashCode()+this.age*25;
}
public boolean equals(Object obj){
if (!(obj instanceof Student))
{
return false;
}
Student str=(Student)obj;
return this.name.equals(str.name)&&this.age==str.age;

}
public String getName(){
return name;
}
public Integer getAge(){
return age;
}

}

如果对学生对象的年龄进行升序排序话那要做下修改,因为数据是以键值对形式存在的,所以要使用可以排序的Map集合的TreeMap。

class StuNameComparator implements Comparator<Student>
{
public int compare(Student s1,Student s2)
{
int num = s1.getName().compareTo(s2.getName());
if(num==0)
return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));

return num;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: