Commit 44057bbd by jiewen.li

update

parent dfde1296
...@@ -14,16 +14,15 @@ import java.util.*; ...@@ -14,16 +14,15 @@ import java.util.*;
public class CollectionTests { public class CollectionTests {
User zs = new User(1L,"张三");
User ls = new User(2L,"李四");
User ww = new User(3L,"王五");
User zl = new User(4L,"郑六");
@Test @Test
public void TestList(){ public void LinkedListTest(){
//LinkedList //LinkedList
LinkedList<User> userLinkedList = Lists.newLinkedList(); 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 可重复 // LinkedList 可重复
userLinkedList.add(zs); userLinkedList.add(zs);
userLinkedList.add(zs); userLinkedList.add(zs);
...@@ -40,6 +39,7 @@ public class CollectionTests { ...@@ -40,6 +39,7 @@ public class CollectionTests {
Assert.assertSame(userLinkedList.get(0),userLinkedList.get(1)); Assert.assertSame(userLinkedList.get(0),userLinkedList.get(1));
// LinkedList 先后有序保存 // LinkedList 先后有序保存
userLinkedList.removeFirst(); userLinkedList.removeFirst();
userLinkedList.removeLast(); userLinkedList.removeLast();
...@@ -52,6 +52,10 @@ public class CollectionTests { ...@@ -52,6 +52,10 @@ public class CollectionTests {
ckUsers.add(zl); ckUsers.add(zl);
Assert.assertNotEquals(JSON.toJSONString(userLinkedList),JSON.toJSONString(ckUsers)); Assert.assertNotEquals(JSON.toJSONString(userLinkedList),JSON.toJSONString(ckUsers));
}
@Test
public void ArrayListTest(){
//ArrayList //ArrayList
...@@ -63,6 +67,13 @@ public class CollectionTests { ...@@ -63,6 +67,13 @@ public class CollectionTests {
Assert.assertSame(zs,ss); Assert.assertSame(zs,ss);
}
@Test
public void HashSetTest(){
//HashSet //HashSet
HashSet<Long> userHashSet = Sets.newHashSet(); HashSet<Long> userHashSet = Sets.newHashSet();
...@@ -75,7 +86,5 @@ public class CollectionTests { ...@@ -75,7 +86,5 @@ public class CollectionTests {
System.out.println(JSON.toJSONString(userHashSet)); System.out.println(JSON.toJSONString(userHashSet));
Assert.assertEquals(JSON.toJSONString(userHashSet),"[1,2]"); Assert.assertEquals(JSON.toJSONString(userHashSet),"[1,2]");
} }
} }
...@@ -21,11 +21,12 @@ import static com.google.common.base.Preconditions.*; ...@@ -21,11 +21,12 @@ import static com.google.common.base.Preconditions.*;
public class IOTests { public class IOTests {
String content = "this is test file.";
String fileName = "D:/java-training/test1.md";
@Test @Test
public void Test(){ public void IOFileReadWriteTest(){
String content = "this is test file.";
String fileName = "D:/java-training/test1.md";
checkNotNull(fileName,"test fileName must not be null."); checkNotNull(fileName,"test fileName must not be null.");
File testFile = new File(fileName); File testFile = new File(fileName);
...@@ -70,59 +71,65 @@ public class IOTests { ...@@ -70,59 +71,65 @@ public class IOTests {
Assert.assertEquals(fileInputStreamContent,content); Assert.assertEquals(fileInputStreamContent,content);
String copyFileName = "D:/java-training/test1_copy.md"; }
String copyFileName2 = "D:/java-training/test1_copy2.md"; catch (IOException fileIoEx){
File copyFile = new File(copyFileName); String ss = fileIoEx.toString();
File copyFile2 = new File(copyFileName2); 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(); copyFile.delete();
if(copyFile2.exists())
copyFile2.delete(); 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 { try {
Files.createParentDirs(copyFile); fileOutputStream.close();
fileOutputStream = new FileOutputStream(copyFile); } catch (IOException e) {
fileOutputStream.write(content.getBytes());
fileOutputStream.flush();
}catch (IOException e){
e.printStackTrace(); 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 { try {
FileInputStream fis = new FileInputStream(testFile); fos.close();
fos = new FileOutputStream(copyFileName2); } catch (IOException e) {
byte[] buf = new byte[fis.available()];
fis.read(buf);
fos.write(buf);
fos.flush();
} catch (Exception e) {
e.printStackTrace(); 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());
} }
} }
...@@ -11,14 +11,16 @@ import java.util.LinkedHashMap; ...@@ -11,14 +11,16 @@ import java.util.LinkedHashMap;
public class MapTests { public class MapTests {
User user1 = new User(1L,"张三");
User user2 = new User(2L,"李四");
User user3 = new User(3L,"王五");
@Test @Test
public void Test() { public void HashMapTest() {
//HashMap 无序排列 效率较高 //HashMap 无序排列 效率较高
HashMap<String,User> userHashMap = Maps.newHashMap(); 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("xxx",user1);
userHashMap.put("aaa",user2); userHashMap.put("aaa",user2);
userHashMap.put("sss",user3); userHashMap.put("sss",user3);
...@@ -30,6 +32,11 @@ public class MapTests { ...@@ -30,6 +32,11 @@ public class MapTests {
System.out.println(userHashMap); System.out.println(userHashMap);
}
@Test
public void LinkedHashMapTest(){
//LinkedHashMap 自动排序 效率较低 //LinkedHashMap 自动排序 效率较低
LinkedHashMap<String,User> userLinkedHashMap = Maps.newLinkedHashMap(); LinkedHashMap<String,User> userLinkedHashMap = Maps.newLinkedHashMap();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment