P539Hashtable使用P541PropertiesP542总结开发中选择集合类P544TreeMap源码P546Collections 工具类2
P539Hashtable使用
public class Hashtable1 {
public static void main(String[] args) {
/*
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, java.io.Serializable
*/
Hashtable table = new Hashtable();
table.put("john",88);
table.put("lucy",100);
table.put("lic",100);
table.put("lic",88);
System.out.println(table);//输出{lic=88, lucy=100,john=88}
table.put("hello1",1);
table.put("hello2",1);
table.put("hello3",1);
table.put("hello4",1);
table.put("hello5",1);
table.put("hello6",1);
//底层
//1.底层有数组,Hashtable$Entry[]初始化为11、
//2.临界值threshold8=11*0.75,扩容到23,现在临界值为17
//3.扩容,按照自己的机制,先装箱
//4.执行方法 addEntry(hash, key, value, index);添加kv封装到Entry
//5. 当addEntry里面的if (count >= threshold)满足时,就进行扩容
// 按照int newCapacity = (oldCapacity << 1) + 1;进行扩容
/*
public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<K,V>)tab[index];
for(; entry != null ; entry = entry.next) {
if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value;
entry.value = value;
return old;
}
}
addEntry(hash, key, value, index);
return null;
}
*/
}
}
P541Properties
public class Prpperties1 {
public static void main(String[] args) {
//老韩解读
// /1. Properties 继承Hashtable
// 2. 可以通过k-v 存放数据,当然key 和value 不能为null
// 增加
Properties properties = new Properties();
//properties.put(null, "abc");//抛出空指针异常
// properties.put("abc", null); //抛出空指针异常
properties.put("john", 100);//k-v
properties.put("lic", 100);
properties.put("lic", 88);//如果有相同的key , value 被替换
System.out.println("properties=" + properties);
//通过k 获取对应值
System.out.println(properties.get("lic"));//输出88
// 删除
properties.remove("lic");
System.out.println("properties=" + properties);//输出properties={john=100}
//修改
properties.put("john", "约翰");
System.out.println("properties=" + properties);//输出properties={john=约翰}
}
}
P542总结开发中选择集合类
public class TreeSet1 {
public static void main(String[] args) {
//1. 当我们使用无参构造器,创建TreeSet 时,仍然是无序的
TreeSet treeSet = new TreeSet();
//1.添加数据.
treeSet.add("jack");
treeSet.add("tom");//3
treeSet.add("sp");
treeSet.add("a");
treeSet.add("abc");//3
System.out.println("treeSet=" + treeSet);//输出treeSet=[a, abc, jack, sp, tom]
//此时是有序的
//2. 老师希望添加的元素,按照字符串大小来排序
//3. 使用TreeSet 提供的一个构造器,可以传入一个比较器(匿名内部类)
// 并指定排序规则
TreeSet treeSet2 = new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return ((String)o2).compareTo((String)o1);
//如果想按照长度改为return (((String)o1).length()).compareTo(((String)o1).length());
//注意此时如果想添加add.("abc")其实加入不进去,因为tom已经等于3
//而底层源码cmp只有大于和小于就行后续,而else直接return
};
});
/*源码
1. 构造器把传入的比较器对象,赋给了TreeSet 的底层的TreeMap 的属性this.comparator
public TreeSet(Comparator<? super E> comparator) {
this(new TreeMap<>(comparator));
}
2. 在调用treeSet.add("tom"), 在底层会执行到
if (cpr != null) {//cpr 就是我们的匿名内部类(对象)
do {
parent = t;
//动态绑定到我们的匿名内部类(对象)compare
cmp = cpr.compare(key, t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else //如果相等,即返回0,这个Key 就没有加入
return t.setValue(value);
} while (t != null);
}
*/
treeSet2.add("jack");
treeSet2.add("tom");//3
treeSet2.add("sp");
treeSet2.add("a");
treeSet2.add("abc");//3
System.out.println("treeSet2=" + treeSet2);
//输出treeSet2=[tom, sp, jack, abc, a]
}
}
P544TreeMap源码
public class HashMap2 {
public static void main(String[] args) {
/*
//1.默认的构造器
TreeMap treeMap = new TreeMap();
treeMap.put("jack", "杰克");
treeMap.put("tom", "汤姆");
treeMap.put("kristina", "克瑞斯提诺");
treeMap.put("smith", "斯密斯");
treeMap.put("hsp", "韩顺平");
System.out.println("treemap=" + treeMap);
//treemap={hsp=韩顺平, jack=杰克, kristina=克瑞斯提诺, smith=斯密斯, tom=汤姆}
*/
TreeMap treeMap = new TreeMap(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
// return ((String)o2).compareTo((String)o1);
//按照K(String)的大小排序
//按照KString)的长度排序
return ((String)o2).length()-((String)o1).length();
}
});
treeMap.put("jack", "杰克");
treeMap.put("tom", "汤姆");
treeMap.put("kristina", "克瑞斯提诺");
treeMap.put("smith", "斯密斯");
treeMap.put("hsp", "韩顺平");
System.out.println("treemap=" + treeMap);
//输出treemap={tom=汤姆, smith=斯密斯, kristina=克瑞斯提诺, jack=杰克, hsp=韩顺平}
//输出treemap={kristina=克瑞斯提诺, smith=斯密斯, jack=杰克, tom=韩顺平}
//此时不能加入treeMap.put("hsp", "韩顺平");,因为>,<,else直接其他不行
/*源码
1. 构造器. 把传入的实现了Comparator 接口的匿名内部类(对象),传给给TreeMap 的comparator
public TreeMap(Comparator<? super K> comparator) {
this.comparator = comparator;
}
2. 调用put 方法
2.1 第一次添加, 把k-v 封装到Entry 对象,放入root
Entry<K,V> t = root;
if (t == null) {
compare(key, key); // type (and possibly null) check
root = new Entry<>(key, value, null);
size = 1;
modCount++;
return null;
}
2.2 以后添加
Comparator<? super K> cpr = comparator;
if (cpr != null) {
do { //遍历所有的key , 给当前key 找到适当位置
parent = t;
cmp = cpr.compare(key, t.key);//动态绑定到我们的匿名内部类的compare
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else //如果遍历过程中,发现准备添加Key 和当前已有的Key 相等,就不添加
return t.setValue(value);
} while (t != null);
}
*/
}
}
P545Collections工具类1
public class Collections1 {
public static void main(String[] args) {
//创建ArrayList 集合,用于测试.
List list = new ArrayList();
list.add("tom");
list.add("smith");
list.add("king");
list.add("milan");
list.add("tom");
Collections.reverse(list);//1.反转list中的元素
//[tom, milan, king, smith, tom]
Collections.shuffle(list);//2.随机排序list中的元素
//每次输出不同[smith, tom, milan, tom, king]
Collections.sort(list);//3.自然排序list中的元素
// [king, milan, smith, tom, tom]
//sort(list, Comparator);//Comparator):4.根据指定的Comparator
// 产生的顺序对List 集合元素进行排序
Collections.sort(list, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return ((String)o2).length()-((String)o1).length();
}
});
//输出符串长度=[milan, smith, king, tom, tom]
//4. swap(List,int, int):将指定list 集合中的i 处元素和j 处元素进行交换
Collections.swap(list, 0, 1);
System.out.println("交换后的情况");
System.out.println("list=" + list);//输出list=[smith, milan, king, tom, tom]
P546Collections 工具类2
public class Collections1 {
public static void main(String[] args) {
//创建ArrayList 集合,用于测试.
List list = new ArrayList();
list.add("tom");
list.add("smith");
list.add("king");
list.add("milan");
list.add("tom");
System.out.println("list=" + list);//输出list=[smith, milan, king, tom, tom]
//1.Object max(Collection):根据元素的自然顺序,返回给定集合中的最大元素
System.out.println("自然顺序最大元素=" + Collections.max(list));
//输出自然顺序最大元素=tom
// 2.Object max(Collection,Comparator):
// 根据Comparator 指定的顺序,返回给定集合中的最大元素
// 我们要返回长度最大的元素
Object maxObject = Collections.max(list, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return ((String)o1).length() - ((String)o2).length();
//System.out.println("字符串长度="+list);
}
});
System.out.println("长度最大的元素=" + maxObject);
//输出长度最大的元素=smith
// 3.Object min(Collection)
// Object min(Collection,Comparator)
// 上面的两个方法,参考max 即可
//4. int frequency(Collection,Object):返回指定集合中指定元素的出现次数
System.out.println("tom 出现的次数=" + Collections.frequency(list, "tom"));
//输出2
//5.void copy(List dest,List src):将src 中的内容复制到dest 中
ArrayList dest = new ArrayList();
//为了完成一个完整拷贝,我们需要先给dest 赋值,大小和list.size()一样
for(int i = 0; i < list.size(); i++) {
dest.add("");
}
//拷贝
Collections.copy(dest, list);
System.out.println("dest=" + dest);
//6.boolean replaceAll(List list,Object oldVal,Object newVal):
// 使用新值替换List 对象的所有旧值
// 如果list 中,有tom 就替换成汤姆
Collections.replaceAll(list, "tom", "汤姆");
System.out.println(list);
//输出[汤姆, smith, king, milan, 汤姆]
}
}