首頁  >  文章  >  Java串流傳輸數組列表並與先前的記錄進行比較

Java串流傳輸數組列表並與先前的記錄進行比較

WBOY
WBOY轉載
2024-02-22 14:00:10632瀏覽

php小編魚仔為您帶來本期Java問答,探討Java中如何使用串流處理數組列表,並與先前的記錄進行比較。透過本文,您將了解如何利用Java的串流處理功能,有效率地對陣列清單進行操作,並實現與先前記錄的比較。讓我們一起深入探討這個主題,提升對Java程式設計的理解與技能水準。

問題內容

我有一個簡單的 recorda 類,它包含一個 id 和一個 int 值。多個「已排序」recorda 元素儲存在清單中。

我想遍歷清單並將當前元素與前一個元素進行比較,並找到它們值的差異。

程式碼:

import java.util.*;  

class RecordA{
    Integer id;
    Integer value;
    
    RecordA(Integer id, Integer value) {
        this.id = id;
        this.value = value;
    }
    
    Integer getId() { return id;}
    Integer getValue() { return value;}
}

class RecordB {
    Integer id;
    Integer value;
    Integer diff;
    
    RecordB(Integer id, Integer value, Integer diff) {
        this.id = id;
        this.value = value;
        this.diff = diff;
    }
    
    Integer getId() { return id;}
    Integer getValue() { return value;}
    Integer getDiff() { return diff;}
}

class HelloWorld {
    public static void main(String[] args) {
        
        
        List<RecordA> listA = new ArrayList<>();
        RecordA recordA1 = new RecordA(1,10);
        listA.add(recordA1);
        RecordA recordA2 = new RecordA(2,15);
        listA.add(recordA2);
        RecordA recordA3 = new RecordA(3,25);
        listA.add(recordA3);
        RecordA recordA4 = new RecordA(4,30);
        listA.add(recordA4);
        
        System.out.println(listA.size());
    }
}

我想使用流(如果可能)將當前 recorda.value 與先前的 recorda.value 進行比較,將結果映射到具有相同 id 和值的 recordb 中,但存儲當前-上一個。

最後,recordb 清單將包含

  • 1, 10, 0 //(10-0)
  • 2, 15, 5 //(15-10)
  • 3, 25, 10 //25-15
  • 4, 30, 5 //30-25

我想避免類別 for 迴圈和 previous_val 變數。有什麼想法如何使用流來做到這一點嗎?

解決方法

您可以使用 intstream

#
intstream.range(0, lista.size())
    .map(index -> 
        new recordb(lista.get(index).getid(), lista.get(index).getvalue(),  lista.get(index).getvalue() - (index > 0 ? lista.get(index - 1).getvalue() : 0))
    )
    .collect(collectors.tolist())

這是一種有點不直觀的方法,我實際上必須檢查一下。
StackOverflow – Java Stream Using Previous Element in Foreach Lambda#。

通常,串流的使用是聚合一組值,而不一定是比較和對比它們。
Lesson: Aggregate Operations (The Java™ Tutorials > Collections)

這是一個使用 Collector 類別和 Collector#of 方法的範例。

本質上,在收集過程中,您可以從已收集的內容中檢索前一個元素。

對於 BiConsumer 參數,a 是迄今為止收集的元素。

list<recordb> l
    = lista.stream()
           .collect(
               collector.<recorda, list<recordb>, list<recordb>>of(
                   arraylist::new,
                   (a, b) -> {
                       if (a.isempty()) a.add(new recordb(b.id, b.value, 0));
                       else {
                           recordb x = a.get(a.size() - 1);
                           a.add(new recordb(b.id, b.value, b.value - x.value));
                       }
                   },
                   (a, b) -> {
                       a.addall(b);
                       return a;
                   },
                   x -> x));

輸出

1, 10, 0
2, 15, 5
3, 25, 10
4, 30, 5

最後一點,您可能希望擺脫 recordb 類,而只使用 Map

Map<RecordA, Integer> m = new LinkedHashMap<>();
RecordA a, b;
m.put(a = listA.get(0), 0);
for (int i = 1, n = listA.size(); i < n; i++)
    m.put(b = listA.get(i), -a.value + (a = b).value);

以上是Java串流傳輸數組列表並與先前的記錄進行比較的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除