Java ArrayList
,填充了名为packinglistrows的对象,其中包含三个键值(ISBN,PalletNumber,Quantity)以及其他属性。
我有ArrayList
,其中包含所有相同的ISBN值。我希望能够将具有相同PalletNumbers数量值的项目合并。
例如:
ArrayList items = [ packinglistrow( 1234, 1, 10 ), packinglistrow( 1234, 2, 5), packinglistrow( 1234, 1, 15 ) ]
合并后,[0]和[2]对象合并,因为它们具有相同的ISBN和托盘编号1.导致合并对象的更新数量:
ArrayList items = [ packinglistrow( 1234, 1, 25 ), packinglistrow( 1234, 2, 5) ]
正在考虑循环并比较并将不同类型添加到新ArrayList
然后循环并合并,但是必须有更简洁的方法来执行此操作吗?
感谢。
答案 0 :(得分:3)
首先创建一个类来处理这个数据。需要注意两点。 equals和hashcode方法仅基于isbn
和palletNumber
值,并且有merge
方法返回PackingListRow
的新实例,其数量介于此和您提供的其他实例作为参数。
class PackingListRow {
private final String isbn;
private final int palletNumber;
private final int quantity;
public PackingListRow(String isbn, int palletNumber, int quantity) {
this.isbn = isbn;
this.palletNumber = palletNumber;
this.quantity = quantity;
}
public String getIsbn() {
return isbn;
}
public int getPalletNumber() {
return palletNumber;
}
public int getQuantity() {
return quantity;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PackingListRow that = (PackingListRow) o;
return Objects.equals(palletNumber, that.palletNumber) &&
Objects.equals(isbn, that.isbn);
}
@Override
public int hashCode() {
return Objects.hash(isbn, palletNumber);
}
@Override
public String toString() {
return "PackingListRow{" +
"isbn='" + isbn + '\'' +
", palletNumber=" + palletNumber +
", quantity=" + quantity +
'}';
}
public PackingListRow merge(PackingListRow other) {
assert(this.equals(other));
return new PackingListRow(this.isbn, this.palletNumber, this.quantity + other.quantity);
}
}
完成后,您只需要创建另一个最初为空的新列表。它将包含合并的值。对于初始列表中的每个实例,检查它是否已在合并列表中。如果是,则通过调用merge来修改现有实例,否则只需将其附加到列表中即可。我们最终得到以下算法:
List<PackingListRow> list =
Arrays.asList(new PackingListRow("1234", 1, 10), new PackingListRow("1234", 2, 5), new PackingListRow("1234", 1, 15));
List<PackingListRow> mergedList = new ArrayList<>();
for(PackingListRow p : list) {
int index = mergedList.indexOf(p);
if(index != -1) {
mergedList.set(index, mergedList.get(index).merge(p));
} else {
mergedList.add(p);
}
}
System.out.println(mergedList);
哪个输出:
[PackingListRow{isbn='1234', palletNumber=1, quantity=25}, PackingListRow{isbn='1234', palletNumber=2, quantity=5}]
使用Java 8,我可能会使用不同的策略(至少你表明有多种方法可以解决问题)。我会创建一个静态类来为我进行分组:
class PackingListRow {
private final String isbn;
private final int palletNumber;
private final int quantity;
static class GroupPacking {
private final String isbn;
private final int palletNumber;
public GroupPacking(PackingListRow p) {
this.isbn = p.isbn;
this.palletNumber = p.palletNumber;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GroupPacking that = (GroupPacking) o;
return Objects.equals(palletNumber, that.palletNumber) &&
Objects.equals(isbn, that.isbn);
}
@Override
public int hashCode() {
return Objects.hash(isbn, palletNumber);
}
}
....
public PackingListRow merge(PackingListRow other) {
assert (new GroupPacking(other).equals(new GroupPacking(this)));
return new PackingListRow(this.isbn, this.palletNumber, this.quantity + other.quantity);
}
}
然后您可以使用Stream API。给定原始列表,您将获得Stream<PackingListRow>
,根据其GroupPacking实例(键)将元素从中收集到Map中。该值只是当前的PackingListRow
实例。如果您有两个具有相同GroupPacking值的实例(根据equals / hashcode),则合并它们。你终于得到了地图的values()
。
List<PackingListRow> mergedList =
new ArrayList<>(list.stream().collect(toMap(PackingListRow.GroupPacking::new, p -> p, PackingListRow::merge)).values());
答案 1 :(得分:0)
这里有一个工作示例(Java8 + Google Guava):
package com.rgrebski.test;
import com.google.common.base.*;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimaps;
import org.assertj.core.api.Assertions;
import org.testng.annotations.*;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class PackingListRowMergeTest {
@Test
public void test() {
//given
List<PackingListRow> packingListRow = ImmutableList.of(
new PackingListRow(1234, 1, 10),
new PackingListRow(1234, 2, 5),
new PackingListRow(1234, 1, 15)
);
//when
List<PackingListRow> mergedPackingListRow = Multimaps.index(packingListRow, groupByPaletNumbers())
.asMap()
.values()
.stream()
.map(packingListRowCollectionToSinglePackingListRowWithQuantitySum())
.collect(Collectors.toList());
//then
Assertions.assertThat(mergedPackingListRow).containsExactly(
new PackingListRow(1234, 1, 25),
new PackingListRow(1234, 2, 5)
);
}
protected java.util.function.Function<Collection<PackingListRow>, PackingListRow> packingListRowCollectionToSinglePackingListRowWithQuantitySum() {
return new java.util.function.Function<Collection<PackingListRow>, PackingListRow>() {
@Override
public PackingListRow apply(Collection<PackingListRow> packingListRows) {
int quantitySum = packingListRows.stream().flatMapToInt(packingListRow -> IntStream.of(packingListRow.getQuantity())).sum();
PackingListRow firstPackingListRow = packingListRows.stream().findFirst().get();
return new PackingListRow(firstPackingListRow.getIsbn(), firstPackingListRow.getPaletNumber(), quantitySum);
}
};
}
private Function<? super PackingListRow, Integer> groupByPaletNumbers() {
return new Function<PackingListRow, Integer>() {
@Override
public Integer apply(PackingListRow input) {
return input.getPaletNumber();
}
};
}
private static class PackingListRow {
private int isbn;
private int paletNumber;
private int quantity;
public PackingListRow(int isbn, int paletNumber, int quantity) {
this.isbn = isbn;
this.paletNumber = paletNumber;
this.quantity = quantity;
}
public int getIsbn() {
return isbn;
}
public int getPaletNumber() {
return paletNumber;
}
public int getQuantity() {
return quantity;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PackingListRow that = (PackingListRow) o;
return Objects.equal(this.isbn, that.isbn) &&
Objects.equal(this.paletNumber, that.paletNumber) &&
Objects.equal(this.quantity, that.quantity);
}
@Override
public int hashCode() {
return Objects.hashCode(isbn, paletNumber, quantity);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("isbn", isbn)
.add("paletNumber", paletNumber)
.add("quantity", quantity)
.toString();
}
}
}
答案 2 :(得分:0)
为ISBN&amp;创建一个对象似乎是合理的。托盘编号,但如果ISBN和&amp ;;托盘数量相等。所以对象可能如下所示:
public class PackingListRow {
private final String isbn;
private final int palletNumber;
public PackingListRow(String isbn, int palletNumber) {
this.isbn = isbn;
this.palletNumber = palletNumber;
}
@Override
public int hashCode() {
return palletNumber * 31 + ((isbn == null) ? 0 : isbn.hashCode());
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PackingListRow other = (PackingListRow) obj;
if (isbn == null) {
if (other.isbn != null)
return false;
} else if (!isbn.equals(other.isbn))
return false;
if (palletNumber != other.palletNumber)
return false;
return true;
}
@Override
public String toString() {
return isbn+":"+palletNumber;
}
}
之后,您可以将结果存储在Map
对象中,并通过以下方法添加项目:
public static void addItem(Map<PackingListRow, Integer> items, PackingListRow row,
int quantity) {
Integer oldQuantity = items.get(row);
items.put(row, oldQuantity == null ? quantity : quantity+oldQuantity);
}
如果您使用的是Java 8,则更简单:
public static void addItem(Map<PackingListRow, Integer> items, PackingListRow row,
int quantity) {
items.merge(row, quantity, Integer::sum);
}
测试样本数据:
public static void main(String[] args) {
Map<PackingListRow, Integer> items = new HashMap<PackingListRow, Integer>();
addItem(items, new PackingListRow("1234", 1), 10);
addItem(items, new PackingListRow("1234", 2), 5);
addItem(items, new PackingListRow("1234", 1), 15);
System.out.println(items);
}
输出结果为:
{1234:1=25, 1234:2=5}