一、集合
/**
* 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());
});
}
一般都用实现Runnable接口比继承Thread类所具有的优势
1、适合多个相同的程序代码的线程去处理同一个资源
2、可以避免java中单继承的限制,继承了Thread类 就不能继承别的类了
3、增加程序的健壮性,代码可以被多个线程共享,代码和数据独立
线程的生命周期,新建、就绪状态、运行状态、死亡状态。阻塞状态
线程控制的基本方法
/**
* 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("女");
}
}
四、多线程,两种原生的写法,但是实际中用的一般是线程池
一种是实现Runnable接口,重写run()方法
public class MyRunnable implements Runnable {
@Override
public void run(){
PayProcessor payProcessor = new PayProcessor();
String userName = "xiaowang";
payProcessor.pay(userName);
}
public static void main(String[] args){
MyRunnable myRunnable =new MyRunnable();
Thread thread = new Thread(myRunnable);
Thread thread1 = new Thread(myRunnable);
thread.start();
thread1.start();
System.out.println("我是主线程");
}
}
一种是集成Thread 类,重写run()方法
public class MyThread extends Thread {
@Override
public void run(){
System.out.println("线程");
}
public static void main(String[] args){
MyThread myThread = new MyThread();
myThread.start();
}
}