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  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, ayivmpnvhhik, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, 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: 931 / 1447
Version history: 10 change(s)
Referenced in: #1002948 - cloneTreeMap - safely clone a map as TreeMap. generously makes a new one if passed map is null
#1005270 - cloneSet - safely clone a set, try to match original type
#1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1016188 - cloneAsHashMap
#1017936 - cloneLinkedHashMap
#3000382 - Answer for ferdie (>> t = 1, f = 0)