自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(56)
  • 收藏
  • 关注

原创 HashMap面试题总结

基于版本1.8HashMap的扩容机制当size大于threshold的时候会扩容,并且是在插入数据之后进行resize。size是指map中的node个数总数。if (++size > threshold) resize();扩容的过程如果老的数组的长度超过了MAXIMUM_CAPACITY,threshold设为Integer.MAX_VALUE,直接return如果老的数组长度*2小于MAXIMUM_CAPACITY, threashold也乘2如果(

2021-06-08 22:50:33 180

原创 volatile面试题总结

volatile作用可见性:当一个线程修改了声明为volatile变量的值,新值对于要读该变量的线程来说是立即可见的有序性:代码的执行禁止指令重排序可见性和原子性的区别原子性:一个或多个操作在CPU执行过程中不中断称为原子性。对于volatile变量的读/写操作具有原子性,但是复合操作没有,比如i++。所以volatile并不保证原子性。volatile在JMM中的可见性原理在Java内存模型(JMM,Java Memory Model)中,所有的变量都存储在祝内存,每条线程都有.

2021-06-07 23:17:33 247

原创 不同缓存比较与方案

Ehcache vs Guava vs Caffeine:Ehcache:一般用做本地缓存,支持持久化,性能没有Caffeine好。Caffeine:号称高性能缓存,性能最好,可以理解为Guava升级版。Redis:分布式缓存,支持持久化,性能比本地缓存差一点。目前方案:Caffeine用作一级缓存,Redis用作二级缓存。或者Ehcache->Caffeine->Redis...

2020-09-27 16:35:55 465

翻译 Ehcache官网教程中文版

官网链接:https://www.ehcache.org/官网英文文档链接:https://www.ehcache.org/documentation/3.8/getting-started.htmlMaven 依赖: <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version

2020-09-25 11:40:13 2853

原创 类加载机制

ClassLoader源码笔记一般示意图中会把BootStrap ClassLoader画成是Ext的父类,但是BootStrap ClassLoader实际上没有父类也没有子类。所以并不是ExtClassLoader的父类。在创建Launcher对象时,先创建ExtClassLoader再创建AppClassLoader,创建AppClassLoader时会把父类指定为ExtClassL...

2020-04-10 14:23:52 80

原创 一张图总结Java堆的划分

2020-03-09 19:16:44 134

原创 一张图总结Java加载类的过程

2020-03-09 14:21:04 76

原创 一张图总结Java内存区域

2020-03-09 14:11:39 86

原创 ThreadPoolExecutor源码笔记

基本策略:1. 当一个新任务提交时,只要线程数小于核心线程数,就会立刻创建一个新的线程给他,即使已有的线程不在工作。2. 如果pool里面有超过核心线程数的线程,在他们KeepAliveTime的时间长度不工作后,会被停掉。3. 当超过核心线程数后,优先把请求放入队列。4. 如果一个请求不能被放入队列,一般会优先创建一个线程,除非会超过最大线程数。队列的三种处理方式:1. ...

2020-03-08 21:26:56 209

原创 AtomicInteger和CAS、volatile

AtomicInteger是一个int的包装类,线程安全。实现线程安全的方式主要是用到了CAS和volatile1. volatileprivate volatile int value;volatile的作用:可见性。一般情况下,变量值存在主内存里面。当新建了一个线程的时候,线程的工作内存从主内存中拷贝变量。当工作内存中的值发生变化的时候会刷新到主内存里面。但是有可能出现线程1...

2020-03-02 13:54:03 270

原创 225. 用队列实现栈

class MyStack { private Queue<Integer> queue1; private Queue<Integer> queue2; private Integer top; /** Initialize your data structure here. */ public MyStack() { ...

2020-03-01 17:34:20 63

原创 StringBuilder和String的效率比较

先来个实际对比,同样加"hello" 3000次,可以看出时间差距很大String方法String是用的StringBuffer,每次加新的String的时候就用一个新的array,全部拷贝进去。StringBuilder方法StringBuilder不同之处在于,扩容时会array会扩到接近原来两倍,所以之后再加字符串时免去扩容。 private int newC...

2020-03-01 17:32:01 486

原创 StringBuilder和StringBuilder源码笔记

API和StringBuffer兼容,不保证线程安全 相比于StringBuffer,更推荐StringBuilder,因为更快。 如果要线程安全就去用StringBuffer 初始容量16,容量不够时会自动扩容 一般情况下是不接受null值的,直接抛出空指针异常。但是如果是append StringBuffer或者StringBuilder,他们的值为null,会变成原来的是值加上nul...

2020-03-01 15:57:27 94

原创 70. 爬楼梯

class Solution { public int climbStairs(int n) { if (n == 0 || n == 1) return 1; int[] res = new int[n+1]; res[0] = res[1] = 1; for (int i = 2; i <= n; i++) { ...

2020-02-26 16:05:35 60

原创 338. 比特位计数

class Solution { public int[] countBits(int num) { int[] res = new int[num+1]; for (int i = 1; i <= num; i++) { res[i] = res[i & (i-1)] + 1; } r...

2020-02-26 15:13:06 65

原创 231. 2的幂

class Solution { public boolean isPowerOfTwo(int n) { long x = (long) n; if (x != 0 && (x & (x - 1)) == 0) return true; return false; }}注:依旧是x & ...

2020-02-26 13:06:00 88

原创 191. 位1的个数

public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { int res = 0; while (n != 0) { n = n & (n - 1); ...

2020-02-26 13:00:42 68

原创 79. 单词搜索

class Solution { public boolean exist(char[][] board, String word) { int m = board.length; int n = board[0].length; boolean[][] visited = new boolean[m][n]; for(in...

2020-02-26 12:25:39 64

原创 208. 实现 Trie (前缀树)

class Trie { class TrieNode { public char val; public boolean isWord; public TrieNode[] children = new TrieNode[26]; TrieNode(char c) { this.val = c;...

2020-02-26 11:17:32 59

原创 37. 解数独

class Solution { public void solveSudoku(char[][] board) { solve(board); } public boolean solve(char[][] board) { for (int i = 0; i < board.length; i++) for (int j = 0...

2020-02-25 18:50:20 74

原创 36. 有效的数独

class Solution { public boolean isValidSudoku(char[][] board) { return solve(board); } public boolean solve(char[][] board) { for (int i = 0; i < board.length; i++) fo...

2020-02-25 18:48:01 47

原创 51. N皇后

class Solution { Set<Integer> lie = new HashSet<>(); Set<Integer> pie = new HashSet<>(); Set<Integer> na = new HashSet<>(); List<List<Integer...

2020-02-25 16:14:11 66

原创 69. x 的平方根

class Solution { public int mySqrt(int x) { if (x == 0 || x == 1) return x; long l = 0; long r = x; long mid = (l + r) / 2;; while (r - l > 1) { ...

2020-02-25 12:42:05 51

原创 22. 括号生成

class Solution { public List<String> generateParenthesis(int n) { List<String> result = new ArrayList<>(); gen("", 0, 0, result, n); return result; }...

2020-02-25 11:26:51 47

原创 111. 二叉树的最小深度

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pub...

2020-02-25 09:38:08 49

原创 104. 二叉树的最大深度

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pub...

2020-02-25 09:31:41 43

原创 102. 二叉树的层次遍历

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pub...

2020-02-24 18:57:40 48

原创 122. 买卖股票的最佳时机 II

class Solution { public int maxProfit(int[] prices) { int res = 0; for (int i = 0; i < prices.length-1; i++) { if (prices[i+1] > prices[i]) res +...

2020-02-24 16:31:21 37

原创 169. 多数元素

class Solution { public int majorityElement(int[] nums) { Arrays.sort(nums); return nums[nums.length/2]; }}注:1.最容易想到的应该是用map,遍历数一遍2.这里稍微有点技巧,因为一定多于n/2,所以数组中间的值一定是次数最多...

2020-02-24 14:36:24 61

原创 50. Pow(x, n)

class Solution { public double myPow(double x, int n) { long N = n; if (N < 0) { N = -N; x = 1/x; } double res = 1.0; while (N !...

2020-02-24 12:41:40 49

原创 236. 二叉树的最近公共祖先

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pub...

2020-02-24 11:28:58 42

原创 98. 验证二叉搜索树

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pub...

2020-02-23 14:40:44 76

原创 15. 三数之和

class Solution { public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); List<List<Integer>> res = new ArrayList<>(); for (int i...

2020-02-23 13:50:25 44

原创 Spring事务的传递性

REQUIRED:A调用B,B抛出异常,Acatch住了也会回滚。REQUIRES_NEW:A调用B,B抛出异常,Acatch住就不会滚,没catch住就回滚。一般在有关联数据的时候用到事务...

2020-01-20 15:20:30 420

转载 abstract与static、synchronized、native不能同时使用的原因

原文地址:http://maxpin.iteye.com/blog/1156469    abstract是抽象的,指的是方法只有声明而没有实现,它的实现要放入声明该类的子类中实现。    static是静态的,是一种属于类而不属于对象的方法或者属性,而我们知道,类其实也是一个对象,他是在class文件加载到虚拟机以后就会产生的对象,通常来说它是单例的,就是整个虚拟机中只有一个这样的类对象...

2020-01-20 14:15:48 571

原创 1. 两数之和

class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); Set<Integer> set = new HashSet<>(); for...

2020-01-19 22:16:31 44

原创 242. 有效的字母异位词

class Solution { public boolean isAnagram(String s, String t) { Map<Character, Integer> map1 = new HashMap<>(); Map<Character, Integer> map2 = new HashMap<&gt...

2020-01-19 21:46:14 50

原创 239. 滑动窗口最大值

class Solution { public int[] maxSlidingWindow(int[] nums, int k) { if (nums.length * k == 0) return new int[0]; Deque<Integer> index = new ArrayDeque<>()...

2020-01-19 20:56:48 113

原创 703. 数据流中的第K大元素

class KthLargest { private PriorityQueue<Integer> q; private Integer k; public KthLargest(int k, int[] nums) { this.k = k; q = new PriorityQueue<>(k); ...

2020-01-19 11:52:28 78

原创 225. 用队列实现栈

class MyStack { private Queue<Integer> queue1; private Queue<Integer> queue2; private Integer top; /** Initialize your data structure here. */ public MyStack() { ...

2020-01-18 22:30:29 47

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除