Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to manipulate list in java

Edit: My list is sorted as it is coming from a DB I have an ArrayList that has objects of class People. People has two properties: ssn and terminationReason. So my list looks like this

ArrayList: 
ssn            TerminatinoReason
123456789      Reason1
123456789      Reason2
123456789      Reason3
568956899      Reason2
000000001      Reason3
000000001      Reason2

I want to change this list up so that there are no duplicates and termination reasons are seperated by commas.

so above list would become

New ArrayList: 
ssn            TerminatinoReason
123456789      Reason1, Reason2, Reason3
568956899      Reason2
000000001      Reason3, Reason2

I have something going where I am looping through the original list and matching ssn's but it does not seem to work.

Can someone help?

Code I was using was:

    String ssn = "";
    Iterator it = results.iterator();
    ArrayList newList = new ArrayList();
    People ob;
    while (it.hasNext())
    {
       ob = (People) it.next();
       if (ssn.equalsIgnoreCase(""))
       {
           newList.add(ob);
           ssn = ob.getSSN();
       }
       else if (ssn.equalsIgnoreCase(ob.getSSN()))
       {
           //should I get last object from new list and append this termination reason?
            ob.getTerminationReason()
       }
    }

1 Answers

To me, this seems like a good case to use a Multimap, which would allow storing multiple values for a single key.

The Google Collections has a Multimap implementation.

This may mean that the Person object's ssn and terminationReason fields may have to be taken out to be a key and value, respectively. (And those fields will be assumed to be String.)

Basically, it can be used as follows:

Multimap<String, String> m = HashMultimap.create();

// In reality, the following would probably be iterating over the
// Person objects returned from the database, and calling the
// getSSN and getTerminationReasons methods.

m.put("0000001", "Reason1");
m.put("0000001", "Reason2");
m.put("0000001", "Reason3");
m.put("0000002", "Reason1");
m.put("0000002", "Reason2");
m.put("0000002", "Reason3");

for (String ssn : m.keySet())
{
    // For each SSN, the termination reasons can be retrieved.
    Collection<String> termReasonsList = m.get(ssn);

    // Do something with the list of reasons.
}

If necessary, a comma-separated list of a Collection can be produced:

StringBuilder sb = new StringBuilder();

for (String reason : termReasonsList)
{
    sb.append(reason);
    sb.append(", ");
}

sb.delete(sb.length() - 2, sb.length());
String commaSepList = sb.toString();

This could once again be set to the terminationReason field.

An alternative, as Jonik mentioned in the comments, is to use the StringUtils.join method from Apache Commons Lang could be used to create a comma-separated list.

It should also be noted that the Multimap doesn't specify whether an implementation should or should not allow duplicate key/value pairs, so one should look at which type of Multimap to use.

In this example, the HashMultimap is a good choice, as it does not allow duplicate key/value pairs. This would automatically eliminate any duplicate reasons given for one specific person.

like image 114
coobird Avatar answered Aug 16 '26 00:08

coobird