集合、IO操作、列舉、多執行緒
阿新 • • 發佈:2018-12-30
一、集合
/** * lambda過濾 */ @Test public void testLambda(){ Person person1 = new Person(); person1.setName("aaa"); person1.setAge(10); Person person2 = new Person(); person2.setName("bbb"); person2.setAge(20); Person person3 = new Person(); person3.setName("ccc"); person3.setAge(30); List<Person> personList = new ArrayList<>(); personList.add(person1); personList.add(person2); personList.add(person3); personList.stream().filter(p -> p.getAge() < 20) .forEach(p -> { System.out.println(p.getName()); }); }
/** * lambda排序 */ @Test public void testLambda1(){ Person person1 = new Person(); person1.setAge(22); person1.setName("aaa"); Person person2 = new Person(); person2.setAge(22); person2.setName("bbb"); Person person3 = new Person(); person3.setAge(23); person3.setName("ccc"); List<Person> personList = new ArrayList<>(); personList.add(person1); personList.add(person2); personList.add(person3); personList.stream().sorted(Comparator.comparing(Person::getAge)) .forEach(person -> { System.out.println(person.getName()); }); }
@Test public void testGetNameByCourse1(){ String name =this.getNameByCourse1("java"); System.out.println(name); Assert.assertEquals("老師名字的校驗","xiaoli",name); } public String getNameByCourse1(String course){ Teacher teacher1 = new Teacher(); teacher1.setName("xiaoli"); teacher1.setCourse("java"); teacher1.setSex("男"); Teacher teacher2 = new Teacher(); teacher2.setName("xiaozhang"); teacher2.setCourse("selenium"); teacher2.setSex("女"); Teacher teacher3 = new Teacher(); teacher3.setName("xiaowang"); teacher3.setCourse("appium"); teacher3.setSex("女"); Map<String,Teacher> teacherMap1 = new HashMap<>(); teacherMap1.put("xiaoli",teacher1); teacherMap1.put("xiaowanzi",teacher2); teacherMap1.put("xiaohu",teacher3); String[] name= new String [1]; teacherMap1.forEach((k,v) -> { if(v.getCourse().equals(course)){ name[0]=v.getName(); } }); return name[0]; }
二、IO操作
public String getFileName(int index) throws IOException {
File file = new File("d:\\homework");
File file1 = new File(file.getAbsoluteFile(),"file1.txt");
File file2 = new File(file.getAbsoluteFile(),"file2.txt");
File file3 = new File(file.getAbsoluteFile(),"file3.txt");
file1.createNewFile();
file2.createNewFile();
file3.createNewFile();
File[] files = file.listFiles();
return files[index-1].getName();
}
@Test
public void mapTest1() throws IOException {
String fileName= this.getFileName(2);
Assert.assertEquals("驗證檔名","file2.txt",fileName);
}
@Test
public void fileIo() throws IOException {
File file = new File("d:\\homework2");
file.mkdir();
File file1 = new File(file.getAbsoluteFile(),"temp1.txt");
file1.createNewFile();
if(file.exists()){
System.out.println("建立成功資料夾temp1.txt");
}
FileUtils.write(new File("d:\\homework2\\temp1.txt"),"是非得失",true);
FileUtils.write(new File("d:\\homework2\\temp1.txt"),"好好學習,天天向上",true);
FileUtils.copyFile(new File("d:\\homework2\\temp1.txt"),new File("d:\\homework2\\temp2.txt"));
}
三、列舉類
public enum SexTypeEnum {
Man("男",1),
Women("女",2);
public String key;
public int value;
SexTypeEnum(String key, int value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
@Test
public void sexTest(){
int sexType = 1;
if(sexType== SexTypeEnum.Man.value){
System.out.println("男");
}else if(sexType ==SexTypeEnum.Women.value){
System.out.println("女");
}
}