Hello Coder


  • 首页

  • 归档

  • 标签

  • 搜索
close

HashMap

发表于 2016-08-01

OpenJDK 源代码阅读之 HashMap


概要

  • 类继承关系
1
2
3
java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,V>
  • 定义
1
2
3
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
  • 核心成员变量
1
2
3
static final float DEFAULT_LOAD_FACTOR = 0.75f; //默认装载因子
final float loadFactor; //装载因子
transient Node<K,V>[] table; //数组,作为Hash桶
  • 内部节点
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
static class Node<K,V> implements Map.Entry<K,V> {
final int hash; //hash值
final K key; //键
V value; //值
Node<K,V> next; //指向hash值相同的下一个节点
Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
  • 要点

1) 与 Hashtable 区别在于:非同步,允许 null
2) 不保证次序,甚至不保证次序随时间不变
3) 基本操作 put, get 常量时间
4) 遍历操作 与 capacity+size 成正比
5) HashMap 性能与 capacity 和 load factor 相关,load factor 是当前元素个数与 capacity 的比值,通常设定为 0.75,如果此值过大,空间利用率高,但是冲突的可能性增加,因而可能导致查找时间增加,如果过小,反之。当元素个数大于 capacity * load_factor 时,HashMap 会重新安排 Hash 表。因此高效地使用 HashMap 需要预估元素个数,设置最佳的 capacity 和 load factor ,使得重新安排 Hash 表的次数下降。

阅读全文 »

git使用小记

发表于 2016-08-01

Git使用小记

前言:

我们写代码写完后总要进行管理,以前写的很多代码虽然写的不是很好,但因为没有一个比较好的代码管理习惯,所以很多都遗失掉了,为此现在都还觉得很可惜,近来在学习使用git来进行代码管理,git是一个很强大的分布式版本控制系统。

git

git工作流程

了解git,首先要弄清楚对象在被git管理过程中所处的4个阶段,分别是:工作目录、index(又称为暂存区)、本地仓库和远程仓库。

从时间先后来讲,工作目录的内容是你当前看到的,也是最新的;index区标记了你当前工作目录中,哪些内容是被git管理的;而本地仓库保存了对象被提交过的各个版本,比起工作目录和暂存区的内容,它要更旧一些;远程仓库是本地仓库的异地备份,远程仓库的内容可能被分布在多个地点的处于协作关系的本地仓库修改,因此它可能与本地仓库同步,也可能不同步,但是它的内容是最旧的。

任何对象都是在工作目录中诞生和被修改;任何修改都是从进入index区才开始被版本控制;只有把修改提交到本地仓库,该修改才能在仓库中留下痕迹;而要与协作者分享本地的修改,可以把它们push到远程仓库来共享。图最上方的add、commit、push等,展示了git仓库的产生过程。反过来,我们可以从远程历史仓库中获得本地仓库的最后一个版本,clone到本地,从本地检出对象的各个版本到index暂存区或工作目录中,从而实现任何对象或整个仓库的任意阶段状态的”回滚”。当正向和反向都能自由切换后,git就强大到无所不能了。

阅读全文 »

ArrayList

发表于 2016-07-29

OpenJDK 源码阅读之 ArrayList


概要

  • 类继承关系
1
2
3
4
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<E>
  • 定义
1
2
3
4
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
}

实现

  • transient
1
private transient Object[] elementData;

声明为 transient后,这个字段不会被序列化。

  • toArray
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}

注意对 elementData 的检查,Bug 6260652中对此有详细描述。主要原因是 c.toArray() 不一定会返回 Object[] 类型的值。

阅读全文 »

java-coding-style

发表于 2016-07-29

Java编码风格

前言

这份文档源自Google Java编程风格规范,并根据我的想法,做了一些调整,当且仅当一个Java源文件符合此文档中的规则, 我们才认为它符合Ximalaya直播team的Java编程风格。

与其它的编程风格指南一样,这里所讨论的不仅仅是编码格式美不美观的问题, 同时也讨论一些约定及编码标准。然而,这份文档主要侧重于我们所普遍遵循的规则,对于那些不是强制要求的,我们尽量不提供意见。

1.1 术语说明

在本文档中,除非另有说明:

术语class可表示一个普通类,枚举类,接口或是annotation类型(@interface)
术语comment只用来指代实现的注释(implementation comments),我们不使用“documentation comments”一词,而是用Javadoc。
其他的术语说明会偶尔在后面的文档出现。

1.2 指南说明

本文档中的示例代码并不作为规范。也就是说,虽然示例代码是遵循Ximalaya编程风格,但并不意味着这是展现这些代码的唯一方式。 示例中的格式选择不应该被强制定为规则。

阅读全文 »

Hello World

发表于 2016-07-26

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

1…3031
David

David

Develop Notes

155 日志
37 标签
GitHub Weibo
© 2016 - 2020 David
由 Hexo 强力驱动
主题 - NexT.Pisces