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

黑马程序员-----Java基础-----Map

2015-08-21 09:57 501 查看
-----<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

 Map集合:该集合存储键值时,一对一对往里存,而且要保证键的唯一性

 添加:

 put(K key,V value)

 putall(map<? extend K,? extend V>m)

 

 删除:

 clear()

 remove(Object key)

 

 判断

 containsValue(Object value)

 containsKey(Object key)

 isEmpty()

 获取

 get(Object key)

 size()

 value()

 重点:

 entrySet()

 keySet()

 

Map

---Hashtable ;底层是哈希表数据结构,不可存入null键和null值,集合是同步的

---HashMap ;哈希表数据结构,允许使用null,不同步,效率高

---TreeMap ;底层是二叉树数据结构,线程不同步,可以用于给map集合中元素排序。

  

 和set很像,其实set底层就是使用了map集合。

 

import java.util.*;

public class Mapgaishu

{
public static void main(String[] args)
{
Map<String,String> map = new HashMap<String,String>();
//添加元素。如果出现相同时,后加的会覆盖原有值,并返回被覆盖值。
map.put("01", "lisi1");
map.put("02", "lisi2");
map.put("03", "lisi3");
System.out.println("con="+map.containsKey("02"));
System.out.println("renm="+map.remove("03"));
System.out.println("get="+map.get("02"));
System.out.println(map);

}

}

 map集合两种取出方式

 

 1,KeySet;将map中所有的键 存入到set集合中,因为set具备迭代器。

  set<k>keyset所以可以迭代方式取出所有的键,在根据get方法,获取每一个对于的值。

  Map集合的取出原理,将map集合转成set集合,在通过迭代器取出

 

  2,entrySet;将map集合中的映射关系存入到set集合中。而这个关系的数据类型就是:Map.Entry.

  set<map.entry<k,v>>

 

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

 

import java.util.*;

public class Mapgaishu

{
public static void main(String[] args)
{
Map<String,String> map =new HashMap<String,String>();
map.put("01", "lisi1");
map.put("02", "lisi2");
map.put("03", "lisi3");
//entryset  将map集合中的映射关系取出,存入set集合中
Set<Map.Entry<String, String>> entrySet = map.entrySet();

Iterator<Map.Entry<String, String>> it =entrySet.iterator();
while(it.hasNext())
{
Map.Entry<String, String> me =it.next();

String key = me.getKey();
String value = me.getValue();
System.out.println(key+"--"+value);
}

/*keyset
//先获取map集合的所有键的set集合,keyset();
Set<String> keySet = map.keySet();
//有了set集合,就可以取迭代器。
Iterator<String> it =keySet.iterator();
while(it.hasNext())
{
String key =it.next();
String value =map.get(key);
System.out.println("key="+key+"--value="+value);
}
*/
}

}

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

package mapp;

import java.util.*;

public class MapTest1 {
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("lisi2",22), "shanghai");
hm.put(new Student("lisi2",22), "TIANJIN");
hm.put(new Student("lisi3",23), "nanjing");
hm.put(new Student("lisi4",24), "shenyang");

//第一种keyset取出方式
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);
}
//第二种取出方式entryset
Set<Map.Entry<Student,String>> entrySet = hm.entrySet();

Iterator<Map.Entry<Student,String>> iter =entrySet.iterator();
while(iter.hasNext())
{
Map.Entry<Student,String> me = iter.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 int compareTo(Student s)
{
int num = new Integer(this.age).compareTo(new Integer(s.age));
if(num == 0)
return this.name.compareTo(s.name);
return num;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
//通过这句打印出名字和年龄,重点XX
public String toString()
{
return name+"--"+age;
}
//hashcode,equals,必须用到的。XX
public int hashCode()
{
return name.hashCode()+age*34;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Student))
throw new ClassCastException("leixingbupipei");
Student s =(Student)obj;
return this.name.equals(s.name)&& this.age == s.age;
}

}

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

package mapp;

//对学生对象的年龄进行升序排序。

//因为数据是以键值对的形式存在的,所以要使用可以排序的Map集合TreeMap。

import java.util.*;

//排序语句:

class Stucom 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;
}

}

public class MapTest2 

{
public static void main(String[] args) 
{
TreeMap<Student,String> hm = new TreeMap<Student,String>(new Stucom());

hm.put(new Student("lisi3",28), "beijing");
hm.put(new Student("lisi2",22), "shanghai");
hm.put(new Student("lisi1",25), "TIANJIN");
hm.put(new Student("lisi5",23), "nanjing");
hm.put(new Student("lisi4",24), "shenyang");

Set<Map.Entry<Student,String>> entrySet = hm.entrySet();

Iterator<Map.Entry<Student,String>> iter =entrySet.iterator();
while(iter.hasNext())
{
Map.Entry<Student,String> me = iter.next();
Student stu = me.getKey();
String addr = me.getValue();
System.out.println(stu+"---"+addr);
}
}

}

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

package mapp;

/*

 练习

 "sdgijspoksdffhdg"获取该字符串中的字母出现的次数。

 希望打印结果:a(1)b(3)c()2。。。

 

 通过结果发现,每一个字母都有对应的次数,

 说明字母和次数之间有映射关系。

 

 注意;当发现有映射关系时,可以选择map集合。

 因为map集合中存放的就是映射关系。

 

 什么时候使用map集合呢?

 当数据之间存在映射关系时,就要想到map集合。

 

 思路:

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

 

 2,定义一个map集合,因为打印结果字母有序,所以使用treemap集合。

 

 3,遍历字符数组,

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

 如果返回null,将该字母和1 放入map集合中。

 如果返回不是null,说明该字母在map集合中已经存在,并有对应次数。

 那么就获取该次数并且进行自增,然后将该字母和自增后次数存入集合map中,覆盖调用原来键锁对应的值。

 4,将map集合中的数据变成指定的字符串形式返回。

 

 

 

 

 * */

import java.util.*;

public class MapTest3 {
public static void main(String [] args)
{
String s =charCount("sddddghii=i-kjjj++spoksdffhdg");
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 x =0; x< chs.length; x++)
{

//字符串中含有符号去除。。
if(!(chs[x] >='a' && chs[x] <='z' || chs[x] >='A' && chs[x] <='Z'))
continue;
Integer value = tm.get(chs[x]);

if(value!=null)
count = value;
count++;
tm.put(chs[x], count);
count = 0;

/* if(value == null)
{
tm.put(chs[x], 1);
}
else
{
value = value +1;
tm.put(chs[x], 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();

}

}

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

 map知识扩展

 

 map集合被使用是因为具备映射关系

 

 "yureban"  "01"  "zhangdsan";

 "yureban"  "02"  "zhangdsan1";

 "jiuyeban"  "03"  "zhangdsan2";

 "jiuyeban"  "04"  "zhangdsan3";

 

 开发 一般封装成对象 Student("01"  "zhangdsan";)

 

import java.util.*;

class Student

{
private String id;
private String name;
Student(String id,String name)
{
this.id =id;
this.name =name;
}
public String toString()
{
return id+"--"+name;
}

}

public class MapTest4 

{
public static void demo()
{
HashMap<String,List<Student>> czbk = new HashMap<String,List<Student>>();
List<Student> yure = new ArrayList<Student>();
List<Student> jiuye = new ArrayList<Student>();

//打印信息
czbk.put("yureban", yure);
czbk.put("jiuyeban", jiuye);

yure.add(new Student("01","zhangsan1"));
yure.add(new Student("02","zhangsan2"));
jiuye.add(new Student("03","zhangsan3"));
jiuye.add(new Student("04","zhangsan4"));

Iterator<String> it =czbk.keySet().iterator();
while(it.hasNext())
{
String roomName = it.next();
List<Student> room = czbk.get(roomName);

System.out.println(roomName);
getInfo(room);//循环套循环,代替下面循环语句
}

}
public static void getInfo(List<Student> list)
{
Iterator<Student> it = list.iterator();
while(it.hasNext())
{
Student s =it.next();
System.out.println(s);
}

}

public static void main(String[] args)
{
//把学生信息封装成对象测试
demo();
//一个学校,两个教室,很多学生。
/* HashMap<String,HashMap<String, String>> czbk= new HashMap<String,HashMap<String, String>>();
HashMap<String, String> yure =new HashMap<String, String>();
HashMap<String, String> jiuye =new HashMap<String, String>();

czbk.put("yure", yure);
czbk.put("jiuye", jiuye);

yure.put("01","zhangsan");
yure.put("02","zhangsan1");

jiuye.put("03","zhangsan2");
jiuye.put("04","zhangsan3");

getStudentInfo(yure);//打印信息

//遍历czbk集合,获得所有教室
Iterator<String> it =czbk.keySet().iterator();
while(it.hasNext())
{
String roomName = it.next();
HashMap<String,String> room = czbk.get(roomName);

System.out.println(roomName);
getStudentInfo(room);//循环套循环,代替下面循环语句
}

}
public static void getStudentInfo(HashMap<String, String> roomMap)
{
Iterator<String> it =roomMap.keySet().iterator();
while(it.hasNext())
{
String id = it.next();
String name = roomMap.get(id);
System.out.println(id+"---"+name);
}
    }
}

}

-----<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: