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).

1  
static <A, B> Map<A, B> cloneMap(Map<A, B> map) {
2  
  if (map == null) ret new HashMap;
3  
  // assume mutex is equal to map
4  
  synchronized(map) {
5  
    ret map instanceof TreeMap ? new TreeMap((TreeMap) map) // copies comparator
6  
      : map instanceof LinkedHashMap ? new LinkedHashMap(map)
7  
      : new HashMap(map);
8  
  }
9  
}
10  
11  
static <A, B> L<B> cloneMap(Iterable<A> l, IF1<A, B> f) {
12  
  L x = emptyList(l);
13  
  if (l != null) for (A o : cloneList(l))
14  
    x.add(f.get(o));
15  
  ret x;
16  
}

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: 798 / 1265
Version history: 10 change(s)
Referenced in: [show references]