Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

16
LINES

< > BotCompany Repo | #1002559 - cloneMap - safely clone a map; null => HashMap. also: map over cloned list

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2150L/14K).

static <A, B> Map<A, B> cloneMap(Map<A, B> map) {
  if (map == null) ret new HashMap;
  // assume mutex is equal to map
  synchronized(map) {
    ret map instanceof TreeMap ? new TreeMap((TreeMap) map) // copies comparator
      : map instanceof LinkedHashMap ? new LinkedHashMap(map)
      : new HashMap(map);
  }
}

static <A, B> L<B> cloneMap(Iterable<A> l, IF1<A, B> f) {
  L x = emptyList(l);
  if (l != null) for (A o : cloneList(l))
    x.add(f.get(o));
  ret x;
}

Author comment

Began life as a copy of #1001634

download  show line numbers  debug dex   

Travelled to 14 computer(s): aoiabmzegqzx, ayivmpnvhhik, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1002559
Snippet name: cloneMap - safely clone a map; null => HashMap. also: map over cloned list
Eternal ID of this version: #1002559/11
Text MD5: dd87529cba1e7a198d1c2d486fade481
Transpilation MD5: ad338300e25fbd334ce422f2f24d2ba8
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-01-15 14:28:14
Source code size: 486 bytes / 16 lines
Pitched / IR pitched: No / No
Views / Downloads: 533 / 901
Version history: 10 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1002559 & http://1002559.tinybrain.de