Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library: an efficient way to raise an exception if an item already exists in Map when adding an item #6118

Open
kevmoo opened this issue Oct 21, 2012 · 2 comments
Labels
area-library core-n P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@kevmoo
Copy link
Member

kevmoo commented Oct 21, 2012

Scenario: I want to make sure Keys are not duplicated in a map.

Current implementation:

  if(map.contains(key)) {
    throw 'dupe key';
  }
  map[key] = value;

I'd rather not dig into the map more than once.

Suggestion: add an optional paramater to putIfAbsent

  V putIfAbsent(K key, V ifAbsent(), [void ifPresent(V value)]);

One could then throw in the ifPresent callback.

I could imagine other uses for the ifPresent param as well. It also has nice symmetry with ifAbsent().

@dgrove
Copy link
Contributor

dgrove commented Oct 23, 2012

cc @floitschG.
cc @lrhn.
Added Area-Library, Triaged labels.

@lrhn
Copy link
Member

lrhn commented Aug 23, 2013

Removed Type-Defect label.
Added Type-Enhancement label.

@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed triaged labels Feb 29, 2016
@lrhn lrhn added the core-m label Aug 11, 2017
@floitschG floitschG added core-n and removed core-m labels Aug 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-library core-n P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants