Java collection slow conversion

IMap<Long, Vehicle> mapVehicles = // get all vehicles , total 2500 unit
Collection<Vehicle> collectionVeh = mapVehicles.values(); // fast
// I want to sort it so wrap to ArrayList 
List<Vehicle> listVehicle = new ArrayList(collectionVeh .values()); // very slow
Collections.sort(listVehicle );// fast

How can I convert Collection to List very fast?

Thank.

+3
source share
3 answers

If you have Collection, and not List, and you want it to be like List, there is no faster way than doing something like new ArrayList(yourCollection). (I don’t think the constructor does any unnecessary work that you might miss, so to speak.)

. HashMap , . LinkedHashMap, . (, , .)

+3

TreeSet ?

+2

Could you try it Arrays.asList(collectionVeh.values().toArray)? Maybe it will work faster?

0
source

Source: https://habr.com/ru/post/1772601/


All Articles