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

12
LINES

< > BotCompany Repo | #1015974 // addToListWithMaxSize - drops elements from start if list is too large; note: should be a LinkedList or CircularArrayList

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

Libraryless. Click here for Pure Java version (2090L/13K).

static <A> void addToListWithMaxSize(L<A> l, A a, int maxSize) {
  if (l == null || maxSize <= 0) ret;
  synchronized(collectionMutex(l)) {
    int n;
    while ((n = l.size()) >= maxSize) {
      l.remove(0);
      if (l.size() != n-1)
        fail("List remove failed!? " + l.size() + " / " + n + " / " + objectToStringWithClassName(l));
    }
    l.add(a);
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1015974
Snippet name: addToListWithMaxSize - drops elements from start if list is too large; note: should be a LinkedList or CircularArrayList
Eternal ID of this version: #1015974/8
Text MD5: 568c01d67cdf345420014374a7be0e7f
Transpilation MD5: 8da8a72403c1d8c1ac091cc8679d062f
Author: stefan
Category: javax / collections
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-05 13:17:48
Source code size: 376 bytes / 12 lines
Pitched / IR pitched: No / No
Views / Downloads: 418 / 513
Version history: 7 change(s)
Referenced in: [show references]