Как преобразовать collection в list

Обновлено: 16.05.2024

Однако мне интересно, есть ли способ сделать это "на лету", когда повторяется список целей, поэтому мне не нужно повторять этот список дважды.

Мой ответ на этот вопрос относится к вашему делу:

Преобразованный список представляет собой представление исходной коллекции, поэтому преобразование происходит, когда обращается к пункту назначения List .

В качестве альтернативы шаблону итератора вы можете использовать абстрактный общий класс сопоставления и только переопределить метод преобразования:

  • создать универсальный сборщик для любого типа данных
  • [необязательно] создать библиотеку методов, которые преобразуются между разными типами данных (и переопределяют метод)
  • используйте эту библиотеку

Это было бы полезно, если вам нужно каждый раз использовать преобразования, инкапсулируя процесс. Таким образом, вы можете легко сделать библиотеку собирателей.

Предполагая, что класс Wrapper имеет конструктор, принимающий String .

Вы можете написать итератор отображения, который украсит существующий итератор и применит к нему функцию. В этом случае функция преобразует объекты из одного типа в другой "на лету".

Что-то вроде этого:

Lambdaj позволяет сделать это очень простым и понятным способом. Например, предположим, что у вас есть список Integer и вы хотите преобразовать их в соответствующее строковое представление, вы могли бы написать что-то подобное:

Lambdaj применяет функцию преобразования только во время повторения результата. Существует также более сжатый способ использования одной и той же функции. В следующем примере предполагается, что у вас есть список лиц с свойством name и вы хотите преобразовать этот список в итератор имен людей.

Довольно легко. Не правда ли?

Этот вопрос не перебирает список дважды. Он просто итерационно один раз и является единственным известным методом.

Также вы можете использовать некоторые классы трансформаторов в коллекциях коллекций google-коллекций, но все они делают то же самое под капотом:) следующим образом:

How to convert a Collection to List?

I am using TreeBidiMap from the Apache Collections library. I want to sort this on the values which are doubles .

My method is to retrieve a Collection of the values using:

Which naturally works fine.

Main Question: I now want to know how I can convert/cast (not sure which is correct) coll into a List so it can be sorted?

I then intend to iterate over the sorted List object, which should be in order and get the appropriate keys from the TreeBidiMap ( themap ) using themap.getKey(iterator.next()) where the iterator will be over the list of doubles .

47.8k 107 107 gold badges 238 238 silver badges 310 310 bronze badges TreeBidiMap is an OrderedMap , the order should be ok. The sorting required in the question is on values, not on keys.

7 Answers 7

Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> .

Any Collection can be passed as an argument to the constructor as long as its type extends the type of the ArrayList , as String extends Object . The constructor takes a Collection , but List is a subinterface of Collection , so you can just use the List<String> .

30.1k 12 12 gold badges 67 67 silver badges 98 98 bronze badges This constructs unnecessary objects and can, if this is a big list, potentially take up a lot of memory. Just cast it. @Martin. It created just one extra object ( with its internal machinery which is small ) Even if the list is huge, there is no a significant impact in the memory usage, because what is being stored are reference values. There is not a deep copy involved. Plus, this would keep using the generics to force the collection of the same type, while casting would just throw it away I just today cracked a memory-hog with a list growing to 90+ million elements, just references, allocating a total of 1.3GB in one thread (and I had 4-5 of those threads). A bug, admittedly, but in normal cases the list would be max 5.7 million, still significant. It comes down to situation and the pragmatic choice. Casting is not a blank no no.

Any java collection is just a collection of objects be it string or other. The type argument is just sugar. Depending on situation, such as dealing with very large lists, you may just want to convert it - obviously risking mixing two different types of objects in the same list.

And put a @SuppressWarning to get rid of nasties.

11.8k 2 2 gold badges 47 47 silver badges 75 75 bronze badges

Personally, while both of the currently top rated answers are right in a way, I do not think any of them solves the problem in an elegant, reusable way, especially if you have to do this very often.

Suppose you have some old legacy code / dependency that you cannot change in any way (so that it would at least accept List<? extends Object> as @ReverendGonzo suggested in his comment. Suppose also, that you need to talk to this legacy module a lot.

I do not think either casting / copying all the time would be bearable on the long run. It makes your code either vulnerable to insidious bugs and hard to follow or slightly (or drastically) inefficient and hard-to-read.

To have readable and efficient production code, it is better to encapsulate the dirty part in a separate module which deals with the otherwise harmless but ugly cast.

Преобразование коллекции в список

Я хотел бы спросить: как вы преобразовываете Collection в List в Java?

Если вы уже создали экземпляр своего подтипа List (например, ArrayList, LinkedList), вы можете использовать метод addAll.

Многие подтипы списка также могут брать исходный набор в свой конструктор.

Создайте новый список и вызовите addAll с помощью коллекции.

Спасибо, что Sandeep положил его. Просто добавил нулевую проверку, чтобы избежать инструкции NullPointerException в else.

вы можете использовать любое из двух решений.. но подумайте, нужно ли клонировать ваши коллекции, поскольку обе коллекции будут содержать те же ссылки на объекты

Collection и List являются интерфейсами. Вы можете выполнить любую реализацию интерфейса List : ArrayList LinkedList и просто вернуть его обратно в Collection , потому что он находится в верхней части

11 Answers 11

As Erel Segal Halevi says below, if coll is already a list, you can skip step one. But that would depend on the internals of TreeBidiMap.

170k 56 56 gold badges 308 308 silver badges 397 397 bronze badges Just to note that there are different side effects to the two approaches: casting the collection to a list and then sorting will also sort the original collection; creating a copy will not. This approach degrades performance greatly if used repeatedly. See my answer for a solution that works on-the-fly, it involves a custom collection. This does not resolve the case when map.values() returns an "inner class" collection. The compiler reports that Collections.sort(List <T>) does not accept Collections.sort(List<InnerClass>). The solution was even to use: List<InnerClass> list = map.values().stream().collect(Collectors.toList())

Something like this should work, calling the ArrayList constructor that takes a Collection:


235k 74 74 gold badges 696 696 silver badges 958 958 bronze badges 20.9k 4 4 gold badges 48 48 silver badges 55 55 bronze badges

I think Paul Tomblin's answer may be wasteful in case coll is already a list, because it will create a new list and copy all elements. If coll contains many elemeents, this may take a long time.

My suggestion is:

27.4k 27 27 gold badges 98 98 silver badges 155 155 bronze badges

I believe you can write it as such:

Great! This resolved my case. My map.values() returns an "inner class" collection. The compiler reported that Collections.sort(List <T>) does not accept Collections.sort(List<InnerClass>).


11.2k 3 3 gold badges 32 32 silver badges 61 61 bronze badges

@Kunigami: I think you may be mistaken about Guava's newArrayList method. It does not check whether the Iterable is a List type and simply return the given List as-is. It always creates a new list:

514 1 1 gold badge 5 5 silver badges 14 14 bronze badges

What you request is quite a costy operation, make sure you don't need to do it often (e.g in a cycle).

If you need it to stay sorted and you update it frequently, you can create a custom collection. For example, I came up with one that has your TreeBidiMap and TreeMultiset under the hood. Implement only what you need and care about data integrity.

This way, you have a sorted Multiset returned from values() . However, if you need it to be a list (e.g. you need the array-like get(index) method), you'd need something more complex.

For brevity, I only return unmodifiable collections. What @Lino mentioned is correct, and modifying the keySet or values collection as it is would make it inconsistent. I don't know any consistent way to make the values mutable, but the keySet could support remove if it uses the remove method from the MyCustomCollection class above.

How to Convert List<String> to List<Object>

I want to convert List<String> to List<Object> .

One of the existing methods is returning List<String> and I want to convert it to List<Object> . Is there a direct way in Java other then iterating over and converting element by element?

11k 5 5 gold badges 43 43 silver badges 42 42 bronze badges 565 2 2 gold badges 5 5 silver badges 5 5 bronze badges You might need to pass the list to a function that expects a List<Object> Yes, I have requirement to convert to List<Object> as i have to pass it back to another method which accepts like List<Object>.

Читайте также: