0

I have 2 arrays :

The first one consists of names of persons . The second one gives me a count of number of names that have appeared in a page. Ex: Names=["James","Loiui","Mario","Peter"] count=[1,4,2,5]

now I tried with this code give below :

SortedMap<Integer, String> m = new TreeMap<Integer, String>();
for(int i = 0; i < Names.size(); i++)
        m.put( count.get(i),Names.get(i));

But this doesnt work given there are repeating values of count . I figured out that the problem is with TreeMap as it stores only unique elements . Now to overcome problem of mine, are there any other valid solutions .

7
  • 1
    Names are likely to be unique, so you should use a SortedMap<String, Integer>. However if your goal is to sort by count, use a SortedMap<Integer, List<String>>. Commented May 5, 2014 at 10:24
  • But I want the final result to have descending order based on count ,i.e the most repeated name to appear first Commented May 5, 2014 at 10:28
  • Then you can use a SortedMap<Integer, List<String>>, as I said. Commented May 5, 2014 at 10:29
  • Then i cannot have 2 count values to be same ! Commented May 5, 2014 at 10:35
  • Why? If you have two count values that are the same, you'll have a mapping from this value to a List of 2 names. Commented May 5, 2014 at 10:35

3 Answers 3

3

Create a class PersonFrequency, containing a name and a count. Create a single array or list of PersonFrequency instances. Sort this array by count:

Java 8 example:

List<PersonFrequency> list = new ArrayList<>(names.length);
for (int i = 0; i < names.length; i++) {
    list.add(new PersonFrequency(names[i], counts[i]);
}

list.sort(Comparator.comparing(PersonFrequency::getCount).reversed());

List<String> sortedNames = list.stream()
                               .map(PersonFrequency::getName)
                               .collect(Collectors.toList());

In Java 7, the sort would become

Collections.sort(list, new Comparator<PersonFrequency>() {
    @Override
    public int compare(PersonFrequency p1, PersonFrequency p2) {
        return Integer.compare(p2.getCount(), p1.getCount());
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

could you please elaborate .
Elaborate what? Isn't the example code sufficiently understandable?
Is there any means by which I can employ PersonFrequency List into java 1.7 ?
Of course. You just need to use Collections.sort() and define a Comparator using an anonymous class rather than a lambda. there must be hundreds of questions asking how to sort a list of objects in Java. Google for them, or read my edited answer.
0

Sorted alphabetically

    String [] names = {"Aaa","Bbb"};
    int [] count = {1,2};

    SortedMap<String, Integer> m = new TreeMap<String, Integer>();

    for(int i = 0; i < names.length; i++){
        m.put(names[i], count[i]);
    }

Sorted by number:

    String [] names = {"Aaa","Bbb"};
    int [] count = {2,1};

    SortedMap<Integer, String> m = new TreeMap<Integer, String>();

    for(int i = 0; i < names.length; i++){
        m.put(count[i],names[i]);
    }

For descending change your sorted map to this

SortedMap<Integer, String> m = new TreeMap<Integer, String>().descendingMap();

1 Comment

Agreed ! But i want the resultant result to be sorted based on the number of count values . and not alphabetically !
0

As mentioned in the previous comments you can have a sorted map with Integer and ArrayList parameters as below,

SortedMap<Integer, ArrayList<String>> map=new TreeMap<Integer, ArrayList<String>>().descendingMap();

Since you want the results to be in descending order I added descendingMap().

We use ArrayList because you may have duplicates in count array. More than one name can have the same count value. So one count will have a list of names.

And the complete code is below,

import java.util.ArrayList;
import java.util.SortedMap;
import java.util.TreeMap;



public class NameCount {

    public static void main(String[] args) {

        String[] names = {"James","Loiui","Mario","Peter"};
        int[] count = {1, 4, 2, 1};

        SortedMap<Integer, ArrayList<String>> map=new TreeMap<Integer, ArrayList<String>>().descendingMap();
        ArrayList<String> nameList=null;

        for(int i = 0; i < names.length; i++)
        {
            if(map.get(count[i])==null)
            {
                nameList = new ArrayList<String>();
                nameList.add(names[i]);
                map.put(count[i], nameList);
            }
            else
            {
                map.get(count[i]).add(names[i]);
            }
        }

        for(int countVal : map.keySet())
        {
            ArrayList<String> namesListVal=map.get(countVal);

            System.out.print("\nCount  "+countVal+ ":  NAMES  : ");
            for(String name : namesListVal)
            {
                System.out.print(name+"  ");
            }

        }
    }

}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.