java - Frequency List (ArrayList or LinkedList) with items over 700,000 -
i have list contains elements of person data type. best way , less consumption of time, find frequency of each element in list.
the following things have tried, results in time beyond 10min on i7, 8gb
arraylist<string> frequent = new arraylist<>(); (person e: mylist2) { int frequency = 0; //int frequency = collections.frequency(mylist2, e); (person e : mylist2) { if (e.getpassname().equals(e.getpassname())) frequency++; } if (frequency >= times) frequent.add(e.getpassname()); }
also, collections results value 1, if there duplicates in linkedlist.
use map<person, integer>
store frequency:
map<person, integer> frequency = new hashmap<person, integer>(); (person person: mylist2) { if (frequency.containskey(person)) { frequency.put(person, frequency.get(person) + 1); } else { frequency.put(person, 1); } }
or map<person, atomicinteger>
(since that's mutable type, makes task of incrementing frequency easier):
for (person person: mylist2) { frequency.putifabsent(person, new atomicinteger(0)); frequency.get(person).incrementandget(); }
Comments
Post a Comment