import java.util.LinkedHashMap;
import java.util.Properties;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.WeakHashMap;
/* 2017-02-18 13:20:26
* LinkedHashMap 是HashMap 的子类
* 使用的是双向链表来维护 key value 对的次序
* 该链表负责维护Map的迭代顺序
* 迭代的顺序与key-value 对的插入顺序保持一致
*/
/*
* Properties 读写属性文件 是Hashtable 的子类
* 该类把map对象和属性文件管理起来
* 从而把map对象中的key-value 写入属性文件中,也可以把属性文件中的 属性 加入到map对象中,
* 属性文件中的key value 都是字符串类型
* 是一个 Map<String,String>
*/
/*
* Set
* -SoredSet
* -TreeSet
*
* Map
* -SortedMap
* -TreeMap
*
* TreeMap 就是一个红黑树数据结构
* 根据key对节点进行排序
* TreeMap可以抱枕所有的key-value对处于有序状态
*
* 自然排序 实现 Comparable 接口 key 应该是同一个类的对象
* 定制排序 创建TreeMap时 传入一个Comparator 对象 定制排序不要去实现 Comparable 接口
* TreeMap中的key判断标准是两个key通过compareTo方法 返回0 这两个可以就是相等的
*/
/* 2017-02-18 14:20:24
* WeakHashMap 实现类
* HashMap 的key保留了对实际对象的强引用 只要不被销毁,该HashMap的所有key所引用的对象就不会被垃圾回收
*
* WeakHashMap 的key 值保留了对实际对象的弱引用,也意味着 key 没有被其他引用 这些key 所引用的对象可能被垃圾回收
* 也可能自动删除这些key所对应的key-value对
*
*
*
*/
class s implements Comparable{
int count;
public s(int count) {
this.count = count;
}
public String toString() {
return "s [count=" + count + "]";
}
public int compareTo(Object o) {
s s2 = (s) o;
return count > s2.count ? 1:
count < s2.count ? -1 : 0 ;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj != null && obj.getClass() == s.class) {
s s1 = (s) obj;
return s1 .count == this .count ;
}
return false;
}
}
public class LinkedHashMapj {
public static void main(String[] args) {
LinkedHashMap linkedHashMap = new LinkedHashMap ();
linkedHashMap.put("asd", 80);
linkedHashMap.forEach((key,value) -> System.out.println(key +" = = " + value));
Properties properties = new Properties();
properties.setProperty("name", "liu");
// properties.store(out, comments) ; 将properties 中的值 保存到 配置文件中
// properties.load(inStream); 加载 配置文件
// properties.getProperty(key) 获取属性
// treemapj();
System.out.println();
WeakHashMap weakHashMap = new WeakHashMap<>();
weakHashMap.put( new String("asdf"), new String("123"));
weakHashMap.put( new String("adfasdfasd"), new String("1222223"));
weakHashMap.put( new String("asdfasdfsadd"), new String("111111123"));
System.out.println(weakHashMap);
System.gc();
System.out.println("gc..."+ weakHashMap); // 弱引用 的会被系统回收
}
/* 2017-02-18 14:46:56
*
*/
private static void treemapj() {
System.out.println(1 > 2 ? 3 : 4 < 5 ? 6 : 7); //6
if (1>2) {
System.out.println(3);
}else {
if (4<5) {
System.out.println(6);
}else {
System.out.println(7);
}
}
System.out.println();
TreeMap treeMap = new TreeMap<>();
treeMap.put(new s(3), "san");
treeMap.put(new s(19), "aaa");
treeMap.put(new s(8), "b");
System.out.println(treeMap);
System.out.println("treeMap.firstEntry() = "+treeMap.firstEntry());
System.out.println("lastKey" + treeMap.lastKey());
System.out.println("treeMap.higherKey(new S(6))" + treeMap.higherKey(new s(6)));
}
}
LinkedHashMap
最新推荐文章于 2021-11-10 23:04:24 发布