/** * Remove and return n elements from the hashtable. * The order in which entries are removed is corresponds * to the order in which they were inserted. * * @return first element */ @Override public List<T> pollN(int n) { if (n >= size) { // if we need to remove all elements then do fast polling return pollAll(); } List<T> retList = new ArrayList<T>(n); while (n-- > 0 && head != null) { T curr = head.element; this.removeElem(curr); retList.add(curr); } shrinkIfNecessary(); return retList; }
/** * Remove and return first n elements on the linked list of all elements. * * @return first element */ public List<T> pollN(int n) { if (n >= size) { // if we need to remove all elements then do fast polling return pollAll(); } List<T> retList = new ArrayList<T>(n); while (n-- > 0 && head != null) { T curr = head.element; this.removeElem(curr); retList.add(curr); } shrinkIfNecessary(); return retList; }
/** * Remove and return n elements from the hashtable. * The order in which entries are removed is corresponds * to the order in which they were inserted. * * @return first element */ @Override public List<T> pollN(int n) { if (n >= size) { // if we need to remove all elements then do fast polling return pollAll(); } List<T> retList = new ArrayList<T>(n); while (n-- > 0 && head != null) { T curr = head.element; this.removeElem(curr); retList.add(curr); } shrinkIfNecessary(); return retList; }
/** * Remove and return n elements from the hashtable. * The order in which entries are removed is corresponds * to the order in which they were inserted. * * @return first element */ @Override public List<T> pollN(int n) { if (n >= size) { // if we need to remove all elements then do fast polling return pollAll(); } List<T> retList = new ArrayList<T>(n); while (n-- > 0 && head != null) { T curr = head.element; this.removeElem(curr); retList.add(curr); } shrinkIfNecessary(); return retList; }
@Test public void testEmptyBasic() { LOG.info("Test empty basic"); Iterator<Integer> iter = set.iterator(); // iterator should not have next assertFalse(iter.hasNext()); assertEquals(0, set.size()); assertTrue(set.isEmpty()); // poll should return nothing assertNull(set.pollFirst()); assertEquals(0, set.pollAll().size()); assertEquals(0, set.pollN(10).size()); LOG.info("Test empty - DONE"); }
@Test public void testRemoveOne() { LOG.info("Test remove one"); assertTrue(set.add(list.get(0))); assertEquals(1, set.size()); // remove from the head/tail assertTrue(set.remove(list.get(0))); assertEquals(0, set.size()); // check the iterator Iterator<Integer> iter = set.iterator(); assertFalse(iter.hasNext()); // poll should return nothing assertNull(set.pollFirst()); assertEquals(0, set.pollAll().size()); assertEquals(0, set.pollN(10).size()); // add the element back to the set assertTrue(set.add(list.get(0))); assertEquals(1, set.size()); iter = set.iterator(); assertTrue(iter.hasNext()); LOG.info("Test remove one - DONE"); }
@Test public void testClear() { LOG.info("Test clear"); // use addAll set.addAll(list); assertEquals(NUM, set.size()); assertFalse(set.isEmpty()); // clear the set set.clear(); assertEquals(0, set.size()); assertTrue(set.isEmpty()); // poll should return an empty list assertEquals(0, set.pollAll().size()); assertEquals(0, set.pollN(10).size()); assertNull(set.pollFirst()); // iterator should be empty Iterator<Integer> iter = set.iterator(); assertFalse(iter.hasNext()); LOG.info("Test clear - DONE"); }