Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
yerui2008198
toBeTopJavaer
提交
22b7675f
T
toBeTopJavaer
项目概览
yerui2008198
/
toBeTopJavaer
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
toBeTopJavaer
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
22b7675f
编写于
6月 14, 2020
作者:
H
hollis.zhl
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
基础篇部分知识完善
上级
d2dd28bb
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
726 addition
and
11 deletion
+726
-11
docs/_sidebar.md
docs/_sidebar.md
+7
-5
docs/basics/java-basic/apache-collections.md
docs/basics/java-basic/apache-collections.md
+388
-0
docs/basics/java-basic/convert-bytestream-characterstream.md
docs/basics/java-basic/convert-bytestream-characterstream.md
+43
-0
docs/basics/java-basic/enum-switch.md
docs/basics/java-basic/enum-switch.md
+13
-1
docs/basics/java-basic/h2-db.md
docs/basics/java-basic/h2-db.md
+152
-0
docs/basics/java-basic/iteration-of-collection.md
docs/basics/java-basic/iteration-of-collection.md
+35
-0
docs/basics/java-basic/replace-in-string.md
docs/basics/java-basic/replace-in-string.md
+83
-0
docs/menu.md
docs/menu.md
+5
-5
未找到文件。
docs/_sidebar.md
浏览文件 @
22b7675f
*
基础篇
* 面向对象
...
...
@@ -65,7 +67,7 @@
* [JDK 6和JDK 7中substring的原理及区别](/basics/java-basic/substring.md)
*
replaceFirst、replaceAll、replace区别
*
[replaceFirst、replaceAll、replace区别](/basics/java-basic/replace-in-string.md)
* [String对“+”的重载](/basics/java-basic/string-append.md)
...
...
@@ -127,13 +129,13 @@
* [Java 8中stream相关用法](/basics/java-basic/stream.md)
*
Apache集合处理工具类的使用
*
[Apache集合处理工具类的使用](/basics/java-basic/apache-collections.md)
* 不同版本的JDK中HashMap的实现的区别以及原因
* [Arrays.asList获得的List使用时需要注意什么](/basics/java-basic/Arrays-asList.md)
*
Collection如何迭代
*
[Collection如何迭代](/basics/java-basic/iteration-of-collection.md)
* [Enumeration和Iterator区别](/basics/java-basic/Enumeration-vs-Iterator.md)
...
...
@@ -169,7 +171,7 @@
* [输入流、输出流](/basics/java-basic/input-stream-vs-output-stream.md)
*
字节流和字符流之间的相互转换
*
[字节流和字符流之间的相互转换](/basics/java-basic/convert-bytestream-characterstream.md)
* [同步、异步](/basics/java-basic/synchronized-vs-asynchronization.md)
...
...
@@ -271,7 +273,7 @@
* [mockito](/basics/java-basic/ut-with-mockito.md)
*
内存数据库(h2)
*
[内存数据库(h2)](/basics/java-basic/h2-db.md)
* 正则表达式
...
...
docs/basics/java-basic/apache-collections.md
0 → 100644
浏览文件 @
22b7675f
Commons Collections增强了Java Collections Framework。 它提供了几个功能,使收集处理变得容易。 它提供了许多新的接口,实现和实用程序。 Commons Collections的主要功能如下
*
Bag - Bag界面简化了每个对象具有多个副本的集合。
*
BidiMap - BidiMap接口提供双向映射,可用于使用值使用键或键查找值。
*
MapIterator - MapIterator接口提供简单而容易的迭代迭代。
*
Transforming Decorators - 转换装饰器可以在将集合添加到集合时更改集合的每个对象。
*
Composite Collections - 在需要统一处理多个集合的情况下使用复合集合。
*
Ordered Map - 有序地图保留添加元素的顺序。
*
Ordered Set - 有序集保留了添加元素的顺序。
*
Reference map - 参考图允许在密切控制下对键/值进行垃圾收集。
*
Comparator implementations - 可以使用许多Comparator实现。
*
Iterator implementations - 许多Iterator实现都可用。
*
Adapter Classes - 适配器类可用于将数组和枚举转换为集合。
*
Utilities - 实用程序可用于测试测试或创建集合的典型集合论属性,例如union,intersection。 支持关闭。
### Commons Collections - Bag
Bag定义了一个集合,用于计算对象在集合中出现的次数。 例如,如果Bag包含{a,a,b,c},则getCount(“a”)将返回2,而uniqueSet()将返回唯一值。
```
java
import
org.apache.commons.collections4.Bag
;
import
org.apache.commons.collections4.bag.HashBag
;
public
class
BagTester
{
public
static
void
main
(
String
[]
args
)
{
Bag
<
String
>
bag
=
new
HashBag
<>();
//add "a" two times to the bag.
bag
.
add
(
"a"
,
2
);
//add "b" one time to the bag.
bag
.
add
(
"b"
);
//add "c" one time to the bag.
bag
.
add
(
"c"
);
//add "d" three times to the bag.
bag
.
add
(
"d"
,
3
);
//get the count of "d" present in bag.
System
.
out
.
println
(
"d is present "
+
bag
.
getCount
(
"d"
)
+
" times."
);
System
.
out
.
println
(
"bag: "
+
bag
);
//get the set of unique values from the bag
System
.
out
.
println
(
"Unique Set: "
+
bag
.
uniqueSet
());
//remove 2 occurrences of "d" from the bag
bag
.
remove
(
"d"
,
2
);
System
.
out
.
println
(
"2 occurences of d removed from bag: "
+
bag
);
System
.
out
.
println
(
"d is present "
+
bag
.
getCount
(
"d"
)
+
" times."
);
System
.
out
.
println
(
"bag: "
+
bag
);
System
.
out
.
println
(
"Unique Set: "
+
bag
.
uniqueSet
());
}
}
```
它将打印以下结果:
```
d is present 3 times.
bag: [2:a,1:b,1:c,3:d]
Unique Set: [a, b, c, d]
2 occurences of d removed from bag: [2:a,1:b,1:c,1:d]
d is present 1 times.
bag: [2:a,1:b,1:c,1:d]
Unique Set: [a, b, c, d]
```
### Commons Collections - BidiMap
使用双向映射,可以使用值查找键,并且可以使用键轻松查找值。
```
java
import
org.apache.commons.collections4.BidiMap
;
import
org.apache.commons.collections4.bidimap.TreeBidiMap
;
public
class
BidiMapTester
{
public
static
void
main
(
String
[]
args
)
{
BidiMap
<
String
,
String
>
bidi
=
new
TreeBidiMap
<>();
bidi
.
put
(
"One"
,
"1"
);
bidi
.
put
(
"Two"
,
"2"
);
bidi
.
put
(
"Three"
,
"3"
);
System
.
out
.
println
(
bidi
.
get
(
"One"
));
System
.
out
.
println
(
bidi
.
getKey
(
"1"
));
System
.
out
.
println
(
"Original Map: "
+
bidi
);
bidi
.
removeValue
(
"1"
);
System
.
out
.
println
(
"Modified Map: "
+
bidi
);
BidiMap
<
String
,
String
>
inversedMap
=
bidi
.
inverseBidiMap
();
System
.
out
.
println
(
"Inversed Map: "
+
inversedMap
);
}
}
```
它将打印以下结果。
```
1
One
Original Map: {One=1, Three=3, Two=2}
Modified Map: {Three=3, Two=2}
Inversed Map: {2=Two, 3=Three}
```
### Commons Collections - MapIterator
JDK Map接口很难迭代,因为迭代要在EntrySet或KeySet对象上完成。 MapIterator提供了对Map的简单迭代。
```
java
import
org.apache.commons.collections4.IterableMap
;
import
org.apache.commons.collections4.MapIterator
;
import
org.apache.commons.collections4.map.HashedMap
;
public
class
MapIteratorTester
{
public
static
void
main
(
String
[]
args
)
{
IterableMap
<
String
,
String
>
map
=
new
HashedMap
<>();
map
.
put
(
"1"
,
"One"
);
map
.
put
(
"2"
,
"Two"
);
map
.
put
(
"3"
,
"Three"
);
map
.
put
(
"4"
,
"Four"
);
map
.
put
(
"5"
,
"Five"
);
MapIterator
<
String
,
String
>
iterator
=
map
.
mapIterator
();
while
(
iterator
.
hasNext
())
{
Object
key
=
iterator
.
next
();
Object
value
=
iterator
.
getValue
();
System
.
out
.
println
(
"key: "
+
key
);
System
.
out
.
println
(
"Value: "
+
value
);
iterator
.
setValue
(
value
+
"_"
);
}
System
.
out
.
println
(
map
);
}
}
```
它将打印以下结果。
```
key: 3
Value: Three
key: 5
Value: Five
key: 2
Value: Two
key: 4
Value: Four
key: 1
Value: One
{3=Three_, 5=Five_, 2=Two_, 4=Four_, 1=One_}
```
### Commons Collections - OrderedMap
OrderedMap是地图的新接口,用于保留添加元素的顺序。 LinkedMap和ListOrderedMap是两个可用的实现。 此接口支持Map的迭代器,并允许在Map中向前或向后迭代两个方向。
```
java
import
org.apache.commons.collections4.OrderedMap
;
import
org.apache.commons.collections4.map.LinkedMap
;
public
class
OrderedMapTester
{
public
static
void
main
(
String
[]
args
)
{
OrderedMap
<
String
,
String
>
map
=
new
LinkedMap
<
String
,
String
>();
map
.
put
(
"One"
,
"1"
);
map
.
put
(
"Two"
,
"2"
);
map
.
put
(
"Three"
,
"3"
);
System
.
out
.
println
(
map
.
firstKey
());
System
.
out
.
println
(
map
.
nextKey
(
"One"
));
System
.
out
.
println
(
map
.
nextKey
(
"Two"
));
}
}
```
它将打印以下结果。
```
One
Two
Three
```
### Commons Collections - Ignore NULL
Apache Commons Collections库的CollectionUtils类为常见操作提供了各种实用方法,涵盖了广泛的用例。 它有助于避免编写样板代码。 这个库在jdk 8之前非常有用,因为Java 8的Stream API现在提供了类似的功能。
```
java
import
java.util.LinkedList
;
import
java.util.List
;
import
org.apache.commons.collections4.CollectionUtils
;
public
class
CollectionUtilsTester
{
public
static
void
main
(
String
[]
args
)
{
List
<
String
>
list
=
new
LinkedList
<
String
>();
CollectionUtils
.
addIgnoreNull
(
list
,
null
);
CollectionUtils
.
addIgnoreNull
(
list
,
"a"
);
System
.
out
.
println
(
list
);
if
(
list
.
contains
(
null
))
{
System
.
out
.
println
(
"Null value is present"
);
}
else
{
System
.
out
.
println
(
"Null value is not present"
);
}
}
}
```
它将打印以下结果。
```
[a]
Null value is not present
```
### Merge & Sort
Apache Commons Collections库的CollectionUtils类为常见操作提供了各种实用方法,涵盖了广泛的用例。 它有助于避免编写样板代码。 这个库在jdk 8之前非常有用,因为Java 8的Stream API现在提供了类似的功能。
```
java
import
java.util.Arrays
;
import
java.util.List
;
import
org.apache.commons.collections4.CollectionUtils
;
public
class
CollectionUtilsTester
{
public
static
void
main
(
String
[]
args
)
{
List
<
String
>
sortedList1
=
Arrays
.
asList
(
"A"
,
"C"
,
"E"
);
List
<
String
>
sortedList2
=
Arrays
.
asList
(
"B"
,
"D"
,
"F"
);
List
<
String
>
mergedList
=
CollectionUtils
.
collate
(
sortedList1
,
sortedList2
);
System
.
out
.
println
(
mergedList
);
}
}
```
它将打印以下结果。
```
[A, B, C, D, E, F]
```
### 安全空检查(Safe Empty Checks)
Apache Commons Collections库的CollectionUtils类为常见操作提供了各种实用方法,涵盖了广泛的用例。 它有助于避免编写样板代码。 这个库在jdk 8之前非常有用,因为Java 8的Stream API现在提供了类似的功能。
```
java
import
java.util.List
;
import
org.apache.commons.collections4.CollectionUtils
;
public
class
CollectionUtilsTester
{
public
static
void
main
(
String
[]
args
)
{
List
<
String
>
list
=
getList
();
System
.
out
.
println
(
"Non-Empty List Check: "
+
checkNotEmpty1
(
list
));
System
.
out
.
println
(
"Non-Empty List Check: "
+
checkNotEmpty1
(
list
));
}
static
List
<
String
>
getList
()
{
return
null
;
}
static
boolean
checkNotEmpty1
(
List
<
String
>
list
)
{
return
!(
list
==
null
||
list
.
isEmpty
());
}
static
boolean
checkNotEmpty2
(
List
<
String
>
list
)
{
return
CollectionUtils
.
isNotEmpty
(
list
);
}
}
```
它将打印以下结果。
```
Non-Empty List Check: false
Non-Empty List Check: false
```
### Commons Collections - Inclusion
检查列表是否是另一个列表的一部分
```
java
import
java.util.Arrays
;
import
java.util.List
;
import
org.apache.commons.collections4.CollectionUtils
;
public
class
CollectionUtilsTester
{
public
static
void
main
(
String
[]
args
)
{
//checking inclusion
List
<
String
>
list1
=
Arrays
.
asList
(
"A"
,
"A"
,
"A"
,
"C"
,
"B"
,
"B"
);
List
<
String
>
list2
=
Arrays
.
asList
(
"A"
,
"A"
,
"B"
,
"B"
);
System
.
out
.
println
(
"List 1: "
+
list1
);
System
.
out
.
println
(
"List 2: "
+
list2
);
System
.
out
.
println
(
"Is List 2 contained in List 1: "
+
CollectionUtils
.
isSubCollection
(
list2
,
list1
));
}
}
```
它将打印以下结果。
```
List 1: [A, A, A, C, B, B]
List 2: [A, A, B, B]
Is List 2 contained in List 1: true
```
### Commons Collections - Intersection
用于获取两个集合(交集)之间的公共对象
```
java
import
java.util.Arrays
;
import
java.util.List
;
import
org.apache.commons.collections4.CollectionUtils
;
public
class
CollectionUtilsTester
{
public
static
void
main
(
String
[]
args
)
{
//checking inclusion
List
<
String
>
list1
=
Arrays
.
asList
(
"A"
,
"A"
,
"A"
,
"C"
,
"B"
,
"B"
);
List
<
String
>
list2
=
Arrays
.
asList
(
"A"
,
"A"
,
"B"
,
"B"
);
System
.
out
.
println
(
"List 1: "
+
list1
);
System
.
out
.
println
(
"List 2: "
+
list2
);
System
.
out
.
println
(
"Commons Objects of List 1 and List 2: "
+
CollectionUtils
.
intersection
(
list1
,
list2
));
}
}
```
它将打印以下结果。
```
List 1: [A, A, A, C, B, B]
List 2: [A, A, B, B]
Commons Objects of List 1 and List 2: [A, A, B, B]
```
### Commons Collections - Subtraction
通过从其他集合中减去一个集合的对象来获取新集合
```
java
import
java.util.Arrays
;
import
java.util.List
;
import
org.apache.commons.collections4.CollectionUtils
;
public
class
CollectionUtilsTester
{
public
static
void
main
(
String
[]
args
)
{
//checking inclusion
List
<
String
>
list1
=
Arrays
.
asList
(
"A"
,
"A"
,
"A"
,
"C"
,
"B"
,
"B"
);
List
<
String
>
list2
=
Arrays
.
asList
(
"A"
,
"A"
,
"B"
,
"B"
);
System
.
out
.
println
(
"List 1: "
+
list1
);
System
.
out
.
println
(
"List 2: "
+
list2
);
System
.
out
.
println
(
"List 1 - List 2: "
+
CollectionUtils
.
subtract
(
list1
,
list2
));
}
}
```
它将打印以下结果。
```
List 1: [A, A, A, C, B, B]
List 2: [A, A, B, B]
List 1 - List 2: [A, C]
```
### Commons Collections - Union
用于获取两个集合的并集
```
java
import
java.util.Arrays
;
import
java.util.List
;
import
org.apache.commons.collections4.CollectionUtils
;
public
class
CollectionUtilsTester
{
public
static
void
main
(
String
[]
args
)
{
//checking inclusion
List
<
String
>
list1
=
Arrays
.
asList
(
"A"
,
"A"
,
"A"
,
"C"
,
"B"
,
"B"
);
List
<
String
>
list2
=
Arrays
.
asList
(
"A"
,
"A"
,
"B"
,
"B"
);
System
.
out
.
println
(
"List 1: "
+
list1
);
System
.
out
.
println
(
"List 2: "
+
list2
);
System
.
out
.
println
(
"Union of List 1 and List 2: "
+
CollectionUtils
.
union
(
list1
,
list2
));
}
}
```
它将打印以下结果。
```
List 1: [A, A, A, C, B, B]
List 2: [A, A, B, B]
Union of List 1 and List 2: [A, A, A, B, B, C]
```
原文地址:https://iowiki.com/commons_collections/commons_collections_index.html
\ No newline at end of file
docs/basics/java-basic/convert-bytestream-characterstream.md
0 → 100644
浏览文件 @
22b7675f
想要实现字符流和字节流之间的相互转换需要用到两个类:
OutputStreamWriter 是字符流通向字节流的桥梁
InputStreamReader 是字节流通向字符流的桥梁
### 字符流转成字节流
```
public static void main(String[] args) throws IOException {
File f = new File("test.txt");
// OutputStreamWriter 是字符流通向字节流的桥梁,创建了一个字符流通向字节流的对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(f),"UTF-8");
osw.write("我是字符流转换成字节流输出的");
osw.close();
}
```
### 字节流转成字符流
```
public static void main(String[] args) throws IOException {
File f = new File("test.txt");
InputStreamReader inr = new InputStreamReader(new FileInputStream(f),"UTF-8");
char[] buf = new char[1024];
int len = inr.read(buf);
System.out.println(new String(buf,0,len));
inr.close();
}
```
\ No newline at end of file
docs/basics/java-basic/enum-switch.md
浏览文件 @
22b7675f
Java 1.7 之前 switch 参数可用类型为 short、byte、int、char,枚举类型之所以能使用其实是编译器层面实现的,编译器会将枚举 switch 转换为类似 switch(s.ordinal()) { case Status.START.ordinal() } 形式,所以实质还是 int 参数类型,感兴趣的可以自己写个使用枚举的 switch 代码然后通过 javap -v 去看下字节码就明白了。
\ No newline at end of file
Java 1.7 之前 switch 参数可用类型为 short、byte、int、char,枚举类型之所以能使用其实是编译器层面实现的
编译器会将枚举 switch 转换为类似
```
switch(s.ordinal()) {
case Status.START.ordinal()
}
```
形式,所以实质还是 int 参数类型,感兴趣的可以自己写个使用枚举的 switch 代码然后通过 javap -v 去看下字节码就明白了。
\ No newline at end of file
docs/basics/java-basic/h2-db.md
0 → 100644
浏览文件 @
22b7675f
H2是一个开源的嵌入式(非嵌入式设备)数据库引擎,它是一个用Java开发的类库,可直接嵌入到应用程序中,与应用程序一起打包发布,不受平台限制。
H2与Derby、HSQLDB、MySQL、PostgreSQL等开源数据库相比,H2的优势为:
*
Java开发,不受平台限制;
*
H2只有一个jar包,占用空间小,适合嵌入式数据库;
*
有web控制台,用于管管理数据库。
接下来介绍Spring+Mybatis+H2的数据库访问实践,参考:https://blog.csdn.net/xktxoo/article/details/78014739
添加H2数据库依赖:
```
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.190</version>
</dependency>
```
H2数据库属性文件配置如下,本文采用内存模式访问H2数据库:
```
driver=org.h2.Driver
# 内存模式
url=jdbc:h2:mem:testdb;MODE=MYSQL;DB_CLOSE_DELAY=-1
# 持久化模式
#url= jdbc:h2:tcp://localhost/~/test1;MODE=MYSQL;DB_CLOSE_DELAY=-1
```
H2数据库访问的Spring配置文件为:
```
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns=
"http://www.springframework.org/schema/beans"
xmlns:tx=
"http://www.springframework.org/schema/tx"
xmlns:jdbc=
"http://www.springframework.org/schema/jdbc"
xsi:schemaLocation=
"
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"
>
<!-- 引入属性文件 -->
<bean
id=
"propertyConfigurer"
class=
"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
>
<property
name=
"locations"
>
<list>
<value>
classpath:config.properties
</value>
</list>
</property>
</bean>
<!-- 自动扫描DAO -->
<bean
class=
"org.mybatis.spring.mapper.MapperScannerConfigurer"
>
<property
name=
"basePackage"
value=
"com.xiaofan.test"
/>
</bean>
<!-- 配置Mybatis sqlSessionFactory -->
<bean
id=
"sqlSessionFactory"
class=
"org.mybatis.spring.SqlSessionFactoryBean"
>
<property
name=
"dataSource"
ref=
"dataSource"
/>
<property
name=
"configLocation"
value=
"classpath:mybatis_config.xml"
/>
<property
name=
"mapperLocations"
value=
"classpath:user_mapper.xml"
/>
</bean>
<!-- 配置数据源 -->
<bean
id=
"dataSource"
class=
"org.springframework.jdbc.datasource.DriverManagerDataSource"
>
<property
name=
"driverClassName"
value=
"${driver}"
/>
<property
name=
"url"
value=
"${url}"
/>
<!--<property name="username" value="sa" />-->
<!--<property name="password" value="123" />-->
</bean>
<!-- 初始化数据库 -->
<jdbc:initialize-database
data-source=
"dataSource"
ignore-failures=
"DROPS"
>
<jdbc:script
location=
"classpath:sql/ddl.sql"
/>
<jdbc:script
location=
"classpath:sql/dml.sql"
/>
</jdbc:initialize-database>
<!-- 配置事务管理 -->
<tx:annotation-driven
transaction-manager=
"transactionManager"
proxy-target-class=
"true"
/>
<bean
id=
"transactionManager"
class=
"org.springframework.jdbc.datasource.DataSourceTransactionManager"
>
<property
name=
"dataSource"
ref=
"dataSource"
/>
</bean>
</beans>
```
初始化数据库的DDL语句文件为:
```
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
```
初始化数据库的DML语句文件为:
```
insert into `user` (`id`,`name`,`age`) values (1, 'Jerry', 27);
insert into `user` (`id`,`name`,`age`) values (2, 'Angel', 25);
```
编写测试文件,如下:
```
java
/**
* Created by Jerry on 17/7/30.
*/
@ContextConfiguration
(
locations
=
{
"classpath:config.xml"
})
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
public
class
Test
extends
AbstractJUnit4SpringContextTests
{
@Resource
UserDAO
userDAO
;
@org
.
junit
.
Test
public
void
testInsert
()
{
int
result
=
userDAO
.
insert
(
new
User
(
null
,
"LiLei"
,
27
));
Assert
.
assertTrue
(
result
>
0
);
}
@org
.
junit
.
Test
public
void
testUpdate
()
{
int
result
=
userDAO
.
update
(
new
User
(
2L
,
"Jerry update"
,
28
));
Assert
.
assertTrue
(
result
>
0
);
}
@org
.
junit
.
Test
public
void
testSelect
()
{
User
result
=
userDAO
.
findByName
(
new
User
(
null
,
"Jerry"
,
null
));
Assert
.
assertTrue
(
result
.
getAge
()
!=
null
);
}
@org
.
junit
.
Test
public
void
testDelete
()
{
int
result
=
userDAO
.
delete
(
"Jerry"
);
Assert
.
assertTrue
(
result
>
0
);
}
}
```
\ No newline at end of file
docs/basics/java-basic/iteration-of-collection.md
0 → 100644
浏览文件 @
22b7675f
Collection的迭代有很多种方式:
1、通过普通for循环迭代
2、通过增强for循环迭代
3、使用Iterator迭代
4、使用Stream迭代
```
List<String> list = ImmutableList.of("Hollis", "hollischuang");
// 普通for循环遍历
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//增强for循环遍历
for (String s : list) {
System.out.println(s);
}
//Iterator遍历
Iterator it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
//Stream 遍历
list.forEach(System.out::println);
list.stream().forEach(System.out::println);
```
docs/basics/java-basic/replace-in-string.md
0 → 100644
浏览文件 @
22b7675f
replace、replaceAll和replaceFirst是Java中常用的替换字符的方法,它们的方法定义是:
replace(CharSequence target, CharSequence replacement) ,用replacement替换所有的target,两个参数都是字符串。
replaceAll(String regex, String replacement) ,用replacement替换所有的regex匹配项,regex很明显是个正则表达式,replacement是字符串。
replaceFirst(String regex, String replacement) ,基本和replaceAll相同,区别是只替换第一个匹配项。
可以看到,其中replaceAll以及replaceFirst是和正则表达式有关的,而replace和正则表达式无关。
replaceAll和replaceFirst的区别主要是替换的内容不同,replaceAll是替换所有匹配的字符,而replaceFirst()仅替换第一次出现的字符
### 用法例子
一以下例子参考:http://www.51gjie.com/java/771.html
1.
replaceAll() 替换符合正则的所有文字
```
//文字替换(全部)
Pattern pattern = Pattern.compile("正则表达式");
Matcher matcher = pattern.matcher("正则表达式 Hello World,正则表达式 Hello World");
//替换第一个符合正则的数据
System.out.println(matcher.replaceAll("Java"));
```
2.
replaceFirst() 替换第一个符合正则的数据
```
//文字替换(首次出现字符)
Pattern pattern = Pattern.compile("正则表达式");
Matcher matcher = pattern.matcher("正则表达式 Hello World,正则表达式 Hello World");
//替换第一个符合正则的数据
System.out.println(matcher.replaceFirst("Java"));
```
3.
replaceAll()替换所有html标签
```
//去除html标记
Pattern pattern = Pattern.compile("<.+?>", Pattern.DOTALL);
Matcher matcher = pattern.matcher("<a href=\"index.html\">主页</a>");
String string = matcher.replaceAll("");
System.out.println(string);
```
4.
replaceAll() 替换指定文字
```
//替换指定{}中文字
String str = "Java目前的发展史是由{0}年-{1}年";
String[][] object = {
new String[] {
"\\{0\\}",
"1995"
},
new String[] {
"\\{1\\}",
"2007"
}
};
System.out.println(replace(str, object));
public static String replace(final String sourceString, Object[] object) {
String temp = sourceString;
for (int i = 0; i < object.length; i++) {
String[] result = (String[]) object[i];
Pattern pattern = Pattern.compile(result[0]);
Matcher matcher = pattern.matcher(temp);
temp = matcher.replaceAll(result[1]);
}
return temp;
}
```
5.
replace()替换字符串
```
System.out.println("abac".replace("a", "\\a")); //\ab\ac
```
docs/menu.md
浏览文件 @
22b7675f
...
...
@@ -122,7 +122,7 @@ Gitee Pages 完整阅读:[进入](http://hollischuang.gitee.io/tobetopjavaer)
* [JDK 6和JDK 7中substring的原理及区别](/basics/java-basic/substring.md)
*
replaceFirst、replaceAll、replace区别
*
[replaceFirst、replaceAll、replace区别](/basics/java-basic/replace-in-string.md)
* [String对“+”的重载](/basics/java-basic/string-append.md)
...
...
@@ -184,13 +184,13 @@ Gitee Pages 完整阅读:[进入](http://hollischuang.gitee.io/tobetopjavaer)
* [Java 8中stream相关用法](/basics/java-basic/stream.md)
*
Apache集合处理工具类的使用
*
[Apache集合处理工具类的使用](/basics/java-basic/apache-collections.md)
* 不同版本的JDK中HashMap的实现的区别以及原因
* [Arrays.asList获得的List使用时需要注意什么](/basics/java-basic/Arrays-asList.md)
*
Collection如何迭代
*
[Collection如何迭代](/basics/java-basic/iteration-of-collection.md)
* [Enumeration和Iterator区别](/basics/java-basic/Enumeration-vs-Iterator.md)
...
...
@@ -226,7 +226,7 @@ Gitee Pages 完整阅读:[进入](http://hollischuang.gitee.io/tobetopjavaer)
* [输入流、输出流](/basics/java-basic/input-stream-vs-output-stream.md)
*
字节流和字符流之间的相互转换
*
[字节流和字符流之间的相互转换](/basics/java-basic/convert-bytestream-characterstream.md)
* [同步、异步](/basics/java-basic/synchronized-vs-asynchronization.md)
...
...
@@ -328,7 +328,7 @@ Gitee Pages 完整阅读:[进入](http://hollischuang.gitee.io/tobetopjavaer)
* [mockito](/basics/java-basic/ut-with-mockito.md)
*
内存数据库(h2)
*
[内存数据库(h2)](/basics/java-basic/h2-db.md)
* 正则表达式
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录