黑马程序员技术交流社区
标题:
集合框架续
[打印本页]
作者:
周飞飞
时间:
2015-8-16 16:30
标题:
集合框架续
Map<K,V>集合:该集合是键值对的方式进行存储。
常用方法:
1、增
V put(k,v); 给相同键存不同值时第二次存的值会覆盖第一次存入的值,
第一次返回的是null 第二次返回的是第一次存入的值。
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 底层是hash表结构,不可以存入null键null值 ,线程同步
|--hashMap 底层是hash表结构,可以存入null键null值,线程非
|--TreeMap 底层是二叉树结构,线程非同步,可以对其中的元素排序。
(Set集合底层调用了Map集合)
Map集合的两种取出方式
1、keySet:将map中的所有键存入到Set集合,因为set具有迭代器。
取出所有的键再用map中的get方法
案例:
public class MapDemo {
public static void main(String args[]){
Map<Integer ,String> map = new HashMap<Integer ,String>();
map.put(1,"zhou");
map.put(2,"zhou04");
map.put(3,"zhou03");
map.put(4,"zhou02");
map.put(5,"zhou01");
//Metode_1(map);
Set<Integer> keyset = map.keySet();
Iterator<Integer> it =keyset.iterator();
while(it.hasNext()){
Integer key =it.next();
System.out.println(key+" "+map.get(key));
}
}
2,Set<Map.Entry<?,?>> entrySet();取出Map集合中的映射关系
存到Set集合中
Map.Entry(<?,?>)中的常用方法
boolean equals(Object o)
比较指定对象与此项的相等性。
K getKey()
返回与此项对应的键。
V getValue()
返回与此项对应的值。
int hashCode()
返回此映射项的哈希码值。
V setValue(V value)
用指定的值替换与此项对应的值(可选操作)。
案例:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class MapEntryDemo {
public static void main(String args[]){
HashMap<Integer ,String> hmap = new HashMap<Integer ,String>();
hmap.put(1,"zhou");
hmap.put(2,"zhou04");
hmap.put(3,"zhou03");
hmap.put(4,"zhou02");
hmap.put(5,"zhou01");
Set<Map.Entry<Integer, String>> hset =hmap.entrySet();
Iterator<Entry<Integer, String>> it =hset.iterator();
while(it.hasNext()){
Entry<Integer, String> en =it.next();
System.out.println(en.getKey()+" "+en.getValue());
}
}
}
Map.Entry<?,?> :Map是一个接口Entry是Map中的一个内部接口
定义内部接口的原因:当有Map是才会有关系
底层原理:
interface Map<K,V>{
public static interface Entry<K,V>{
public abstract V getValue();
public abstract K getKey();
}
}
案例:
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.TreeMap;
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 void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//覆盖hashCode方法
public int hashCode(){
return name.hashCode()+age*25;
}
//覆盖equals方法
public boolean equals(Object o){
if(!(o instanceof Student)){
throw new ClassCastException();
}
Student s = (Student)o;
return this.name.equals(s.name)&& this.age == s.age;
}
public String toString(){
return name +"::"+age;
}
@Override
//覆盖compareTo方法 让对象有自然顺序
public int compareTo(Student o) {
int num = new Integer(this.age).compareTo(new Integer(o.age));
if(num==0){
return this.name.compareTo(o.name);
}
return num;
}
}
//定义比较器
class StuCompare implements Comparator<Student>{
public int compare(Student o1, Student o2) {
int num = o1.getName().compareTo(o2.getName());
if(num==0){
return new Integer(o1.getAge()).compareTo(new Integer(o2.getAge()));
}
return num;
}
}
public class MapTest {
public static void main(String args[]){
method_TreeMapEntry();
method_TreeMapkeySet();
}
//TreeMap类案例:排序并取出:
public static void method_TreeMapkeySet(){
//第一种取出方式:keySet;
// TreeMap<Student,String> hm = new TreeMap<Student,String>();
TreeMap<Student,String> hm = new TreeMap<Student,String>(new StuCompare());//比较器比较
hm.put(new Student("zhou01",25), "gansu");
hm.put(new Student("zhou03",23), "shanghai");
hm.put(new Student("zhou04",22), "shengzhen");
hm.put(new Student("zhou02",24), "beijing");
hm.put(new Student("zhou05",21), "guangdong");
//Set<Student> set = hm.keySet();
// Iterator<Student> it =set.iterator()
Iterator<Student> it =hm.keySet().iterator();
while(it.hasNext()){
Student stu =it.next();
String address =hm.get(stu);//map中的get()方法根据键获取值
System.out.println("keySet: "+stu+"....."+address);
}
}
public static void method_TreeMapEntry(){
//第二种取出方式:Entry();
TreeMap<Student,String> hm = new TreeMap<Student,String>();
//TreeMap<Student,String> hm = new TreeMap<Student,String>(new StuCompare());//比较器比较
hm.put(new Student("zhou01",25), "gansu");
hm.put(new Student("zhou03",23), "shanghai");
hm.put(new Student("zhou02",24), "beijing");
hm.put(new Student("zhou05",21), "guangdong");
hm.put(new Student("zhou04",22), "shengzhen");
// Set<Entry<Student, String>> set =hm.entrySet();
// Iterator<Entry<Student, String>> it =set.iterator();
Iterator<Entry<Student, String>> it =hm.entrySet().iterator();
while(it.hasNext()){
Entry<Student, String> en =it.next();
Student stu =en.getKey();
String address= en.getValue();
System.out.println("Entry: "+stu+"....."+address);
}
}
//HashMap类案例:取出:
public static void method_HashMapEntry(){
//第二种取出方式:Entry();
HashMap<Student,String> hm = new HashMap<Student,String>();
hm.put(new Student("zhou01",25), "gansu");
hm.put(new Student("zhou02",24), "beijing");
hm.put(new Student("zhou03",23), "shanghai");
hm.put(new Student("zhou04",22), "shengzhen");
hm.put(new Student("zhou05",21), "guangdong");
// Set<Entry<Student, String>> set =hm.entrySet();
// Iterator<Entry<Student, String>> it =set.iterator();
Iterator<Entry<Student, String>> it =hm.entrySet().iterator();
while(it.hasNext()){
Entry<Student, String> en =it.next();
Student stu =en.getKey();
String address= en.getValue();
System.out.println("Entry: "+stu+"....."+address);
}
}
public static void method_HashMapkeySet(){
//第一种取出方式:keySet;
HashMap<Student,String> hm = new HashMap<Student,String>();
hm.put(new Student("zhou01",25), "gansu");
hm.put(new Student("zhou02",24), "beijing");
hm.put(new Student("zhou03",23), "shanghai");
hm.put(new Student("zhou04",22), "shengzhen");
hm.put(new Student("zhou05",21), "guangdong");
//Set<Student> set = hm.keySet();
// Iterator<Student> it =set.iterator()
Iterator<Student> it =hm.keySet().iterator();
while(it.hasNext()){
Student stu =it.next();
String address =hm.get(stu);//map中的get()方法根据键获取值
System.out.println("keySet: "+stu+"....."+address);
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2