-4

I have two arraylists A and B in java. Arraylist A has duplicates but Arraylist B has unique and not all elements from arraylist A. I want to count the frequency of elements in arraylist A that are present in arraylist B

small example

A = {Red, Black, Red, Black, Green, Green, Brown, Black, Brown, Green}
B=  {Red, Brown, Green}

Result should be Red =2, Brown= 2 Green = 3

2
  • You can start by taking a look at the java.utils.Collections.frequency method. Jul 10, 2012 at 18:05
  • Collections.frequency seems to be slow. I want something that can do quickly for a log lists. Jul 10, 2012 at 18:38

3 Answers 3

0

Can you not replace list A with a Map<K, V> where K is the class type of the objects you have in A (such as String) and V is a Number such as Integer or Long. Then, instead of simply adding the same old values into A (when you construct the list in the first place), you would do this, for example:

Map<String, Integer> countMap = new HashMap<String, Integer>();
Integer currentCount = countMap.get("Red");
countMap.put("Red", (currentCount == null ? 1 : currentCount.intValue() + 1));

Then to get the counts of each object found in list B, you would just run through the map, checking each object from B one after the other:

List<String> listB = new List<String>();
listB.add("Red");
listB.add("Brown");
listB.add("Green");
for(String s : listB) {
    Integer quantityFoundInA = countMap.get(s);
    System.out.println("String <"+s"> found in list (map) 'A' "+(quantityFoundInA == null ? 0 : quantityFoundInA.intValue())+" times");
}

I'm guessing that this structure would be more efficient than simply storing multiple copies of the same objects in a List (so long as the objects you're storing in A are exactly the same if they share the same name).

0
0

UPDATE:

    List<String> listA = Arrays.asList("Red", "Black", "Red", "Black", "Green", "Green", "Brown", "Black", "Brown", "Green");
    List<String> listB = Arrays.asList("Red", "Brown", "Green");

    for (String color : listB) {
        System.out.println(string + " " + Collections.frequency(listA, color));
    }
5
  • Thanks for the reply. I think i did not frame my question well Jul 10, 2012 at 18:18
  • I have given an example to make the question mo0re clear. Any help is appreciated. Also i want a code well optimized and that can do quickly when there are large number of elements Jul 10, 2012 at 18:22
  • Can you not at least attempt this first?
    – Reimeus
    Jul 10, 2012 at 18:25
  • I tried this: List<String> p = new ArrayList<String>(ngrams); p.retainAll(ugrams); for (String a : p) { Integer freq = gramfrequency.get(a); gramfrequency.put(a, (freq == null ? 1 : freq + 1)); } // this is taking hell lot of time for a big list Jul 10, 2012 at 18:27
  • Collections.frequency is very slow anyway i will try again Jul 10, 2012 at 18:33
0

Take a look at the "MultiSet" interface in google guava:

MultiSet<String> multiSet = HashMultiSet.create(listA);
for (String s : listB) {
   multiSet.count(s);
}

You could always get better performance by storing your elements directly in the multiSet instead of a List. But that depends on what else you want to do with it, since MultiSet implements the Collection interface, and not List.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.