🔥
Caster 開發日誌
  • Java
    • JVM Performance Tool
      • Java Debug Wire Protocol (JDWP) 的詳細介紹
      • JConsole 詳細介紹
    • Spring Boot
      • Spring Security
      • Spring Boot Admin
      • Spring Event
      • Spring AOP
      • Spring Boot JUnit 5
      • Apache Dubbo
    • Reflect 應用
    • ELK + F 建構
    • Socket.IO
    • OCR - 光學字元辨識
    • 讀取JAR resource文件
    • LocalTime & MySQL時間精度
    • Gradle multi module
    • MyBatis-Plus
    • Java Date operation
    • Java IP to Long
    • Apache Commons lang3 應用
      • Function 應用
    • Cloud Platform
      • Amazon S3
        • SDK V1
          • Bucket
        • SDK V2
          • Bucket
      • Google Cloud Platform
      • Azure Cloud
        • Storage
      • OVHcloud
        • Config
    • SSL/TLS工具
    • Util 工具
      • Jackson Json工具
      • Charles應用
      • JMeter – Performing Distributed Load Testing with Docker
    • Redis
      • Stream
      • Redisson 分布式鎖機制
      • Create Redis Cluster Using Docker
      • List Operations
    • Java 8
      • method & constructor Reference
      • CompletableFuture
      • FunctionInterface
      • Stream 應用
      • 繁簡轉換 - 簡易調整
    • MySQL
      • 建立測試用 流水號Table
      • SQL 效能調校 - Explain
      • SQL 效能調校 - Partition
      • 排程 - Event
    • Apache ShardingSphere
  • Kubernetes
    • 初入江湖(K8S)
    • 零中斷服務滾動更新
    • Kubernetes DNS
    • Ingress & Ingress Controller 教學
    • Ingress TLS Easy setup
  • 指令集
  • Telegram
  • SourceTree
    • 踩坑紀錄(ㄧ) - Git Flow
    • 踩坑紀錄(二) - 修改檔名
  • 專案統計
    • Robot
    • Recharge
  • GitHub
    • Actions
  • GitLab
    • 介紹 GitLab
    • 使用 Docker 自架 GitLab
    • 簡介 GitLab CI/CD
      • GitLab Runner 詳細介紹與設定方式
Powered by GitBook
On this page
  1. Java
  2. Java 8

Stream 應用

開發應用日誌

Object list stream cast class

public static void main(String[] args) throws JsonProcessingException, ParseException {	
	List<Object> list = new ArrayList<>();
	for(long i = 0; i < 100; i++){
		list.add(new ViewCounts().setMemberId(i));
	}

	calculateProcess("has Filter >", () -> {
		list.stream()
		.filter(ViewCounts.class::isInstance)
		.map(ViewCounts.class::cast)
		.forEach(o -> System.out.println("2 > memberId:" + o.getMemberId()));
	});
	// has Filter > duration: 19(ms)

	calculateProcess("no Filter >", () -> {
		list.stream()
		.map(o -> (ViewCounts) o)
		.forEach(o -> System.out.println("memberId:" + o.getMemberId()));
	});
	// no Filter > duration: 17(ms)
}

interface calculateProcess {
	void calculate();
}

public static void calculateProcess(String methodName, calculateProcess process) {
	long start = System.nanoTime();
	if (Objects.nonNull(process))
		process.calculate();
	long end = System.nanoTime();
	long duration = TimeUnit.NANOSECONDS.toMillis(end - start);
	System.out.println(methodName + " duration: " + duration + "(ms)");
}

兩種方法經過效能測試幾乎一樣,但第一種寫法比較穩健,可以避免ClassCastException的問題發生。

Summing Numbers - 統計數值

List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
Integer sum = integers.stream()
  .mapToInt(Integer::intValue)
  .sum();
  
System.out.println(sum);
// console log :15

// BigDecimal 加總辦法
List<BigDecimal> list = List.of(BigDecimal.ONE, BigDecimal.ZERO, BigDecimal.TEN, 
                                new BigDecimal(100), new BigDecimal(200));
BigDecimal sum = list.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
System.out.println(sum);
// console log :311

LongStream - 簡易迴圈操作

String str = LongStream.rangeClosed(0, 100)
                                .mapToObj(o -> String.valueOf(o))
                                .collect(Collectors.joining(",", "[", "]"));

// console log:[0,1,2,3,4,5,.....,97,98,99,100]
// java.util.stream package 底下還有一些實用的物件可以使用,大大減少測試及開發上的時間。

如果我有看到很棒的寫法,會持續更新....

PreviousFunctionInterfaceNext繁簡轉換 - 簡易調整

Last updated 1 year ago