Skip to content

Improve Collectors.toMap(Function,Function) error message for duplicate keys #9495

@mseele

Description

@mseele

GWT version: 2.8.0
Browser (with version): Chrome 56.0.2924.87 (64-bit)
Operating System: Windows 7


Description

Improve Lambda error messages. Please execute the example i've attached.
What i get running this as GWT, is:

Uncaught Error: java.lang.IllegalStateException: Can't assign multiple values to the same key
at Fm.P [as s] (dmm-0.js:573:12483)
at Fm.T [as v] (dmm-0.js:573:13033)

What i get running this in pure Java, is:

Exception in thread "main" java.lang.IllegalStateException: Duplicate key Item [id=1, name=1-Test]
at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1253)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)

The code works fine, i only want highlight that the error message in Pure Java is way better than i GWT. It's much more harder to get behind the error in GWT.

Steps to reproduce

Run the attached Java Class as pure Java application and as GWT application.

TestEntryPoint.txt

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.google.gwt.core.client.EntryPoint;

public class TestEntryPoint implements EntryPoint {

	public static void main(String[] args) {
		new TestEntryPoint().onModuleLoad();
	}
	
	@Override
	public void onModuleLoad() {
		List<Item> items = new ArrayList<>();
		items.add(new Item(0, "0-Test"));
		items.add(new Item(1, "1-Test"));
		items.add(new Item(1, "1-Duplicate"));

		Map<Long, Item> map = items.stream().collect(Collectors.toMap(t -> t.id, Function.identity()));
	}

	public static class Item {

		public Long id;

		public String name;

		public Item(long id, String name) {
			this.id = Long.valueOf(id);
			this.name = name;
		}

		@Override
		public String toString() {
			StringBuilder builder = new StringBuilder();
			builder.append("Item [id="); //$NON-NLS-1$
			builder.append(this.id);
			builder.append(", name="); //$NON-NLS-1$
			builder.append(this.name);
			builder.append("]"); //$NON-NLS-1$
			return builder.toString();
		}

	}

}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions