admin管理员组文章数量:1023125
In the following example I would like to print out the items Ids of the one that get discarded. Is there a way not to get only the values, but the objects themselves?
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
// Example list with potential duplicates
List<Item> items = Arrays.asList(
new Item(1, "Item A"),
new Item(2, "Item B"),
new Item(1, "Item C"), // Duplicate key
new Item(3, "Item D")
);
// Building the Map
Map<Integer, String> itemMap = items.stream()
.collect(Collectors.toMap(
Item::getId, // Key mapper
Item::getName, // Value mapper
(existing, incoming) -> { // Merge function
// Print info about the duplicate
System.out.println("Duplicate key found. Existing value: " + existing + ", Incoming value: " + incoming);
// HOW CAN I PRINT THE INCOMING ELEMENTS ID HERE? <<<<<<<<<<<<
return incoming; // Keep the incoming value
}
));
// Print the resulting map
System.out.println("Resulting Map: " + itemMap);
}
}
class Item {
private final int id;
private final String name;
public Item(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Item{id=" + id + ", name='" + name + "'}";
}
}
In the following example I would like to print out the items Ids of the one that get discarded. Is there a way not to get only the values, but the objects themselves?
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
// Example list with potential duplicates
List<Item> items = Arrays.asList(
new Item(1, "Item A"),
new Item(2, "Item B"),
new Item(1, "Item C"), // Duplicate key
new Item(3, "Item D")
);
// Building the Map
Map<Integer, String> itemMap = items.stream()
.collect(Collectors.toMap(
Item::getId, // Key mapper
Item::getName, // Value mapper
(existing, incoming) -> { // Merge function
// Print info about the duplicate
System.out.println("Duplicate key found. Existing value: " + existing + ", Incoming value: " + incoming);
// HOW CAN I PRINT THE INCOMING ELEMENTS ID HERE? <<<<<<<<<<<<
return incoming; // Keep the incoming value
}
));
// Print the resulting map
System.out.println("Resulting Map: " + itemMap);
}
}
class Item {
private final int id;
private final String name;
public Item(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Item{id=" + id + ", name='" + name + "'}";
}
}
Share
Improve this question
asked Nov 19, 2024 at 10:04
BeginnerBeginner
5,4976 gold badges38 silver badges74 bronze badges
4
|
1 Answer
Reset to default 7You may use
Map<Integer, String> itemMap = items.stream()
.collect(Collectors.groupingBy(
Item::getId, // Key mapper
Collectors.collectingAndThen(
Collectors.reducing((existing, incoming) -> { // Merge function
// Print info about the duplicate
System.out.println("Duplicate key " + existing.getId()
+ " found. Existing value: " + existing.getName()
+ ", Incoming value: " + incoming.getName());
return incoming; // Keep the incoming value
}), o -> o.get().getName() // Value mapper
)));
Duplicate key 1 found. Existing value: Item A, Incoming value: Item C
Resulting Map: {1=Item C, 2=Item B, 3=Item D}
But you may also consider a tradional loop:
Map<Integer, String> itemMap = new HashMap<>();
for(Item i: items) {
String old = itemMap.put(i.getId(), i.getName());
if(old != null) {
System.out.println("Duplicate key " + i.getId()
+ " found. Existing value: " + old + ", Incoming value: " + i.getName());
}
}
In the following example I would like to print out the items Ids of the one that get discarded. Is there a way not to get only the values, but the objects themselves?
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
// Example list with potential duplicates
List<Item> items = Arrays.asList(
new Item(1, "Item A"),
new Item(2, "Item B"),
new Item(1, "Item C"), // Duplicate key
new Item(3, "Item D")
);
// Building the Map
Map<Integer, String> itemMap = items.stream()
.collect(Collectors.toMap(
Item::getId, // Key mapper
Item::getName, // Value mapper
(existing, incoming) -> { // Merge function
// Print info about the duplicate
System.out.println("Duplicate key found. Existing value: " + existing + ", Incoming value: " + incoming);
// HOW CAN I PRINT THE INCOMING ELEMENTS ID HERE? <<<<<<<<<<<<
return incoming; // Keep the incoming value
}
));
// Print the resulting map
System.out.println("Resulting Map: " + itemMap);
}
}
class Item {
private final int id;
private final String name;
public Item(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Item{id=" + id + ", name='" + name + "'}";
}
}
In the following example I would like to print out the items Ids of the one that get discarded. Is there a way not to get only the values, but the objects themselves?
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
// Example list with potential duplicates
List<Item> items = Arrays.asList(
new Item(1, "Item A"),
new Item(2, "Item B"),
new Item(1, "Item C"), // Duplicate key
new Item(3, "Item D")
);
// Building the Map
Map<Integer, String> itemMap = items.stream()
.collect(Collectors.toMap(
Item::getId, // Key mapper
Item::getName, // Value mapper
(existing, incoming) -> { // Merge function
// Print info about the duplicate
System.out.println("Duplicate key found. Existing value: " + existing + ", Incoming value: " + incoming);
// HOW CAN I PRINT THE INCOMING ELEMENTS ID HERE? <<<<<<<<<<<<
return incoming; // Keep the incoming value
}
));
// Print the resulting map
System.out.println("Resulting Map: " + itemMap);
}
}
class Item {
private final int id;
private final String name;
public Item(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Item{id=" + id + ", name='" + name + "'}";
}
}
Share
Improve this question
asked Nov 19, 2024 at 10:04
BeginnerBeginner
5,4976 gold badges38 silver badges74 bronze badges
4
-
2
This is not a particularly good candidate problem for a stream as streams do not prioritize or consider state. btw, your
Item
object (particularly because of your declared definition of 'duplicate') should overrideequals
andhashCode
– g00se Commented Nov 19, 2024 at 10:33 -
2
@g00se it's the
id
(the map key as anInteger
) that is duplicate, not theItem
type itself, so no need for equals/hashCode (although it's generally a good idea to have them). – k314159 Commented Nov 19, 2024 at 10:37 - 2 why not use Map<Integer, Item> instead. You are using itemId as key right? You will have the required info – Anubhav Sharma Commented Nov 19, 2024 at 10:38
-
2
@k314159 An object that has an
id
attribute that doesn't uniquely identify that object, should probably name its attribute differently ;) – g00se Commented Nov 19, 2024 at 10:39
1 Answer
Reset to default 7You may use
Map<Integer, String> itemMap = items.stream()
.collect(Collectors.groupingBy(
Item::getId, // Key mapper
Collectors.collectingAndThen(
Collectors.reducing((existing, incoming) -> { // Merge function
// Print info about the duplicate
System.out.println("Duplicate key " + existing.getId()
+ " found. Existing value: " + existing.getName()
+ ", Incoming value: " + incoming.getName());
return incoming; // Keep the incoming value
}), o -> o.get().getName() // Value mapper
)));
Duplicate key 1 found. Existing value: Item A, Incoming value: Item C
Resulting Map: {1=Item C, 2=Item B, 3=Item D}
But you may also consider a tradional loop:
Map<Integer, String> itemMap = new HashMap<>();
for(Item i: items) {
String old = itemMap.put(i.getId(), i.getName());
if(old != null) {
System.out.println("Duplicate key " + i.getId()
+ " found. Existing value: " + old + ", Incoming value: " + i.getName());
}
}
本文标签: javaAccess to the collected object when handling duplicates in toMapStack Overflow
版权声明:本文标题:java - Access to the collected object when handling duplicates in toMap - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745569020a2156623.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Item
object (particularly because of your declared definition of 'duplicate') should overrideequals
andhashCode
– g00se Commented Nov 19, 2024 at 10:33id
(the map key as anInteger
) that is duplicate, not theItem
type itself, so no need for equals/hashCode (although it's generally a good idea to have them). – k314159 Commented Nov 19, 2024 at 10:37id
attribute that doesn't uniquely identify that object, should probably name its attribute differently ;) – g00se Commented Nov 19, 2024 at 10:39