我有包含以下資訊的串列。
public class PriceDetails {
public String materialId;
public String price;
public String priceEndDate;
}
串列資料如下:
List<PriceDetails> priceList = new ArrayList<>();
priceList1 [priceEndDate=2022-04-29 00:00:00.000000, price=1.59, materialId=10054700],
priceList2 [priceEndDate=2022-03-31 00:00:00.000000, price=1.64, materialId=10054700],
priceList3 [priceEndDate=2022-05-26 00:00:00.000000, price=3.1, materialId=10063200]]
我想用上面的資訊創建另一個串列。
正如我們所見,priceList1 和 priceList2 的 materialId 相同。所以我需要選擇 priceEndDate 在前兩個日期中最新的那個。所以我的結果串列應該如下,
priceList1 [priceEndDate=2022-04-29 00:00:00.000000, price=1.59, materialId=10054700],
priceList3 [priceEndDate=2022-05-26 00:00:00.000000, price=3.1, materialId=10063200]]
到目前為止我試過這個:
for(PriceDetails unit :priceList) {
if(map.containsKey(unit.getmaterialId())) {
map.put(unit.getmaterialId(), <notgettingwhattoputhere>));
}else {
}
}
請幫助實作相同的目標。
uj5u.com熱心網友回復:
這可能是您問題的答案。但盡量避免將 price 和 priceEndDate 作為字串。
static class PriceDetails {
public String materialId;
public String price;
public String priceEndDate;
public PriceDetails(String materialId, String price, String priceEndDate) {
super();
this.materialId = materialId;
this.price = price;
this.priceEndDate = priceEndDate;
}
public String toString() {
return String.format("[materialId: %s, price: %s, priceEndDate:%s]", this.materialId, this.price, this.priceEndDate);
}
}
public static void filter() throws ParseException {
List<PriceDetails> priceList = new ArrayList<>();
PriceDetails priceList1 = new PriceDetails("10054700", "1.59", "2022-04-29 00:00:00.000000");
PriceDetails priceList2 = new PriceDetails("10054700", "1.64", "2022-03-31 00:00:00.000000");
PriceDetails priceList3 = new PriceDetails("10063200", "3.1", "2022-05-26 00:00:00.000000");
priceList.add(priceList1);
priceList.add(priceList2);
priceList.add(priceList3);
Map<String, PriceDetails> map = new HashMap<>();
for(PriceDetails unit :priceList) {
if(map.containsKey(unit.materialId)) {
if((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(unit.priceEndDate)).after((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(map.get(unit.materialId).priceEndDate)))) {
map.put(unit.materialId, unit);
}
} else {
map.put(unit.materialId, unit);
}
}
System.out.println(map.values());
}
輸出:
[[materialId: 10054700, price: 1.59, priceEndDate:2022-04-29 00:00:00.000000], [materialId: 10063200, price: 3.1, priceEndDate:2022-05-26 00:00:00.000000]]
uj5u.com熱心網友回復:
可以收集串列中的資料并獲取最新價格,然后values()從獲得的地圖中檢索價格,前提是PriceDetails該類具有與其成員欄位相關的 getter:
- 使用以下收集器:
Collectors.groupingBy按 id 對價格詳細資訊進行分組Collectors.maxBy在適當的吸氣劑的幫助下找到最新的價格細節Comparator.comparing(這里是價格結束日期)Collectors.collectingAndThen將Optional上一步回傳的轉換為適當的PriceDetail:
List<PriceDetails> latest = new ArrayList<>(
data.stream()
.collect(Collectors.groupingBy(
PriceDetails::getMaterialId,
Collectors.collectingAndThen(
Collectors.maxBy(
Comparator.comparing(PriceDetails::getPriceEndDate)
),
Optional::get
)
))
.values()
);
- 使用
Collectors.toMap與合并功能使用BinaryOperator::maxBy:
List<PriceDetails> latest = new ArrayList<>(
data.stream()
.collect(Collectors.toMap(
PriceDetails::getMaterialId,
price -> price,
BinaryOperator.maxBy(
Comparator.comparing(PriceDetails::getPriceEndDate)
)
))
.values());
如果沒有 Stream API,可以使用以下函式從 priceId 映射到 price 類似地檢索縮減串列Map::merge:
Map<String, PriceDetails> map = new LinkedHashMap<>();
for (PriceDetails pd : data) {
map.merge(pd.getMaterialId(), pd, BinaryOperator.maxBy(
Comparator.comparing(PriceDetails::getPriceEndDate)
));
}
List<PriceDetails> latest2 = new ArrayList<>(map.values());
uj5u.com熱心網友回復:
修改了PriceDetails類,使其具有建構式和toString()方法:
public class PriceDetails {
public String materialId;
public String price;
public String priceEndDate;
// Constructor:
public PriceDetails(String materialId, String price, String priceEndDate) {
this.materialId = materialId;
this.price = price;
this.priceEndDate = priceEndDate;
}
@Override
public String toString() {
return "priceEndDate = " priceEndDate ", price = " price ", materialId = " materialId;
}
}
以下方法決議物件詳細資訊以便與日期進行一些比較。日期轉換為java.time.LocalDate然后使用LocalDate#isBefore()和LocalDate#isAfter()方法。閱讀代碼中的注釋:
public List<PriceDetails> removeLesserDateDuplicates(List<PriceDetails> priceList) {
List<PriceDetails> wrkList = new ArrayList<>();
wrkList.addAll(priceList);
// Current list Element loop...
for (int i = 0; i < wrkList.size(); i ) {
// Get current element priceEndDate (without the time component).
String iEndDate = wrkList.get(i).priceEndDate.split("\\s ")[0];
// Get current element materialId.
String iID = wrkList.get(i).materialId;
// List element comparison iterator loop...
for (int j = 0; j < wrkList.size(); j ) {
// Get iterator element priceEndDate (without the time component).
String jEndDate = wrkList.get(j).priceEndDate.split("\\s ")[0];
// Get current element materialId.
String jID = wrkList.get(j).materialId;
/* Is the Current ID a duplicate of another List element ID
and are the dates Not Equal.... */
if (i != j && iID.equals(jID) && !iEndDate.equals(jEndDate)) {
// Yes....
// Convert the current element Date to LocalDate.
java.time.LocalDate iDate = java.time.LocalDate.parse(iEndDate);
// Convert the iterated element Date to LocalDate.
java.time.LocalDate jDate = java.time.LocalDate.parse(jEndDate);
/* Is the current element Date before the iterated element Date?
If so, remove the current element record from the List. */
if (iDate.isBefore(jDate)) {
wrkList.remove(i);
i--; // Reduce iterator loop increment by 1.
}
/* Otherwise, is the current element Date after the iterated element Date?
If so, remove the iterated element record from the List. */
else if (iDate.isAfter(jDate)) {
wrkList.remove(j);
j--; // Reduce current element loop increment by 1.
}
}
}
}
return wrkList;
}
要使用此方法,您可能會執行以下操作:
/* Fill the priceList List with PriceDetails instances using
its constructor (see the `PriceDetails` class). */
List<PriceDetails> priceList = new ArrayList<>();
priceList.add(new PriceDetails("10054700", "1.59", "2022-04-29 00:00:00.000000"));
priceList.add(new PriceDetails("10054700", "1.64", "2022-03-31 00:00:00.000000"));
priceList.add(new PriceDetails("10063200", "3.1", "2022-05-26 00:00:00.000000"));
priceList.add(new PriceDetails("10054700", "1.58", "2022-04-30 00:00:00.000000"));
priceList.add(new PriceDetails("10063200", "3.88", "2022-02-18 00:00:00.000000"));
List<PriceDetails> anotherPriceList = removeLesserDateDuplicates(priceList);
/* Display the anotherPriceList List within the Console Window.
To see why the displayed result is as it is, look at the ID
values and the Dates (yyyy-MM-dd) carefully. */
for (PriceDetails pd : anotherPriceList) {
System.out.println(pd.toString());
}
使用示例資料運行時,控制臺視窗應顯示:
priceEndDate = 2022-05-26 00:00:00.000000, price = 3.1, materialId = 10063200
priceEndDate = 2022-04-30 00:00:00.000000, price = 1.58, materialId = 10054700
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/513108.html
標籤:爪哇日期收藏品哈希图
下一篇:在R中將日期更改為yyyy-mm
