1 Map概述
Map代表具有映射关系的集合,
该接口中的方法如下:
需要强调的是:
size():Map集合中的映射条数如果大于Integer.MAX_VALUE,将返回Integer.MAX_VALUE,而不是实际的条数。
put():将映射插入到集合中,如果出现key已存在的情况,将会覆盖。
Set keySet():返回该Map集合中所有Key组成的Set集合。
Set entrySet():返回Map集合中包含映射关系组成的Set集合。
equals(Object o):比较两个Map集合,具有相同的映射,即两个集合的entrySet返回的Set视图相同。
输出Map集合中的映射
//将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);
}
遍历输出Map中的元素:
/**
* 遍历输出Map集合中的元素
*/
public static void printMap_01(Map<Integer, String> map) {
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println("key=" + key + " value=" + value);
}
}
/**
* 遍历输出Map集合中的元素
* 使用lambda遍历输出元素
*/
public static void printMap_02(Map<Integer, String> map) {
//java8才能使用的lambda表达式 没测试过
//books.forEach(obj-> System.out.println("迭代集合元素:"+obj));
}
/**
* for输出map中的元素
* @param map
*/
public static void printMap_03(Map<Integer, String> map){
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println("key:"+entry.getKey()+" "+entry.getValue());
}
}
2 HashMap
HashMap数据存储结构为Entry数组,而Entry类为具有四个变量,一个为key,存储键,一个为value,存储对应的值,一个为next,指向下一个Entry的地址空间,还有hash值。其中key和value均可为空。
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
int hash;
/**
* Creates new entry.
*/
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
2.1默认空间大小:16,最大空间大小:2^30
2.2创建时没声明大小时,默认大小为16.
2.3创建对象时,可将一个对象作为参数来创建一个具有一模一样的映射HashMap。
源码如下:
/**
* Constructs a new <tt>HashMap</tt> with the same mappings as the
* specified <tt>Map</tt>. The <tt>HashMap</tt> is created with
* default load factor (0.75) and an initial capacity sufficient to
* hold the mappings in the specified <tt>Map</tt>.
*
* @param m the map whose mappings are to be placed in this map
* @throws NullPointerException if the specified map is null
*/
public HashMap(Map<? extends K, ? extends V> m) {
this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
inflateTable(threshold);
putAllForCreate(m);
}
2.4 根据Key值获取Value
V get(Object key);
2.5 判断是否包含指定的Kay
boolean containsKey(Object key);
2.6 添加一个键值对,当key为null时,将会把值添加到第一个空key的位置。
V put(K key, V value)
2.7 是否包含指定Value值
boolean containsValue(Object value);
3 LinkedHashMap
3.1 LinkedHashMap继承自HashMap类,同时实现Map接口。该构造函数均调用的是父类HashMap的构造函数,迭代顺序与插入的顺序保持一致,迭代的速度较HashMap快。
3.2 变量:新添加了一个指向头结点的变量header,
3.3 键值对结构:Entry
private static class Entry<K,V> extends HashMap.Entry<K,V> {
// These fields comprise the doubly linked list used for iteration.
Entry<K,V> before, after;
Entry(int hash, K key, V value, HashMap.Entry<K,V> next) {
super(hash, key, value, next);
}
4 TreeMap
4.1 TreeMap继承自AbstractMap抽象函数。实现Navigablemap接口,底层为红黑树,
以自然排序为例:
TreeMap的排序,TreeMap可以对集合中的键进行排序。如何实现键的排序?
方式一:元素自身具备比较性
和TreeSet一样原理,需要让存储在键位置的对象实现Comparable接口,重写compareTo方法,也就是让元素自身具备比较性,这种方式叫做元素的自然排序也叫做默认排序。
方式二:容器具备比较性
当元素自身不具备比较性,或者自身具备的比较性不是所需要的。那么此时可以让容器自身具备。需要定义一个类实现接口Comparator,重写compare方法,并将该接口的子类实例对象作为参数传递给TreeMap集合的构造方法。
注意:当Comparable比较方式和Comparator比较方式同时存在时,以Comparator的比较方式为主;
注意:在重写compareTo或者compare方法时,必须要明确比较的主要条件相等时要比较次要条件。(假设姓名和年龄一直的人为相同的人,如果想要对人按照年龄的大小来排序,如果年龄相同的人,需要如何处理?不能直接return 0,以为可能姓名不同(年龄相同姓名不同的人是不同的人)。此时就需要进行次要条件判断(需要判断姓名),只有姓名和年龄同时相等的才可以返回0.)
通过return 0来判断唯一性。
例:
final Entry<K,V> getFirstEntry():获取该Map中key最小的映射关系。
Map.Entry<K,V> lastEntry():获取该Map中的最大的映射关系。
void writeObject(java.io.ObjectOutputStream s):将映射关系写入指定的输出流
class MyComparator implements Comparator<Person> {
@Override
public int compare(Person p1, Person p2) {
if (p1.getAge() > p2.getAge()) {
return -1;
} else if (p1.getAge() < p2.getAge()) {
return 1;
}
return p1.getName().compareTo(p2.getName());
}
}
class Person implements Comparable<Person> {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Person) {
Person p = (Person) obj;
return this.name.equals(p.name) && this.age == p.age;
} else {
return false;
}
}
/**
* 实现compareTo方法
*/
@Override
public int compareTo(Person p) {
if (this.age > p.age) {
return 1;
} else if (this.age < p.age) {
return -1;
}
return this.name.compareTo(p.name);
}
}
参考博客:
http://blog.csdn.net/qq_33642117/article/details/52049764
class R implements Comparable{
int count;
public R(int count) {
this.count = count;
}
/**
* 重写equal方法,保证compareTo方法保持一致的返回结果。
* 两个key通过equal进行比较时返回true,他们通过compareTo方法比较
* 返回的是0.
*/
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if(this==obj){
return true;
}
if(obj!=null&&obj.getClass()==R.class){
R r=(R)obj;
return r.count==this.count;
}
return false;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
R r=(R)o;
return count>r.count?1:count<r.count?-1:0;
}
}
public static void main(String[] args) {
TreeMap tm=new TreeMap();
tm.put(new R(0), "3");
tm.put(new R(-1), "-5");
tm.put(new R(1), "9");
System.out.println(tm.firstEntry().getValue());//输出-5
}
5 HashTable
5.1 HashTable实现Map接口,key和value不可以为null,
5.2 HashTable中的部分方法如put()为线程安全。
5.3 HashTable中映射结构类型与HashMap类似。
6 Properties
6.1 Properties是HashMap的子类,可以从输入输出流中读取键值的映射到一个类似HashMap集合中。
6.2 保存键值对到输出流中:
public void store(OutputStream out, String comments)
throws IOException
{
store0(new BufferedWriter(new OutputStreamWriter(out, "8859_1")),
comments,
true);
}
Properties properties=new Properties();
try {
properties.load(new FileInputStream("D:\\demo.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(properties.getProperty("jdbc"));
未完待续。。。