Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java-training
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jiewen.li
java-training
Commits
44057bbd
Commit
44057bbd
authored
Nov 01, 2018
by
jiewen.li
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
dfde1296
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
57 deletions
+80
-57
demo1/src/test/java/cn/freemud/demo/CollectionTests.java
+17
-8
demo1/src/test/java/cn/freemud/demo/IOTests.java
+52
-45
demo1/src/test/java/cn/freemud/demo/MapTests.java
+11
-4
No files found.
demo1/src/test/java/cn/freemud/demo/CollectionTests.java
View file @
44057bbd
...
...
@@ -14,16 +14,15 @@ import java.util.*;
public
class
CollectionTests
{
User
zs
=
new
User
(
1L
,
"张三"
);
User
ls
=
new
User
(
2L
,
"李四"
);
User
ww
=
new
User
(
3L
,
"王五"
);
User
zl
=
new
User
(
4L
,
"郑六"
);
@Test
public
void
TestList
(){
public
void
LinkedListTest
(){
//LinkedList
LinkedList
<
User
>
userLinkedList
=
Lists
.
newLinkedList
();
User
zs
=
new
User
(
1L
,
"张三"
);
User
ls
=
new
User
(
2L
,
"李四"
);
User
ww
=
new
User
(
3L
,
"王五"
);
User
zl
=
new
User
(
4L
,
"郑六"
);
// LinkedList 可重复
userLinkedList
.
add
(
zs
);
userLinkedList
.
add
(
zs
);
...
...
@@ -40,6 +39,7 @@ public class CollectionTests {
Assert
.
assertSame
(
userLinkedList
.
get
(
0
),
userLinkedList
.
get
(
1
));
// LinkedList 先后有序保存
userLinkedList
.
removeFirst
();
userLinkedList
.
removeLast
();
...
...
@@ -52,6 +52,10 @@ public class CollectionTests {
ckUsers
.
add
(
zl
);
Assert
.
assertNotEquals
(
JSON
.
toJSONString
(
userLinkedList
),
JSON
.
toJSONString
(
ckUsers
));
}
@Test
public
void
ArrayListTest
(){
//ArrayList
...
...
@@ -63,6 +67,13 @@ public class CollectionTests {
Assert
.
assertSame
(
zs
,
ss
);
}
@Test
public
void
HashSetTest
(){
//HashSet
HashSet
<
Long
>
userHashSet
=
Sets
.
newHashSet
();
...
...
@@ -75,7 +86,5 @@ public class CollectionTests {
System
.
out
.
println
(
JSON
.
toJSONString
(
userHashSet
));
Assert
.
assertEquals
(
JSON
.
toJSONString
(
userHashSet
),
"[1,2]"
);
}
}
demo1/src/test/java/cn/freemud/demo/IOTests.java
View file @
44057bbd
...
...
@@ -21,11 +21,12 @@ import static com.google.common.base.Preconditions.*;
public
class
IOTests
{
String
content
=
"this is test file."
;
String
fileName
=
"D:/java-training/test1.md"
;
@Test
public
void
Test
(){
public
void
IOFileReadWrite
Test
(){
String
content
=
"this is test file."
;
String
fileName
=
"D:/java-training/test1.md"
;
checkNotNull
(
fileName
,
"test fileName must not be null."
);
File
testFile
=
new
File
(
fileName
);
...
...
@@ -70,59 +71,65 @@ public class IOTests {
Assert
.
assertEquals
(
fileInputStreamContent
,
content
);
String
copyFileName
=
"D:/java-training/test1_copy.md"
;
String
copyFileName2
=
"D:/java-training/test1_copy2.md"
;
File
copyFile
=
new
File
(
copyFileName
);
File
copyFile2
=
new
File
(
copyFileName2
);
}
catch
(
IOException
fileIoEx
){
String
ss
=
fileIoEx
.
toString
();
System
.
out
.
print
(
ss
);
}
}
@Test
public
void
IOFileStreamReadWriteTest
(){
File
testFile
=
new
File
(
fileName
);
String
copyFileName
=
"D:/java-training/test1_copy.md"
;
String
copyFileName2
=
"D:/java-training/test1_copy2.md"
;
File
copyFile
=
new
File
(
copyFileName
);
File
copyFile2
=
new
File
(
copyFileName2
);
if
(
copyFile
.
exists
())
copyFile
.
delete
();
if
(
copyFile2
.
exists
())
copyFile2
.
delete
();
FileOutputStream
fileOutputStream
=
null
;
FileOutputStream
fileOutputStream
=
null
;
try
{
Files
.
createParentDirs
(
copyFile
);
fileOutputStream
=
new
FileOutputStream
(
copyFile
);
fileOutputStream
.
write
(
content
.
getBytes
());
fileOutputStream
.
flush
();
}
catch
(
IOException
e
){
e
.
printStackTrace
();
}
finally
{
//关闭文件流
try
{
Files
.
createParentDirs
(
copyFile
);
fileOutputStream
=
new
FileOutputStream
(
copyFile
);
fileOutputStream
.
write
(
content
.
getBytes
());
fileOutputStream
.
flush
();
}
catch
(
IOException
e
){
fileOutputStream
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
//关闭文件流
try
{
fileOutputStream
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
FileOutputStream
fos
=
null
;
FileOutputStream
fos
=
null
;
try
{
FileInputStream
fis
=
new
FileInputStream
(
testFile
);
fos
=
new
FileOutputStream
(
copyFileName2
);
byte
[]
buf
=
new
byte
[
fis
.
available
()];
fis
.
read
(
buf
);
fos
.
write
(
buf
);
fos
.
flush
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
//关闭文件流
try
{
FileInputStream
fis
=
new
FileInputStream
(
testFile
);
fos
=
new
FileOutputStream
(
copyFileName2
);
byte
[]
buf
=
new
byte
[
fis
.
available
()];
fis
.
read
(
buf
);
fos
.
write
(
buf
);
fos
.
flush
();
}
catch
(
Exception
e
)
{
fos
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
//关闭文件流
try
{
fos
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
Assert
.
assertTrue
(
new
File
(
copyFileName
).
exists
());
Assert
.
assertTrue
(
new
File
(
copyFileName2
).
exists
());
}
catch
(
IOException
fileIoEx
){
String
ss
=
fileIoEx
.
toString
();
System
.
out
.
print
(
ss
);
}
Assert
.
assertTrue
(
new
File
(
copyFileName
).
exists
());
Assert
.
assertTrue
(
new
File
(
copyFileName2
).
exists
());
}
}
demo1/src/test/java/cn/freemud/demo/MapTests.java
View file @
44057bbd
...
...
@@ -11,14 +11,16 @@ import java.util.LinkedHashMap;
public
class
MapTests
{
User
user1
=
new
User
(
1L
,
"张三"
);
User
user2
=
new
User
(
2L
,
"李四"
);
User
user3
=
new
User
(
3L
,
"王五"
);
@Test
public
void
Test
()
{
public
void
HashMap
Test
()
{
//HashMap 无序排列 效率较高
HashMap
<
String
,
User
>
userHashMap
=
Maps
.
newHashMap
();
User
user1
=
new
User
(
1L
,
"张三"
);
User
user2
=
new
User
(
2L
,
"李四"
);
User
user3
=
new
User
(
3L
,
"王五"
);
userHashMap
.
put
(
"xxx"
,
user1
);
userHashMap
.
put
(
"aaa"
,
user2
);
userHashMap
.
put
(
"sss"
,
user3
);
...
...
@@ -30,6 +32,11 @@ public class MapTests {
System
.
out
.
println
(
userHashMap
);
}
@Test
public
void
LinkedHashMapTest
(){
//LinkedHashMap 自动排序 效率较低
LinkedHashMap
<
String
,
User
>
userLinkedHashMap
=
Maps
.
newLinkedHashMap
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment