JAVA: Set lepszy do .contain niż ArrayList
Dzisiaj takie święto, więc przy okazji - a w zasadzie to bez okazji, ale święto jest ;) - napiszę o takiej ciekawostce jak z tytułu. Zebrałem ciekawe wypowiedzi stąd : The set will give much better performance ( O(n) vs O(n^2) for the list), and that's normal because set membership (the contains operation) is the very purpose of a set. Contains for a HashSet is O(1) compared to O(n) for a list, therefore you should never use a list if you often need to run contains . I've made a test so please check the result: For SAME STRING items in a HashSet, TreeSet, ArrayList and LinkedList, here are the results for 1. 50.000 UUIDs SEARCHED ITEM : e608c7d5-c861-4603-9134-8c636a05a42b (index 25.000) hashSet.contains(item) ? TRUE 0 ms treeSet.contains(item) ? TRUE 0 ms arrayList.contains(item) ? TRUE 2 ms linkedList.contains(item) ? TRUE 3 ms 2. 5.000.000 UUIDs SEARCH...