How to convert a Collection to List?

In Java, you can convert a Collection (such as Set, Queue, etc.) to a List using the following methods:

  1. Using the constructor of the ArrayList class:
Collection<String> collection = new HashSet<>();
collection.add("item1");
collection.add("item2");

List<String> list = new ArrayList<>(collection);
  1. Using the addAll() method of the ArrayList class:
Collection<String> collection = new HashSet<>();
collection.add("item1");
collection.add("item2");

List<String> list = new ArrayList<>();
list.addAll(collection);
  1. Using the asList() method of the Arrays class:
Collection<String> collection = new HashSet<>();
collection.add("item1");
collection.add("item2");

List<String> list = Arrays.asList(collection.toArray(new String[0]));

Keep in mind that the asList() method returns a fixed-size list, which means that you cannot add or remove elements from the list. If you need a mutable list, you should use one of the other methods.