Posty

Wyświetlam posty z etykietą set

Java: fajne sprawdzenie duplikatów

Można oczywiście iterować i wgl, a można po prostu tak: public boolean hasDuplicateSortValue (String[] items) { Set<String> appeared = new HashSet<String>() ; for (String item : items) { if (!appeared.add(item)) { return true; } } return false; } Sekret tkwi w tym, że metoda dodawania do seta to boolean ;) Bardzo mnie interesuje co o tym sądzisz, dlatego byłoby mi miło, jeśli byś napisał w komentarzu coś o tym, może być cokolwiek :)

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...