• Home

  • 归档

  • 分类

  • 标签
Keep Coding
Keep Coding

11月
22
Java

Java 类的 hashCode 方法和 equals 方法

发表于 2017-11-22 • 分类于 Java

The contract between equals() and hashCode() is:

1) If two objects are equal, then they must have the same hash code.
2) If two objects have the same hash code, they may or may not be equal.

如果两个对象相同,则它们必须要有相同的 hash code,所以重写 equals 方法时,一定也要重写 hashCode 方法。
否则会出现不可预见的错误,例如下面的代码:

import java.util.HashMap;

public class Apple {
    private String color;

    public Apple(String color) {
        this.color = color;
    }

    public boolean equals(Object obj) {
        if(obj==null) return false;
        if (!(obj instanceof Apple))
            return false;    
        if (obj == this)
            return true;
        return this.color.equals(((Apple) obj).color);
    }

    public static void main(String[] args) {
        Apple a1 = new Apple("green");
        Apple a2 = new Apple("red");

        //hashMap stores apple type and its quantity
        HashMap<Apple, Integer> m = new HashMap<Apple, Integer>();
        m.put(a1, 10);
        m.put(a2, 20);
        System.out.println(m.get(new Apple("green")));
    }
}

打印结果输出 null。

阅读全文 »
11月
14
Liquibase

Liquibase "Could not acquire change log lock."

发表于 2017-11-14 • 分类于 Liquibase

Problem:

1
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [stash-context.xml]: Invocation of init method failed; nested exception is liquibase.exception.LockException: Could not acquire change log lock.  Currently locked by fe80:0:0:0:a00:27ff:fe6a:c787%2 (fe80:0:0:0:a00:27ff:fe6a:c787%2) since 12/6/12 2:08 PM

Cause
The DATABASECHANGELOGLOCK table has not been updated with the release lock information.
The likely cause of this is that the Stash instance was forced to quit while it was trying to migrate the database schema after an upgrade, with the consequence that the lock was not released. You should always wait for Stash to start up sufficiently for it to provide error messages if there are schema migration problems – never assume that it has hung and kill the process.
Another possible cause of not releasing the lock is that Stash was forced to quit while performing automatic application setup.

Resolution

Stash needs an exclusive lock on the DATABASECHANGELOGLOCK table in order to start so take care to disconnect / close the tool used to update the database. If you do not do this you may experience Stash hanging on start up with no errors in the logs and no response from the web server.

MySQL, SQL Server and Oracle:

1
UPDATE DATABASECHANGELOGLOCK SET LOCKED=0, LOCKGRANTED=null, LOCKEDBY=null where ID=1;

PostgreSQL:

1
UPDATE DATABASECHANGELOGLOCK SET LOCKED=0, LOCKGRANTED=null, LOCKEDBY=null where ID=1;

HSQLDB:

1
2
3
1- Shutdown Stash
2- Open your <STASH_HOME>/data/db.script and look for a string like:
INSERT INTO DATABASECHANGELOGLOCK VALUES(1,TRUE,'2012-06-12 14:08:00.982000','fe80:0:0:0:a00:27ff:fe6a:c787%2 (fe80:0:0:0:a00:27ff:fe6a:c787%2)')
阅读全文 »
10月
26
Java

Difference between path and classpath in Java

发表于 2017-10-26 • 分类于 Java

Difference between path and classpath in Java

path

path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer. In Java to run any program we use java tool and for compile Java code use javac tool. All these tools are available in bin folder so we set path upto bin folder.

classpath

classpath variable is set for providing path of all Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.

Difference between path and classPath

path variable is set for providing path for all java tools like java, javac, javap, javah, jar, appletviewer

classpath variable is set for provide path of all java classes which is used in our application.

阅读全文 »
10月
08
问题记录

aliyun OSS 上传 blob 文件

发表于 2017-10-08 • 分类于 问题记录

使用 cropper 图片裁剪插件上传时,前段将裁剪后图片转成了 blob 文件。现在要将该 blob 文件上传到阿里云的 OSS 上。

阅读全文 »
09月
07
Java

Java 8

发表于 2017-09-07 • 分类于 Java

函数式接口

https://www.cnblogs.com/heimianshusheng/p/5672641.html
http://lucida.me/blog/java-8-lambdas-insideout-language-features
https://www.cnblogs.com/jianwei-dai/p/5819479.html

函数式接口,简单的说就是指仅含有一个抽象方法的接口,以 @Functionalnterface 标注,注意,这里的抽象方法指的是该接口自己特有的抽象方法,而不包含它从其上级继承过来的抽象方法。
如 java.lang.Comparable,之前被称为 SAM 类型,即单抽象方法(Single Abstract Method)。我们并不需要声明一个接口是函数式接口,编译器会根据接口的结构自行判断,但是也可以用
@FunctionalInterface 来显式指定一个接口是函数式接口,加上这个注解之后,编译器就会验证该接口是否满足函数式接口的要求

Java8 的接口还可以定义一个 default 的方法,default 方法使用 default 关键字修饰,它是对象方法,需要使用对象来进行访问。

JAVA8接口中的default、static方法使用注意事项

Java8 中常用的全新的接口

Java7 中已经存在的函数式接口:

  • java.lang.Runnable
  • java.util.concurrent.Callable
  • java.security.PrivilegedAction
  • java.util.Comparator
  • java.io.FileFilter
  • java.beans.PropertyChangeListener

除此之外,Java8 中增加了一个新的包:java.util.function,它里面包含了常用的函数式接口,例如:

  • Predicate:接收 T 并返回 boolean
  • Consumer:接收 T,不返回值
  • Supplier:提供 T 对象(例如工厂),不接收值
  • Function<T, R>:接收 T,返回 R
  • UnaryOperator:接收 T 对象,返回 T
  • BinaryOperator:接收两个 T,返回 T

Java 中的 lambda 无法单独出现,它需要一个函数式接口来盛放,lambda 表达式方法体其实就是函数接口的实现。

阅读全文 »
08月
16
Java

Java 日期时间转换

发表于 2017-08-16 • 分类于 Java

JSR310 时间类型转换

1
2
3
4
5
6
7
final java.util.Date date = new java.util.Date();
final Timestamp timestamp = new Timestamp(date.getTime());
final Calendar calendar = Calendar.getInstance();

final Instant instant = Instant.now();
final LocalDateTime localDateTime = LocalDateTime.now();
final ZonedDateTime zonedDateTime = ZonedDateTime.now();
阅读全文 »
08月
15
Spring

JPA-Criteria-Querydsl

发表于 2017-08-15 • 分类于 Spring

JPA criteria 查询: 类型安全与面向对象

阅读全文 »
05月
11
Docker

Docker 的学习和使用

发表于 2017-05-11 • 分类于 Docker

Docker 的安装 (Ubuntu)

安装 Docker 需要下面任一64位 Ubuntu 系统版本:

  • Yakkety 16.10
  • Xenial 16.04
  • Trusty 14.04

查看 Linux 内核和 gcc 版本:

1
2
3
4
> $ cat /proc/version 
>
> Linux version 4.8.0-49-generic (buildd@lcy01-26) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) ) #52~16.04.1-Ubuntu SMP Thu Apr 20 10:55:59 UTC 2017
>

查看 Ubuntu Linux 系统版本:

1
2
3
4
5
6
7
> $ lsb_release -a
> No LSB modules are available.
> Distributor ID: Ubuntu
> Description: Ubuntu 16.04.2 LTS
> Release: 16.04
> Codename: xenial
>
阅读全文 »
04月
16
JS

循环调用 AJAX 请求

发表于 2017-04-16 • 分类于 JS

在页面循环发送 ajax 请求时,不能直接使用循环就去请求,这样会造成很多的问题。

阅读全文 »
04月
08
问题记录

HTTP 协议和 HTTPS 协议的 GET 方法请求

发表于 2017-04-08 • 分类于 问题记录

HTTP 协议请求

方法一:

获取 HTTP 协议请求状态码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public int requestHttpGet(String url) {
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(9000).setConnectionRequestTimeout(9000).build();
httpGet.setConfig(requestConfig);
CloseableHttpClient httpClient = null;
HttpResponse response;
try {
httpClient = HttpClients.createDefault();
response = httpClient.execute(httpGet);
return response.getStatusLine().getStatusCode();
} catch (Exception e) {
log.error("HTTP 协议 GET 请求出错:" + e.getMessage());
return 408;
} finally {
try {
httpClient.close();
} catch (IOException e) {
log.error("关闭 CloseableHttpClient 对象出现错误:" + e.getMessage());
}
}
}
阅读全文 »
1…45678
wuchao

72 日志
18 分类
Creative Commons

博客已萌萌哒运行(●'◡'●)ノ♥

© 2020 Keep Coding. 由 Hexo 强力驱动. Theme By Sagiri v0.0.4.

Made with by wuchao.