常用集合

线程安全的集合:

淘汰Vector和HashTable,使用

CopyOnWriteArrayList:内部的方法使用了ReentrantLock加锁;

ConcurrentHashMap:则利用ReentrantLock实现了锁分离,锁的只是segment而不是整个Hash表

HashMap源码:

HashMap就是一个散列表,它是通过“拉链法”解决哈希冲突的。

四个构造函数(有一个子Map的构造函数)
构造函数有两个参数:初始容量(initialCapacity) 和加载因子(loadFactor)。

1、容量 是哈希表中桶的数量,初始容量只是哈希表在创建时的容量。(初始容量16)

2、加载因子 是哈希表在其容量自动增加之前可以达到多满的一种尺度。(默认0.75f)

当哈希表中的条目数超出了加载因子与当前容量的乘积时,则要对该哈希表进行 rehash 操作(即重建内部数据结构),从而哈希表将具有大约两倍的桶数。

HashMap中的key-value都是存储在Entry数组中的,本质是一个单向链表。

HashMap中的size都是存储key-value的个数。

遍历方法entrySet()、values()、keySet():

entrySet()实际上是通过newEntryIterator()实现的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class HashMap<K,V>
  5     extends AbstractMap<K,V>
  6     implements Map<K,V>, Cloneable, Serializable
  7 {
  8
  9     // 默认的初始容量是16,必须是2的幂。
 10     static final int DEFAULT_INITIAL_CAPACITY = 16;
 11
 12     // 最大容量(必须是2的幂且小于2的30次方,传入容量过大将被这个值替换)
 13     static final int MAXIMUM_CAPACITY = 1 << 30;
 14
 15     // 默认加载因子
 16     static final float DEFAULT_LOAD_FACTOR = 0.75f;
 17
 18     // 存储数据的Entry数组,长度是2的幂。
 19     // HashMap是采用拉链法实现的,每一个Entry本质上是一个单向链表
 20     transient Entry[] table;
 21
 22     // HashMap的大小,它是HashMap保存的键值对的数量
 23     transient int size;
 24
 25     // HashMap的阈值,用于判断是否需要调整HashMap的容量(threshold = 容量*加载因子)
 26     int threshold;
 27
 28     // 加载因子实际大小
 29     final float loadFactor;
 30
 31     // HashMap被改变的次数
 32     transient volatile int modCount;
<wiz_tmp_tag class="wiz-block-scroll">
 

集合转String类型:

String.join(",",(String[])listCode.toArray(new String[listCode.size()]))

Arraylist和Linkedlist:

1、ArrayList基于动态数组,可以动态扩容;

2、LinkedList基于双向链表,前后引用,指向head和tail;

3、LinkedList适合添加和删除元素,只需要改变前后引用,查找不适合用fori,4个数据会找6次;

4、ArrayList适合随机查找元素,有下标,4个数据查找4次,添加和删除需要复制数组到新数组,耗时。

5、Linkedlist不能指定集合初始容量,ArrayList可以指定容量有三个构造函数(空,集合,初始容量);

6、ArrayList扩容大小规则是,扩容后的大小= 原始大小+原始大小/2 + 1。(例如:原始大小是 10 ,扩容后的大小就是 10 + 5+1 = 16)

HashMap、TreeMap、LinkedHashMap和HashSet、TreeSet、LinkedHashSet:

1、HashMap基于散列表(哈希表),存储无序;

           哈希表依赖两个方法:hashCode()和equals()

2、TreeMap基于红黑树结构,按自然顺序和自定义顺序;

          在Student类中实现Comparable,重写compareTo方法

          在构造函数中new Comparator,匿名内部类,重写compare 方法

3、LinkedHashMap在HashMap的基础上实现了双向链表,按照插入顺序存储;

4、Set中的大部分方法是调用Map来实现的,value是一个虚拟值;

5、都允许有一个null键值,多个覆盖。

Collections和Arrays集合工具类:List中对象的排序:

1
2
3
4
5
6
7
8
Collections.sort(listout, new Comparator() {
@Override
   public int compare(Object o1, Object o2) {
      Map map1 = (Map) o1;
      Map map2 = (Map) o2;
      return map1.get("baseTime").toString().compareTo(map2.get("baseTime").toString());
   }
});

List初始化构造函数:

1
2
ArrayList<String> places = new ArrayList<String>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));//构造ArrayList(Collection c)
List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");

Comparator和Comparable接口排序:

1、Comparable属于Java.lang中,内比较器,自然比较方法,依赖ComparaTo方法实现;

2、若一个类实现了Comparable 接口,实现 Comparable 接口的类的对象的 List 列表 ( 或数组)可以通过 Collections.sort(或 Arrays.sort)进行排序;

3、Comparator属于Java.util中,外比较器,适用没有实现Comparable接口或者ComparaTo方法满足不了你的。

1
2
3
4
5
6
7
8
9
10
11
Collections.sort(listout);//升序
//升序,匿名内部类
Collections.sort(listout, new Comparator() {
  @Override
  public int compare(Object o1, Object o2) {
   String map1 = (String) o1;
   String map2 = (String) o2;
   return map1.compareTo(map2);
//  return map1.get("baseTime").toString().compareTo(map2.get("baseTime").toString());
            }
        });

    

List互转:

List<String> list = new ArrayList<String>();

list.add("a1");

list.add("a2");

String[] toBeStored = list.toArray(new String[list.size()]);

1、ArrarList 转换为 string[] :

  ArrayList list = new ArrayList();

  list.Add("aaa");

  list.Add("bbb");

  string[] arrString = (string[])list.ToArray(typeof( string)) ;

2、string[] 转换为 ArrarList :

        String r1[] = {"fd","df"};

  •         List list2 = Arrays.asList(r1);固定大小,不能add和remove。
  •         List list = new ArrayList<>(Arrays.asList(r1)); 可以add和remove。
  •         Collections.addAll(list, r1);

    非String类型的数值:r1为int类型时。

          for(int a: r1){

            list.add(a);

        }

3、ArrayList 转换为 string :

  ArrayList list = new ArrayList();

  list.Add("aaa");

  list.Add("bbb");

  string str= string.Join(",", (string[])list.ToArray(typeof( string)));

4、string 转换为 ArrayList :

  string str="1,2,3,4,5";

  ArrayList b = new ArrayList( str.Split(',') ) ;

 



1、将通过毅力完成的事转化为习惯。
2、清心寡欲、方能高枕无忧。
3、纸上得来终觉浅,绝知此事要躬行。

种一棵树最好的时间是 十年前。 其次是, 现在!

原文地址:https://www.cnblogs.com/shuchen007/p/9168097.html