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

31
LINES

< > BotCompany Repo | #1001899 // asSet - makes a guess whether to use TreeSet or HashSet. also drops null elements

JavaX fragment (include)

1  
static Set asSet(O[] array) {
2  
  new HashSet set;
3  
  for (O o : array)
4  
    if (o != null)
5  
      set.add(o);
6  
  ret set;
7  
}
8  
9  
static Set<S> asSet(S[] array) {
10  
  new TreeSet<S> set;
11  
  for (S o : array)
12  
    if (o != null)
13  
      set.add(o);
14  
  ret set;
15  
}
16  
17  
static <A> Set<A> asSet(Iterable<A> l) {
18  
  if (l instanceof Set) ret (Set) l;
19  
  new HashSet<A> set;
20  
  for (A o : unnull(l))
21  
    if (o != null)
22  
      set.add(o);
23  
  ret set;
24  
}
25  
26  
ifclass MultiSet
27  
// Note: does not clone the set (keeps multiset alive)
28  
static <A> Set<A> asSet(MultiSet<A> ms) {
29  
  ret ms == null ? null : ms.asSet();
30  
}
31  
endif

download  show line numbers  debug dex  old transpilations   

Travelled to 21 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, wtqryiryparv, xrpafgyirdlv

No comments. add comment

Snippet ID: #1001899
Snippet name: asSet - makes a guess whether to use TreeSet or HashSet. also drops null elements
Eternal ID of this version: #1001899/6
Text MD5: 44056753af36a97874ab95e7322d2556
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-06 16:28:40
Source code size: 610 bytes / 31 lines
Pitched / IR pitched: No / No
Views / Downloads: 955 / 3134
Version history: 5 change(s)
Referenced in: [show references]