avatar

最近动态

未分类

淘汰算法LRU

基于双链表实现LRU
原理: 将Cache的所有位置都用双连表连接起来,当一个位置被命中之后,就将通过调整链表的指向,将该位置调整到链表头的位置,新加入的Cache直接加到链表头中。这样,在多次进行Cache操作后,最近被命中的,就会被向链表头方向移动,而没有命中的,而想链表后面移动,链表尾则表示最近最少使用的Cache。当需要替换内容时候,链表的最后位置就是最少被命中的位置,我们只需要淘汰链表最后的部分即可。

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
public class LRUCache {  

//缓存大小
private int cacheSize;
//缓存容器
private Hashtable<K, CacheNode<K, V>> nodes;
//当前缓存对象数量
private int currentSize;
//(实现双链表)链表头
private CacheNode first;
//(实现双链表)链表尾
private CacheNode last;

public LRUCache(int i) {
currentSize = 0;
cacheSize = i;
nodes = new Hashtable(i);//缓存容器
}

/**
* 链表节点
*
*/
private static class CacheNode<K,V> {
CacheNode prev;
CacheNode next;
K key;
V value;
CacheNode() {
}
}

public V get(K key){
CacheNode<K, V> node = nodes.get(key);
if(node!=null){
moveToHead(node);
return node.value;
}else{

return null;
}
}

public void put(K key, V value){
CacheNode<K, V> node = nodes.get(key);
if(node == null){
if(currentSize >= cacheSize){
if(last != null){
nodes.remove(last.key);
}
removeLast();
}else{
currentSize++;
}
node = new CacheNode<>();
}
node.value=value;
node.key=key;
moveToHead(node);
nodes.put(key, node);
}

private void removeLast(){
if(last!=null){
if(last.prev != null){
last.prev.next = null;
}else{
first = null;
}
last = last.prev;
}
}

private void moveToHead(CacheNode node){
if(node == first){
return;
}

if (node.prev != null) {
node.prev.next = node.next;
}
if (node.next != null) {
node.next.prev = node.prev;
}
if (last == node) {
last = node.prev;
}
if (first != null) {
node.next = first;
first.prev = node;
}
first = node;
node.prev = null;
if (last == null) {
last = first;
}
}
}

阅读剩下更多

默认配图
未分类

链表相关算法

1.删除链表指定节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class LinkedNode{
public LinkedNode removeElement(LinkedNode head, int val){

//先从头结点开始删
while(head != null && head.val == val){
head = head.next;
}

if(head==null){
return head;
}

LinkedNode pre = head;
while(pre.next!=null){
if(pre.next.val==val){
pre.next = pre.next.next;
}else{
pre = pre.next;
}
}
return head;
}
}

2.有序链表插入元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class LinkedNode{
public void insertElement(LinkedNode node){
LinkedNode pre = null;
LinkedNode current = first;
while(current!=null&&node.value<current.value){
pre = current;
current = current.next;
}

if(pre==null){
first = node;
}else{
pre.next = node;
}
node.next = current;
}
}

3.判断链表是否有环

1
2
3
4
5
6
7
8
9
10
11
12
13
public static <T> boolean isLoopList(ListNode<T> head){
ListNode<T> slowPointer, fastPointer;

slowPointer = fastPointer = head;
while(fastPointer != null && fastPointer.next != null){
slowPointer=slowPointer.next;
fastPointer=fastPointer.next.next;
if(slowPointer == fastPointer){
return true;
}
}
return false;
}

阅读剩下更多

默认配图
未分类

String StringBuilder StringBuffer的异同

Java中一共有三总形式来表示字符串,分别为String、StringBuilder、StringBuffer,分别来分析下每一种形式的特点,以及他们之间的区别和联系。
1.String:又称为字符串常量,即一旦创建就不能修改。关于不能修改这一条,我们可以通过追踪源码看到,String内部实现用了一个final修饰的char数组来存字符串的值,因此,一旦给这个char数组赋值,那么这个值就不能再修改。关于String不能被继承的解释,在定义String类时,采用了final关键词进行修饰,因此我们知道了String类不能被用来继承。

阅读剩下更多

默认配图
未分类

Java动态代理

代理
代理是设计模式中的一种,分为静态代理和动态代理两种。
所谓静态代理就是在程序运行之前将要代理的类通过代码进行实现,而动态代理则是在程序运行时创建,代理类不会显示的在程序中实现出来,需要在运行时根据事先定义好的规则进行实现。根据定义静态代理适合一些需要单独生成代理类的场景,而动态代理则是用于一些统一处理的情况,这样做的好处是可以避免创建很多重复的代码,增强程序的可维护性和可读性。

阅读剩下更多

默认配图
未分类

二叉树的一些操作

反转二叉树

前一段时间国外某大牛面试Google被刷掉,原因是没有能够徒手写出来反转二叉树的代码,在网上看到了一个人的分析代码,特此记下来。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
root.left = invertTree(root.left);
root.right = invertTree(root.right);

TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
return root;
}
}

阅读剩下更多

默认配图
未分类

Java垃圾收集和内存分配策略

垃圾收集

判断对象是否存活的算法常用的有两种:
1.引用计数算法,给对象添加一个引用计数器,每次对象被引用的时候,计数器的值就加1;引用失效时,计数器值减1,当计数器的值都为0的对象就是不再被引用的。
优点:实现简单,判定效率高
缺点:对象之间互相循环引用的情况

2.根搜索算法,通过一系列的“GC Roots”对象作为起始点,从这些节点开始向下搜索,搜索所走的路径成为引用链(Reference Chain),当一个对象到GC Roots没有任何引用链相连时,则证明此对象是不可用的。
垃圾收集算法

阅读剩下更多

默认配图
未分类

关于Java技术的一些理解

刚接触java时,很不能理解jdk、java se、java ee、jdk1.5、j2ee 1.4等一些词语,感觉很混乱。后来通过查阅一些资料,知道jdk是java的核心,提供了java运行时环境、java工具盒java基础类库(也就是一些基础的API)。java的版本最初有两个,一个是java1.1和java1.2,后来为了区分将java1.2改为了java2,现在我们所说的java都是指java2。在java2之前的jdk版本都是1.1X的。java中jdk有三个j2se、j2ee、j2me,其中j2se称为标准版,也就是通用的一个版本,从jdk5.0改为Java SE;j2ee称为企业版,使用这种jdk开发j2ee应用程序,从5.0开始改为Java EE;ME(J2ME),micro edition,主要用于移动设备、嵌入式设备上的java应用程序,从JDK 5.0开始,改名为Java ME。

j2ee使用多层的分布式应用模型,典型分层是四层结构:
a.运行在客户端机器上的客户层组件
b.运行在J2ee服务器上的Web层组件
c.运行在j2ee服务器上的业务逻辑层组件
d.运行在EIS服务器上的企业信息系统(Enterprise informationsystem)层软件(好专业,就是与数据库的连接操作)


阅读剩下更多

默认配图
未分类

Spring框架学习笔记

一、Spring框架概述

1、 Spring框架提供了一个开发平台,用于整合其他技术,例如Struts,Hibernate,Mybatis等。举个例子Spring就像是一台电脑的主板,为各种硬件设施提供了接口,CPU,内存,硬盘都有各自的接口。这样做的好处也是显而易见的,比如在改善系统的结构方面,更利于系统的扩展和升级。
2、作用:a,提供了整合其他技术的API
b,提供了创建对象的功能,这样Spring就编程了一个大的工厂,所以spring是一个具有工厂功能的框架
c,spring提供了两种非常重要的机制IOC和AOP,这样的好处是降低了组件对象之间的耦合度。

阅读剩下更多

默认配图
未分类

经典排序算法

本次介绍几种经典排序算法以及Java实现代码
1.冒泡排序
冒泡排序是属于相邻两两比较进行排序的一种,由于其实现是经过1到n-1轮排序,
通过比较相邻的两个数,大的向下继续与相邻的进行比较,直到找到正确的顺序位置。

1
2
3
4
5
6
7
8
9
10
11
public void bubbleSort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {//比较轮次,经过n-1次比较
for (int j = 0; j < a.length - 1 - i; j++) {//每次比较相邻的两个数,大的向后移动
if (a[j] > a[j + 1]) {
int temp = a[j + 1];
a[j + 1] = a[j];
a[j] = temp;
}
}
}
}

阅读剩下更多

默认配图
未分类

二分查找Java实现

这个栗子是用Java实现的二分查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class BinarySearch {

public static int binarySearch(int array[], int number, int value) {
int left = 0;
int right = number - 1;
while (left <= right) {
int middle = left + ((right - left) >> 1);
if (array[middle] > value) {
right = middle - 1;
} else if (array[middle] < value) {
left = middle + 1;
} else {
return middle;
}
}
return -1;
}
}

阅读剩下更多

默认配图
未分类

求最大子序列和的问题

问题:给定(可能有负数)整数A1,A2,…,An,求∑Ak的最大值。(所有整数均为负数,则最大子序列和为0)。

1.穷举法
这种算法就是穷举所有的可能。for循环中的循环变量反映了Java中数组从0开始而不是从1开始这样一个事实。还有,本算法并不计算实际的子序列;实际的计算还要添加一些额外的代码。这个算法的运行时间为O(N3)

1
2
3
4
5
6
7
8
9
10
11
12
13
public static int maxSubSum1(int[] a) {
int maxSum = 0;
for(int i = 0; i < a.length; i++)
for(int j = i; j < a.length; j++){
int thisSum = 0;
for(int k = i; k <= j; k++)
thisSum += a[k];

if(thisSum > maxSum)
maxSum = thisSum;
}
return maxSum;
}

阅读剩下更多

默认配图
返回顶部