3

My program has to use the Collections sort method to sort the ArrayList of Strings lexicographically but each String has a corresponding integer value stored in a separate ArrayList. I want to sort them both the same so the integer values stay with the correct Strings. And if you know a better way to store both values I'm all ears.

public class a5p1b {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in).useDelimiter("[^a-zA-z]+");
        // ArrayLists to store the Strings and the frequencies
        ArrayList<String> lst = new ArrayList<String>();
        ArrayList<Integer> intLst = new ArrayList<Integer>();

        //loops through as long as there is user input
        while (input.hasNext()) {
            String str = input.next().toLowerCase();
            // if the list already has the string it doesn't add it and it
            // ups the count by 1
            if (lst.contains(str)) {
                int index = lst.indexOf(str);
                intLst.set(index, intLst.get(index) + 1);
            } else {
                // if the word hasnt been found yet it adds it to the list
                lst.add(str);
                intLst.add(1);
            }
        }
    }    
}
2
  • Do you want them sorted numerically or lexicographically? But maybe you could store them in a map. Commented Nov 15, 2016 at 18:43
  • Use a map from strings to integers, then sort the keys and pull out the values in sorted order? Commented Nov 15, 2016 at 18:45

3 Answers 3

5

You are getting your abstractions wrong. If that string and that number belong together, then do not keep them in two distinct lists.

Instead create a class (or maybe use one of the existing Pair classes) that holds those two values. You can then provide an equals method for that class; plus a specific comparator, that only compares the string elements.

Finally, you put objects of that class into a single list; and then you sort that list.

The whole idea of good OO programming is to create helpful abstractions!

For the record: as dnault suggests, if there is really no "tight" coupling between strings and numbers you could also use a TreeMap (to be used as TreeMap<String, Integer>) to take care of sorting strings that have a number with them.

Sign up to request clarification or add additional context in comments.

2 Comments

TreeMap<String, Integer> might also be a viable alternative.
@dnault I had the same idea while walking the dog; but thanks for you input; I updated my answer accordingly.
0

Try

inList.sort(Comparator.comparing(i -> i.toString());

Although, I don't think the two lists is a good idea.

Comments

0

You should use a Map to associate each unique String key with an Integer value.

Then you can invoke Collections.sort on the map's set of keys returned by keySet().

Additionally, if you use a SortedMap such as TreeMap, it is not necessary to sort the keys. However that solution may not fulfill the requirements of your "Assignment 5 Problem 1b."

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.