Commit 44057bbd by jiewen.li

update

parent dfde1296
......@@ -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]");
}
}
......@@ -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 IOFileReadWriteTest(){
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());
}
}
......@@ -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 HashMapTest() {
//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();
......
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