0

I have a sorted array list with 6 elements.The first 5 elements have some value, and the 6th one is empty.

I want to loop through this ArrayList, and compare the first 5 elements of first record, with the same elements in next record. If any one of the element is different, then populate 6th element.

Anyone know an easier and faster approach to do that?

Thanks Angad

1
  • 2
    add your attempts and we will help you Commented May 14, 2016 at 17:25

2 Answers 2

1

First split the all records into many String[], then parse all values in each. After that you can compare the current record with the first one. Here is an example:

public class StringComparison {

ArrayList<String[]> list = new ArrayList<String[]>();

public void comapreStrings() {
    // The list you are comparing everything to
    String[] firstRecord = list.get(0);

    for(int n = 1; n < list.size(); n++) {
        String[] currentRecord = list.get(n);
        for (int i = 0; i < currentRecord.length; i++) {
            String val1 = firstRecord[i];
            String val2 = currentRecord[i];
            if (val1.equals(val2)) {
                // The two strings are the same
            } else {
                // Replace "a value" with whatever you want to fill the 6th element with
                currentRecord[5] = "a value";
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Akash, But I have only one arraylist. I need to compare record by record. My requirement is to generate a sequence id whenever a new record is encountered.
What do you mean by record?
following is example of my Arraylist Row1 : A1, B1, C1, D1, E1, ' ' Row2: A1, B1, C1, D1, E2, ' ' I need to compare values in first row with values in the next. In the above case, since E1 != E2, It means it is differnt, so need to generate a sequence number, and populate the 6th element of array which is currently blank.
So is your ArrayList one with String[] or String?
thanks Akash, but i have hundreds of records, not just two.
|
0

Maybe this could be an alternative to think about:

public String[] generateFirstRow(int startingRow) {

    final String[] row1 = rowList.get(startingRow);
    final String[] row2 = rowList.get(startingRow + 1);
    final List<String> list1 = Arrays.stream(row1).collect(toList());
    final List<String> toAdd = Arrays.stream(row2).parallel().sorted().filter(s -> !list1.contains(s)).collect(Collectors.toList());
    if (list1.get(list1.size() - 1) == "") {
        list1.set(list1.size() - 1, toAdd.get(0));
        return list1.toArray(new String[0]);
    }
    return new String[0];

}

Now you can call this per row you have untill the pre-last one.

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.