• hashmap和treemap、hashset和treeset、linkedhashmap


    集合框架图

    iterator--listIterator

    collection---list---arraylist  动态数组

    collection--list---abstractsequenceList--linkedlist 链表

    collection--list--vector     同步的动态数组

    collectiong-list-vector--stack  同步的栈

    collection--set--hashset      无序的

    collectiong--set--treeset      有序的

    map-abstractmap-hashmap--linkedhashmap

    map-sortedmap-treemap

    public interface Collection<E> extends Iterable<E>

    map接口:映射关键字到值的一个接口

    map.entry接口:描述映射中的元素,是Map的一个内部类

    sortMap接口:扩展Map以便按照升序排序

    hashtable 是散列表 线程安全的 

    java中线程安全体现如下:

      1、多个thread对同一个java实例的访问(read和modify)不会相互干扰,它主要体现在关键字synchronized.如ArrayList和Vector,HashMap和Hashtable

      (后者每个方法前都有synchronized关键字)。如果你在interator一个List对象时,其它线程remove一个element,问题就出现了。

      2、每个线程都有自己的字段,而不会在多个线程之间共享。它主要体现在java.lang.ThreadLocal类,而没有Java关键字支持,如像static、transient那样。

    1.hashmap--map        线程非安全  无序存储

      treemap--sortedmap 线程非安全   有序存储

      hashmap是哈希表

    2.hashset 是散列表  无序存储

       treeset    有序存储

    HashMap通常比TreeMap快一点(树和哈希表的数据结构使然),建议多使用HashMap,在需要排序的Map时候才用TreeMap.

      import java.util.HashMap;

      import java.util.Hashtable;

      import java.util.Iterator;

      import java.util.Map;

      import java.util.TreeMap;

      public class HashMaps {

      public static void main(String[] args) {

      Map<String, String> map = new HashMap<String, String>();

      map.put("a", "aaa");

      map.put("b", "bbb");

      map.put("c", "ccc");

      map.put("d", "ddd");

      Iterator<String> iterator = map.keySet().iterator();

      while (iterator.hasNext()) {

      Object key = iterator.next();

      System.out.println("map.get(key) is :" + map.get(key));

      }

      // 定义HashTable,用来测试

      Hashtable<String, String> tab = new Hashtable<String, String>();

      tab.put("a", "aaa");

      tab.put("b", "bbb");

      tab.put("c", "ccc");

      tab.put("d", "ddd");

      Iterator<String> iterator_1 = tab.keySet().iterator();

      while (iterator_1.hasNext()) {

      Object key = iterator_1.next();

      System.out.println("tab.get(key) is :" + tab.get(key));

      }

      TreeMap<String, String> tmp = new TreeMap<String, String>();

      tmp.put("a", "aaa");

      tmp.put("b", "bbb");

      tmp.put("c", "ccc");

      tmp.put("d", "cdc");

      Iterator<String> iterator_2 = tmp.keySet().iterator();

      while (iterator_2.hasNext()) {

      Object key = iterator_2.next();

      System.out.println("tmp.get(key) is :" + tmp.get(key));

      }

      }

      }

      运行结果如下:

      map.get(key) is :ddd

      map.get(key) is :bbb

      map.get(key) is :ccc

      map.get(key) is :aaa

      tab.get(key) is :bbb

      tab.get(key) is :aaa

      tab.get(key) is :ddd

      tab.get(key) is :ccc

      tmp.get(key) is :aaa

      tmp.get(key) is :bbb

      tmp.get(key) is :ccc

      tmp.get(key) is :cdc

      HashMap的结果是没有排序的,而TreeMap输出的结果是排好序的。

     transient使用小结

    1)一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问。

    2)transient关键字只能修饰变量,而不能修饰方法和类。注意,本地变量是不能被transient关键字修饰的。变量如果是用户自定义类变量,则该类需要实现Serializable接口。

    3)被transient关键字修饰的变量不再能被序列化,一个静态变量不管是否被transient修饰,均不能被序列化。

  • 相关阅读:
    埋点
    go 搭建web服务
    go的常见操作
    Zeus资源调度系统介绍
    支付系统中热点账户的性能问题
    redis
    集成Spring-Boot与gRPC,grpc-spring-boot-starter
    Spring Cloud灰度发布之Nepxion Discovery
    Spring Cloud Stream
    通过消息总线Spring Cloud Bus实现配置文件刷新(使用Kafka或RocketMQ)
  • 原文地址:https://www.cnblogs.com/zhengtu2015/p/4877035.html
Copyright © 2020-2023  润新知