7. www.luxoft.com
После вычитки
T[] items = data.array;
Collection<T> coll = new HashSet<>();
coll.addAll(asList(items));
T[] items = data.array;
Collection<T> coll = new HashSet<>();
Collections.addAll(coll, items);
8. www.luxoft.com
Ведь написано же
/**
* Adds all of the specified elements to the specified collection.
* Elements to be added may be specified individually or as an array.
* The behavior of this convenience method is identical to that of
* <tt>c.addAll(Arrays.asList(elements))</tt>, but this method is likely
* to run significantly faster under most implementations.
*/
public static <T> boolean addAll(Collection<? super T> c, T... elements) {
boolean result = false;
for (T element : elements)
result |= c.add(element);
return result;
}
25. www.luxoft.com
public E remove(int index) {
Objects.checkIndex(index, size);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index + 1, elementData, index, numMoved);
elementData[--size] = null; // clear to let GC do its work
return oldValue;
}
ArrayList.remove(index)
26. www.luxoft.com
public E remove(int index) {
Objects.checkIndex(index, size);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index + 1, elementData, index, numMoved);
elementData[--size] = null; // clear to let GC do its work
return oldValue;
}
ArrayList.remove(index)
29. www.luxoft.com
for (int i = from; i < to; i++) {
E removed = list.remove(from);
use(removed)
}
Если удалённое значение
используется?
30. www.luxoft.com
//обратный проход
for (int i = to - 1; i >= from; i--) {
E removed = list.remove(i);
use(removed)
}
//прямой проход
for (int i = from; i < to; i++) {
E removed = list.remove(from);
use(removed)
}
ArrayList.remove(i): обратный проход
31. www.luxoft.com
for (int i = from; i < to; i++) // прямой проход
list.remove(from);
for (int i = to - 1; i >= from; i--) // обратный проход
list.remove(i);
list.subList(from, to).clear(); // subList
Считаем удаление из середины списка
43. www.luxoft.com
И посмотрим на них в отладчике
Iterator<String> iterator1 = items.stream().map(Object::toString).collect(toList()).iterator();
Iterator<String> iterator2 = items.stream().map(Object::toString).iterator();
45. www.luxoft.com
Проход по нему тоже несложный
public boolean hasNext() {
return cursor != size;
}
public E next() {
checkForComodification();
int i = cursor;
if (i >= size) throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
49. www.luxoft.com
Проход по нему сложнее
public boolean hasNext() {
if (!valueReady)
spliterator.tryAdvance(this);
return valueReady;
}
public T next() {
if (!valueReady && !hasNext())
throw new NoSuchElementException();
else {
valueReady = false;
return nextElement;
}
}
64. www.luxoft.com
Что мы сделали?
Завернули
массив в
список
Обрезали
список
Развернули
список в
массив
StackTraceElement[] trace = th.getStackTrace();
StackTraceElement[] newTrace = Arrays
.asList(trace).subList(0, newDepth).toArray(new StackTraceElement[0]);
65. www.luxoft.com
Что нужно было сделать
Arrays.asList(trace).subList(0, newDepth).toArray(new StackTraceElement[0]);
Arrays.copyOf(trace, newDepth);
66. www.luxoft.com
Что нужно было сделать
Arrays.asList(trace).subList(0, newDepth).toArray(new StackTraceElement[0]);
Arrays.copyOf(trace, newDepth); // простая запись, отсчёт с 0
или
Arrays.copyOfRange(trace, 0, newDepth)); // формальная запись, явно указан
0
80. www.luxoft.com
List<T> list = new ArrayList<>();
for (Byte item : items)
list.add(item);
Выводы: аргументация
List<T> list = new ArrayList<>(items);
81. www.luxoft.com
List<T> list = new ArrayList<>();
for (Byte item : items)
list.add(item);
Iterator<T> iterator = list.stream()
.map(Object::toString)
.iterator();
while (iterator.hasNext())
bh.consume(iterator.next());
Выводы: аргументация
List<T> list = new ArrayList<>(items);
Iterator<T> iterator = list.stream()
.map(Object::toString)
.forEach(bh::consume);
82. www.luxoft.com
List<T> list = new ArrayList<>();
for (Byte item : items)
list.add(item);
Iterator<T> iterator = list.stream()
.map(Object::toString)
.iterator();
while (iterator.hasNext())
bh.consume(iterator.next());
list.stream().allMatch(collection::contains);
Выводы: аргументация
List<T> list = new ArrayList<>(items);
Iterator<T> iterator = list.stream()
.map(Object::toString)
.forEach(bh::consume);
collection.containsAll(list);
83. www.luxoft.com
List<T> list = new ArrayList<>();
for (Byte item : items)
list.add(item);
Iterator<T> iterator = list.stream()
.map(Object::toString)
.iterator();
while (iterator.hasNext())
bh.consume(iterator.next());
list.stream().allMatch(collection::contains);
Arrays.asList(array)
.subList(0, size)
.toArray(new String[0]);
Выводы: аргументация
List<T> list = new ArrayList<>(items);
Iterator<T> iterator = list.stream()
.map(Object::toString)
.forEach(bh::consume);
collection.containsAll(list);
Arrays.copyOf(array, size);
84. www.luxoft.com
IDEA-138456 Performance inspection: 'Collection.addAll() can be replaced with
parametrised constructor‘ (142.1217)
IDEA-165942 Inspection to replace method call in a loop with bulk operation (2017.1)
IDEA-178761 'Collection.addAll() can be replaced with parametrized constructor'
should be active by default (2017.3)
IDEA-182206 Simplification for Arrays.asList().sublist().toArray() (2018.1)
IDEA-181928 Stream.allMatch(Collection::contains) can be simplified to
Collection.containsAll() (2018.1)
IDEA-184240 Unnecessary array-to-collection wrapping should be detected (2018.1)
В сухом остатке