一、Map集合常见子类
HashTable:内部结构是哈希表,同步,此实现提供所有可选的映射操作,不允许使用 null 值和 null 键
(HashTable下有子类Properties,使用频率非常高,用来存储键值对型的配置文件信息和IO技术相结合)
HashMap:内部结构是哈希表,不同步,此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。
三、TreeMap演示
在什么时候用相应懂得集合,需要事先考虑好
TreeMap:内部结构是二叉树,不同步,可以对Map集合中的键进行排序。
二、HashMap演示
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
class Man
{
private String name;
private int 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;
}
public Man(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Man() {
super();
// TODO Auto-generated constructor stub
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null ||getClass() != obj.getClass() )
return false;
Man other = (Man) obj;
if (age != other.age)
return false;
/* if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;*/
if(name==null ? other.name !=null : !name.equals(other.name))
return false;
return true;
}
}
public class JiaoYan
{
public static void main(String[] args) {
HashMapDemo();
}
public static void HashMapDemo(){
//人和籍贯 通过键值对存储在HashMap中
HashMap<Man, String> hm = new HashMap<Man,String>();
hm.put(new Man("周",1),"上海");
hm.put(new Man("周",1),"东北");//要保证哈希表,所以Man中的HashCode和equals方法要复写
hm.put(new Man("王",4),"北京");
hm.put(new Man("孙",2),"广州");
hm.put(new Man("赵",6),"山东");
Iterator<Man> it = hm.keySet().iterator();
while(it.hasNext()){
Man man = it.next();
String bir = hm.get(man);
System.out.println("man - bir :"+man.getName()+","+man.getAge()+" - "+bir);
}
}
}
三、TreeMap演示
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
class Man extends Object
{
private String name;
private int 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;
}
public Man(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Man() {
super();
// TODO Auto-generated constructor stub
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Man other = (Man) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
class Compararule implements Comparator<Object>
{
public int compare(Object arg0, Object arg1) {
if(!(arg0 instanceof Man))
throw new ClassCastException();
Man man = (Man)arg0;
Man man2 = (Man)arg1;
int te = man.getAge() - man2.getAge();
return te==0?man.getName().compareTo(man2.getName()):te;
}
}
public class Main
{
public static void main(String[] args) {
TreeMapDemo();
}
public static void TreeMapDemo(){
TreeMap<Man, String> tm = new TreeMap<Man,String>(new Compararule());
tm.put(new Man("zhou",1),"上海");
tm.put(new Man("zhou",1),"东北");//要保证哈希表,所以Man中的HashCode和equals方法要复写
tm.put(new Man("wang",4),"北京");
tm.put(new Man("sun",2),"广州");
tm.put(new Man("zhao",6),"山东");
Iterator<Map.Entry<Man,String>> it = tm.entrySet().iterator();
while(it.hasNext()){
Map.Entry<Man, String> mEntry = it.next();
Man man = mEntry.getKey();
String bir = mEntry.getValue();
System.out.println("man - bir :"+man.getName()+","+man.getAge()+" - "+bir);
}
}
}
一.看元素是否需要唯一
1.需要:Set集合
看是否需要制定顺序
需要:TreeSet
不需要:HashSet
想要保存存储顺序(有序):LinkedHashSet
2.不:List
看是否需要经常性的增删元素
需要:LinkedList
不需要:ArrayList
根据容器的名字就可以确定容器的结构和所属体系
如:List下:ArrayList、LinkedList
Set下:HashSet、TreeSet
集合框架的三大集合:List、Set、Map,到此就结束了
看到array,数组,有角标,查询速度快
看到link,链表,增删速度快,add、get、remove frist/last 方法
看到hash,哈希表,元素唯一性,覆盖HashCode方法和equals方法
看到tree,二叉树,排序,两大接口Comparator,Comparable
而Map自身没有迭代器,但是可以通过keySet,entry返回Set映射,再通过Set的迭代器,访问键值对
明天就是泛型的学习。要重点掌握,加油!!!