Был задан вопрос о том, как превратить массив в список. Большинство ответов пока показали, как создать новый список с тем же содержимым, что и массив, или ссылаться на сторонние библиотеки. Тем не менее, существуют простые встроенные параметры для такого преобразования. Некоторые из них уже были набросаны в других ответах (например, этот ). Но я хотел бы указать и разработать определенные степени свободы для реализации здесь, и показать потенциальные выгоды, недостатки и предостережения.
Необходимо сделать как минимум два важных различия:
- Должен ли результирующий список быть представлением в массиве или это должен быть новый список
- Должен ли результирующий список быть изменяемым или нет
Параметры будут кратко изложены здесь, а полный пример программы показан внизу этого ответа.
Создание нового списка по сравнению с представлением в массиве
Когда результатом должен быть новый список, тогда можно использовать один из подходов из других ответов:
List<Long> list = Arrays.stream(array).boxed().collect(Collectors.toList());
Но следует учитывать недостатки этого: массив со значениями 1000000 long
будет занимать примерно 8 мегабайт памяти. Новый список также займет примерно 8 мегабайт. И, конечно же, полный массив должен быть пройден при создании этого списка. Во многих случаях создание нового списка просто не нужно. Вместо этого достаточно создать представление для массива:
// This occupies ca. 8 MB
long array[] = { /* 1 million elements */ }
// Properly implemented, this list will only occupy a few bytes,
// and the array does NOT have to be traversed, meaning that this
// operation has nearly ZERO memory- and processing overhead:
List<Long> list = asList(array);
(см. Пример внизу для реализации метода toList
)
Использование view для массива означает, что изменения в массиве будут видны в списке:
long array[] = { 12, 34, 56, 78 };
List<Long> list = asList(array);
System.out.println(list.get(1)); // This will print 34
// Modify the array contents:
array[1] = 12345;
System.out.println(list.get(1)); // This will now print 12345!
К счастью, создание копии (то есть, нового списка, который не подвержен изменениям в массиве) из представления тривиально:
List<Long> copy = new ArrayList<Long>(asList(array));
Теперь это верная копия, эквивалентная тому, что достигается с помощью потокового решения, показанного выше.
Создание изменяемого представления или неизменяемого представления
Во многих случаях будет достаточно, если список только для чтения . Содержимое результирующего списка часто не изменяется, а только передается в последующую обработку, которая только читает список.
Возможность внесения изменений в список вызывает некоторые вопросы:
long array[] = { 12, 34, 56, 78 };
List<Long> list = asList(array);
list.set(2, 34567); // Should this be possible?
System.out.println(array[2]); // Should this print 34567?
list.set(3, null); // What should happen here?
list.add(99999); // Should this be possible?
Можно создать представление списка для массива, который может быть модифицируемым . Это означает, что изменения в списке, такие как установка нового значения по определенному индексу, будут видны в массиве.
Но невозможно создать представление списка, которое является структурно модифицируемым . Это означает, что невозможно выполнять операции, которые влияют на размер списка. Это просто потому, что размер базового массива не может быть изменен.
Ниже приведен MCVE , показывающий различные варианты реализации и возможные способы использования полученных списков:
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class PrimitiveArraysAsLists
{
public static void main(String[] args)
{
long array[] = { 12, 34, 56, 78 };
// Create VIEWS on the given array
List<Long> list = asList(array);
List<Long> unmodifiableList = asUnmodifiableList(array);
// If a NEW list is desired (and not a VIEW on the array), this
// can be created as well:
List<Long> copy = new ArrayList<Long>(asList(array));
System.out.println("array : " + Arrays.toString(array));
System.out.println("list : " + list);
System.out.println("unmodifiableList: " + unmodifiableList);
System.out.println("copy : " + copy);
// Modify a value in the array. The changes will be visible
// in the list and the unmodifiable list, but not in
// the copy.
System.out.println("Changing value at index 1 of the array...");
array[1] = 34567;
System.out.println("array : " + Arrays.toString(array));
System.out.println("list : " + list);
System.out.println("unmodifiableList: " + unmodifiableList);
System.out.println("copy : " + copy);
// Modify a value of the list. The changes will be visible
// in the array and the unmodifiable list, but not in
// the copy.
System.out.println("Changing value at index 2 of the list...");
list.set(2, 56789L);
System.out.println("array : " + Arrays.toString(array));
System.out.println("list : " + list);
System.out.println("unmodifiableList: " + unmodifiableList);
System.out.println("copy : " + copy);
// Certain operations are not supported:
try
{
// Throws an UnsupportedOperationException: This list is
// unmodifiable, because the "set" method is not implemented
unmodifiableList.set(2, 23456L);
}
catch (UnsupportedOperationException e)
{
System.out.println("Expected: " + e);
}
try
{
// Throws an UnsupportedOperationException: The size of the
// backing array cannot be changed
list.add(90L);
}
catch (UnsupportedOperationException e)
{
System.out.println("Expected: " + e);
}
try
{
// Throws a NullPointerException: The value 'null' cannot be
// converted to a primitive 'long' value for the underlying array
list.set(2, null);
}
catch (NullPointerException e)
{
System.out.println("Expected: " + e);
}
}
/**
* Returns an unmodifiable view on the given array, as a list.
* Changes in the given array will be visible in the returned
* list.
*
* @param array The array
* @return The list view
*/
private static List<Long> asUnmodifiableList(long array[])
{
Objects.requireNonNull(array);
return new AbstractList<Long>()
{
@Override
public Long get(int index)
{
return array[index];
}
@Override
public int size()
{
return array.length;
}
};
}
/**
* Returns a view on the given array, as a list. Changes in the given
* array will be visible in the returned list, and vice versa. The
* list does not allow for <i>structural modifications</i>, meaning
* that it is not possible to change the size of the list.
*
* @param array The array
* @return The list view
*/
private static List<Long> asList(long array[])
{
Objects.requireNonNull(array);
return new AbstractList<Long>()
{
@Override
public Long get(int index)
{
return array[index];
}
@Override
public Long set(int index, Long element)
{
long old = array[index];
array[index] = element;
return old;
}
@Override
public int size()
{
return array.length;
}
};
}
}
Вывод примера показан здесь:
array : [12, 34, 56, 78]
list : [12, 34, 56, 78]
unmodifiableList: [12, 34, 56, 78]
copy : [12, 34, 56, 78]
Changing value at index 1 of the array...
array : [12, 34567, 56, 78]
list : [12, 34567, 56, 78]
unmodifiableList: [12, 34567, 56, 78]
copy : [12, 34, 56, 78]
Changing value at index 2 of the list...
array : [12, 34567, 56789, 78]
list : [12, 34567, 56789, 78]
unmodifiableList: [12, 34567, 56789, 78]
copy : [12, 34, 56, 78]
Expected: java.lang.UnsupportedOperationException
Expected: java.lang.UnsupportedOperationException
Expected: java.lang.NullPointerException