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

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -