Map
1.1、HashMap
键值对存储,允许键值对是null值,而且线程不同步,从JDK1.7开始添加了红黑树(左右平衡/左右对等的二叉树),对增删查改性能有所提高,特别是检索功能。
package com.wlx.day14;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class HashMapDemo
{
public static void main(String[] args)
{
//创建Map对象
Map<Integer,String> map = new HashMap<>();
map.put(0,"tom");
map.put(1,"jim");
map.put(2,"lilei");
map.put(3,"hanmm");
map.put(4,"lucy");
map.put(null,null);
System.out.println(map);
//获取map集合的长度
System.out.println(map.size());
//移除map集合中指定的元素
map.remove(4);
System.out.println(map);
//遍历map集合
for(Integer ite : map.keySet())
{
System.out.println(ite+"--->"+map.get(ite));
}
System.out.println("------------");
//使用迭代器迭代map集合
//将Map集合转换成Set集合
Set<Integer> set = map.keySet();
Iterator<Integer> it = set.iterator();
while(it.hasNext())
{
Integer key = it.next();
System.out.println(key+"--->"+map.get(key));
}
}
}
1.2、Hashtable与HashMap的区别
Hashtable键值对存储,不允许键值对为null值,如果有一个为null,则出现NullPointerException空指针异常,线程同步;HashMap键值对存储,允许键值对是null值,而且线程不同步。
1.3、Properties
Hashtable子类Properties。
package com.wlx.day14;
import java.io.FileInputStream;
import java.util.Properties;
public class PropertiesDemo
{
public static void main(String[] args)throws Exception
{
//创建Map集合对象
Properties ppt = new Properties();
/*ppt.setProperty("hello","world");
ppt.setProperty("qq","tx");
ppt.setProperty("dd","ali");
System.out.println(ppt);
//通过key获取对应的value
System.out.println(ppt.getProperty("dd"));*/
//加载指定的文件
ppt.load(new FileInputStream("./javasecode/src/com/wlx/day14/ppt.properties"));
//通过指定文件中的key获取对应的value值
String cname = ppt.getProperty("classname");
System.out.println(cname);
/*String hello = ppt.getProperty("hello");
System.out.println(hello);
String demo = ppt.getProperty("demo");
System.out.println(demo);*/
/*//通过类的完整包路径,得到一个类对象
Class cl = Class.forName(cname);
//通过类对象,创建一个Emp实例
Emp emp = (Emp) cl.newInstance();
emp.setEname("tom");
emp.setSal(9876.1);
System.out.println(emp);*/
}
}
1.4、TreeMap
默认情况下对Map集合进行自然排序(从小到大)。
package com.wlx.day14;
import java.util.TreeMap;
public class TreeMapDemo
{
public static void main(String[] args)
{
//创建TreeMap对象
TreeMap<Integer,String> treeMap = new TreeMap<>();
treeMap.put(0,"tom");
treeMap.put(10,"jim");
treeMap.put(2,"lilei");
treeMap.put(13,"tim");
treeMap.put(3,"tmi");
treeMap.put(12,"mi");
//对treeMap集合进行自然排序
System.out.println(treeMap);
}
}
1.5、红黑树
定义:红黑树也是一种自平衡的二叉搜索树,插入和删除时旋转次数更少。
特性:
1、所有节点都有两种颜色:红与黑
2、所哟 null 值视为黑色
3、红色节点不能相邻
4、根节点是黑色
5、从根节点到任意一个叶子节点,路径中的黑色节点数是一样的。
Collections
集合工具类。
package com.wlx.day14;
import java.util.Collections;
import java.util.LinkedList;
public class CollectionsDemo
{
public static void main(String[] args)
{
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("jim");
linkedList.add("tom");
linkedList.add("lilei");
linkedList.add("lucy");
//对List集合进行排序,自然排序
Collections.sort(linkedList);
System.out.println(linkedList);
//对已经进行自然排序List进行反转,即倒序排序
Collections.reverse(linkedList);
System.out.println(linkedList);
//对List集合元素进行打乱
Collections.shuffle(linkedList);
System.out.println(linkedList);
}
}
一些概念
桶:哈希表中的每一个位置称为桶。
容量:哈希表中桶的数量。
初始容量:Hash表对象的最初的桶的数量。
大小:元素的个数。
加载因子:加载因子 = 大小/容量
加载因子为0时,表示空的哈希表,加载因子为0.75时,表示半满的哈希表;依此类推,轻负载的哈希表具有冲突少,适合插入和查找的操作优点,HashSet和HashMap的构造方法允许指定加载因子,当哈希表的当前加载达到用户设定的加载因子时,HashSet和HashMap会自动成倍的增加容量,一般情况下Java中以1.5倍数量进行增加,也有2倍4倍8倍的方式进行增加容量。