/** * Finds the index at which object should be inserted. */ public int findInsertionPoint(E o) { return findInsertionPoint(o, 0, size() - 1); }
/** * Adds an Object to sorted list. Object is inserted at correct place, found * using binary search. If the same item exist, it will be put to the end of * the range. * <p> * This method breaks original list contract since objects are not * added at the list end, but in sorted manner. */ @Override public boolean add(E o) { int idx = 0; if (!isEmpty()) { idx = findInsertionPoint(o); } super.add(idx, o); return true; }
/** * Finds the index at which object should be inserted. */ public int findInsertionPoint(final E o) { return findInsertionPoint(o, 0, size() - 1); }
/** * Adds an Object to sorted list. Object is inserted at correct place, found * using binary search. If the same item exist, it will be put to the end of * the range. * <p> * This method breaks original list contract since objects are not * added at the list end, but in sorted manner. */ @Override public boolean add(final E o) { int idx = 0; if (!isEmpty()) { idx = findInsertionPoint(o); } super.add(idx, o); return true; }
@Test void testList2() { SortedArrayList<String> list = new SortedArrayList<>(); list.add("bbb"); list.add("aaa"); assertEquals(2, list.size()); assertEquals("aaa", list.get(0)); assertEquals("bbb", list.get(1)); list.add("aa"); assertEquals(3, list.size()); assertEquals("aa", list.get(0)); list.add("a"); assertEquals(4, list.size()); assertEquals("a", list.get(0)); assertEquals(1, list.findInsertionPoint("a")); }
/** * Finds the index at which object should be inserted. */ public int findInsertionPoint(E o) { return findInsertionPoint(o, 0, size() - 1); }
/** * Finds the index at which object should be inserted. */ public int findInsertionPoint(final E o) { return findInsertionPoint(o, 0, size() - 1); }
/** * Adds an Object to sorted list. Object is inserted at correct place, found * using binary search. If the same item exist, it will be put to the end of * the range. * <p> * This method breaks original list contract since objects are not * added at the list end, but in sorted manner. */ @Override public boolean add(final E o) { int idx = 0; if (!isEmpty()) { idx = findInsertionPoint(o); } super.add(idx, o); return true; }
/** * Adds an Object to sorted list. Object is inserted at correct place, found * using binary search. If the same item exist, it will be put to the end of * the range. * <p> * This method breaks original list contract since objects are not * added at the list end, but in sorted manner. */ @Override public boolean add(E o) { int idx = 0; if (isEmpty() == false) { idx = findInsertionPoint(o); } super.add(idx, o); return true; }