高效 告别996,开启java高效编程之门 4-2垃圾回收与物理资源释放

1    重点:

1.1  垃圾回收的特点

1.2  何种资源需要手动回收

1.3  物理资源不回收的后果

 
2     垃圾回收的特点

排序demo需求:

股票撮合交易,优先按照价格高的排序,在按照时间早的,再按照交易量大的,在按照先机构后个人的方式

测试类:

package com.imooc.zhangxiaoxi.stream.cases;

import com.alibaba.fastjson.JSON;
import com.sun.tools.javac.util.StringUtils; import lombok.AllArgsConstructor; import lombok.Data; import org.junit.Before; import org.junit.Test; import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; /** * 类名称:CaseFive * ******************************** * <p> * 类描述:案例五 * 重点讲解:sort和compare的使用方式 * * @author zhangxiaoxi * @date 下午10:32 */ public class CaseFive { /** * 交易实体模型 */ /*@Data @AllArgsConstructor*/ class Trade { // 下单价格 private BigDecimal price; // 下单时间 private LocalDateTime time; // 下单量 private Integer count; // 下单类型:机构 / 个人 private String type; public Trade(BigDecimal price, LocalDateTime time, Integer count, String type) { this.price = price; this.time = time; this.count = count; this.type = type; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } public LocalDateTime getTime() { return time; } public void setTime(LocalDateTime time) { this.time = time; } public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } public String getType() { return type; } public void setType(String type) { this.type = type; } } /** * 一段时间内的交易申请 */ List<Trade> trades; @Before public void init() { trades = new ArrayList<>(); trades.add(new Trade(new BigDecimal(100), // 在当前时间的基础上添加 1 秒 LocalDateTime.now().plusSeconds(1), 500, "机构")); trades.add(new Trade(new BigDecimal(101), LocalDateTime.now().plusSeconds(2), 1, "个人")); trades.add(new Trade(new BigDecimal(101), LocalDateTime.now().plusSeconds(1), 1, "个人")); trades.add(new Trade(new BigDecimal(100), LocalDateTime.now().plusSeconds(1), 500, "个人")); trades.add(new Trade(new BigDecimal(100), LocalDateTime.now().plusSeconds(0), 2, "个人")); trades.add(new Trade(new BigDecimal(100), LocalDateTime.now().plusSeconds(0), 100, "机构")); } //以下代码是我模仿的代码 /** * 1 按照价格排序 * 2 按照时间排序 * 3 按照交易量排序 * 4 按照类型排序 */ @Test public void sortTestByMe(){ List<Trade> list = trades.stream().sorted( Comparator //1 按照价格排序  .comparing(Trade::getPrice,Comparator.reverseOrder()) //2 按照时间排序  .thenComparing(Trade::getTime) //3 按照交易量排序  .thenComparing(Trade::getCount,Comparator.reverseOrder()) //4 按照类型排序 .thenComparing(Trade::getType,(type1,type2)->{ if("机构".equals(type1)&&"个人".equals(type2)){ return -1; }else if("个人".equals(type1)&&"机构".equals(type2)){ return 1; }else{ return 0; } }) ).collect(Collectors.toList()); System.out.println(JSON.toJSONString(list,true)); } //以下代码是教学代码 /*@Test public void sortTrade() { System.out.println("排序前数据~~~\n" + JSON.toJSONString(trades, true)); List<Trade> sorted = trades.stream() .sorted( Comparator // 首先按照价格排序 .comparing( Trade::getPrice, // TODO 进行排序调整,将自然排序翻转 Comparator.reverseOrder()) // 时间先后进行排序,自然排序 .thenComparing(Trade::getTime) // 交易量排序,自然排序翻转 .thenComparing( Trade::getCount, Comparator.reverseOrder()) // 自定义排序规则 .thenComparing( // 要排序的字段值 Trade::getType, // 自定义排序规则 (type1, type2) -> { if ("机构".equals(type1) && "个人".equals(type2)) { // -1:type1在先, type2在后 return -1; } else if ("个人".equals(type1) && "机构".equals(type2)) { return 1; } else { return 0; } })) .collect(Collectors.toList()); System.out.println("排序后结果~~~\n" + JSON.toJSONString(sorted, true)); }*/ }

打印日志:

[
    {
        "count":1,
        "price":101,
        "time":"2020-05-21T18:14:50.341655300",
        "type":"个人"
    },
    {
        "count":1, "price":101, "time":"2020-05-21T18:14:51.341655300", "type":"个人" }, { "count":100, "price":100, "time":"2020-05-21T18:14:49.341655300", "type":"机构" }, { "count":2, "price":100, "time":"2020-05-21T18:14:49.341655300", "type":"个人" }, { "count":500, "price":100, "time":"2020-05-21T18:14:50.340656800", "type":"机构" }, { "count":500, "price":100, "time":"2020-05-21T18:14:50.341655300", "type":"个人" } ] Process finished with exit code 0

猜你喜欢

转载自www.cnblogs.com/1446358788-qq/p/12933905.html