Java项目学习Day4

知识点

Java8 Lambda

Lambda 表达式,也可称为闭包,它是推动 Java 8 发布的最重要新特性。
Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中)。
使用 Lambda 表达式可以使代码变的更加简洁紧凑。
格式:

(parameters) -> expression
或
(parameters) ->{ statements; }
 return distinctProjects.stream().map(projectVO -> {
            ProjectInfoResponse projectInfoResponse = new ProjectInfoResponse();
            projectInfoResponse.setDescription(projectVO.getDescription());
            projectInfoResponse.setLogoColor(projectVO.getLogoColor());
            projectInfoResponse.setName(projectVO.getName());
            projectInfoResponse.setUserId(userId + "");
            projectInfoResponse.setRole(AuthRole.PROJECT_ADMIN);
            projectInfoResponse.setUuid(projectVO.getUuid());
            return projectInfoResponse;
        }).collect(Collectors.toList());

更复杂的例子:

@Override
    public List<XlyProjectComponentBean> queryProjectComponentBean(String company, String project) {
        List<Application> list1 = applicationService.queryApplications(company, true, false);
        List<Application> list2 = applicationService.queryApplications(company, false, true);
        List<Privilege> privilegeList = queryProjectPrivilegeComponent(company, project);
        return list1.stream().map(app -> {
            XlyProjectComponentBean bean = new XlyProjectComponentBean();
            bean.setCompanyUuid(company);
            bean.setProjectUuid(project);
            bean.setId(app.getId());
            bean.setName(app.getName());
            bean.setNum(app.getNum());
            // CHECKSTYLE:OFF
            bean.setLevel(1);
            bean.setKey(app.getCode());
            privilegeList.forEach(p -> {
                if (p.getDeleteStatus() == 0 && p.getPermissionAppId().equals(app.getId())) {
                    bean.setIschecked(true);
                }
            });
            // 组装二级组件
            List<XlyProjectComponentBean> childrenBeanList = new ArrayList<>();
            list2.forEach(appc -> {
                if (appc.getParentId().equals(app.getId())) {
                    XlyProjectComponentBean beanChild = new XlyProjectComponentBean();
                    beanChild.setCompanyUuid(company);
                    beanChild.setProjectUuid(project);
                    beanChild.setId(appc.getId());
                    beanChild.setName(appc.getName());
                    // CHECKSTYLE:OFF
                    beanChild.setLevel(2);
                    beanChild.setNum(appc.getNum());
                    beanChild.setKey(appc.getCode());
                    privilegeList.forEach(p -> {
                        if (p.getDeleteStatus() == 0 && p.getPermissionAppId().equals(appc.getId())) {
                            beanChild.setIschecked(true);
                        }
                    });
                    childrenBeanList.add(beanChild);
                }
            });
            bean.setChildren(childrenBeanList);
            return bean;
        }).sorted(Comparator.comparing(XlyProjectComponentBean::getNum)).collect(Collectors.toList());
    }

Thread

  1. What are Java Threads?
  • Facility to allow multiple activities within a single process
    Referred as lightweight process

  • A thread is a series of executed statements

  • Each thread has its own program counter, stack and local
    variables

  • A thread is a nested sequence of method callsIts shares
    memory, files and per-process state

  1. Whats the need of a thread or why we use Threads?
  • To perform asynchronous or background processing
  • Increases the responsiveness of GUI applications
  • Take advantage of multiprocessor systems
  • Simplify program logic when there are multiple independent entities
  1. What happens when a thread is invoked?
    When a thread is invoked, there will be two paths of execution. One path will execute the thread and the other path will follow the statement after the thread invocation. There will be a separate stack and memory space for each thread.

MultiThread

Let’s summarize the discussion in points:

  1. The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each such part of a program called thread.2. Threads are lightweight sub-processes, they share the common memory space. In Multithreaded environment, programs that are benefited from multithreading, utilize the maximum CPU time so that the idle time can be kept to minimum.3. A thread can be in one of the following states:
    NEW – A thread that has not yet started is in this state.
    RUNNABLE – A thread executing in the Java virtual machine is in this state.
    BLOCKED – A thread that is blocked waiting for a monitor lock is in this state.
    WAITING – A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
    TIMED_WAITING – A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
    TERMINATED – A thread that has exited is in this state.
    A thread can be in only one state at a given point in time.

泛型嵌套

 @GetMapping("/code/application/{application}")
 @ApiOperation(value = "code全局设置权限列表", notes = "code全局设置权限列表") 
 public ResponseEntity<AdminResponse<PageBean<PermissionApplicationResponse>>> queryCodeList(
            @PathVariable String company,
            @PathVariable(value = "application") String application,
            @PathVariable(value = "project") String project,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return ResponseEntity.status(HttpStatus.OK)
                .body(AdminResponse.result(GlobalConstant.SUCCESS, "授权列表查询成功!",
                        permissionService.queryPermissionList2(company, project, application, page, pageSize, true)));
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值