4

I have two ArrayLists: a String list and an integer list. For example:

Bob        2
Kevin      6
Lane       4
Susan      2
//the ArrayLists will have more elements

I'm trying to sort the integer ArrayList (with MergeSort) and having the String ArrayList correspond to the integer ArrayList, such as:

Susan      2
Bob        2
Lane       4
Kevin      6

The other answers I've come upon advised on using a Map (the int being the key, the String being the value); however, I cannot do so because there would be duplicate keys.

I have read about Guava's Multimap; however, I'm not quite comfortable with including jar files to classpaths.

Is there any other way to do this?

1
  • 4
    I suggest you create a POJO (Plain Old Java Object) with your two field values, add that to one ArrayList. Commented Jun 15, 2017 at 23:30

2 Answers 2

4

As long as there's actually some sort of meaningful connection between the Strings and ints, the best way to do this would be to create a Java class to serve your purpose, something like:

public class MyClass implements Comparable<MyClass> {
    private String myString;
    private int myInt;

    public MyClass(String s, int x) {
        myString = s;
        myInt = x;
    }

    public int getInt() {
        return myInt;
    }

    public String getString() {
        return myString;
    }

    public void setInt(int x) {
        myInt = x;
    }

    public void setString(String s) {
        myString = s;
    }

    // this method is the only method defined in the Comparable<T> interface
    // and is what allows you to later do something like Collections.sort(myList)

    public int compareTo(MyClass other) {
        return myInt - other.getInt();
    }
}

And then create a list List<MyClass> ls = new ArrayList<MyClass>(); which you can add instances of your newly created class to.

The fact that this class implements the Comparable interface means that you can use Java's predefined sort methods like Collections.sort(), and have the Collections class know how you want your objects to be sorted. If instead you would rather implement your own sorting algorithm, implementing this interface is not necessary, but still good practice.

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

Comments

0

One of the ways to use is really easy hashmap, for example:

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;

public class ExStringArraySort {

    static HashMap<String, Integer> mapPerson = new HashMap<String, Integer>();
    static String[] prsn;

    public static void main(String[] args) {
        // Add persons
        mapPerson.put("Bob", 2);
        mapPerson.put("Kevin", 6);
        mapPerson.put("Lane", 4);
        mapPerson.put("Susan", 2);

        String[] prsn = mapPerson.keySet().toArray(new String[mapPerson.size()]);

        Arrays.sort(prsn);
        System.out.println(Arrays.toString(prsn));

        System.out.println("Print detail:");
        for (int i = 0; i < prsn.length; i++) {
            System.out.println(prsn[i]+"\t"+mapPerson.get(prsn[i]));
        }

        Arrays.sort(prsn, Collections.reverseOrder());
        System.out.println("\n"+Arrays.toString(prsn));
        System.out.println("Print detail:");
        for (int i = 0; i < prsn.length; i++) {
            System.out.println(prsn[i]+"\t"+mapPerson.get(prsn[i]));
        }

    }

}

OUTPUT:

[Bob, Kevin, Lane, Susan]
Print detail:
Bob 2
Kevin   6
Lane    4
Susan   2

[Susan, Lane, Kevin, Bob]
Print detail:
Susan   2
Lane    4
Kevin   6
Bob 2

Comments

Your Answer

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