1

I have a class, called ADVAObject, with a variable "type" and "value":

public class ADVAObject {
   String type = "";
   String value = "";
}

Then, I did some ArrayList<ADVAObject> pattern = new ArrayList<ADVAObject>();. After some processing, I ended up with this list using:

for(ADVAObject a : pattern) {
   System.out.println(a.type + " : " + a.value);
}

The list:

STRING : V
STRING : A
STRING : R
STRING : I
STRING : A
STRING : B
STRING : L
STRING : E
SPACE : null
SPECIAL : null
SPACE : null
NUMBER : 3
NUMBER : 4
SPACE : null
DIV : null
SPACE : null
NUMBER : 5
NUMBER : 6
SPACE : null
QUESTION : null

As you can see, instead of getting "VARIABLE", I get it separated. This isn't a problem, for now. "For now"? Yes. You see, I wanted to keep them separated to do additional processing, but now I need to "merge", as said in the title of this question, all repeated elements which have a value (which aren't null). So:

//This:
STRING : AB
STRING : CD
STRING : EF

//Turns into this:
STRING : ABCDEF

//However, ...:

//This:

SPACE : null
SPACE : null

//Can't turn into this:

SPACE : null

//.

I have tried creating a seperate array and regexing it all inside some loops, but it didn't... work as expected.

0

2 Answers 2

1

What you should look into, instead of an arraylist, could be a HashMap, with a linkedlist, or an arraylist as it's parameter as follows:

EDIT: Changed ArrayList<String> to ArrayList<ADVAObject> to align with the class you are using

HashMap<String, ArrayList<ADVAObject>> keyValues = new HashMap<>(); 

This way you could map each value even if there are duplicates, for example, STRING could be the key, and the values of that would be: ABCDEF.

Note: to handle the duplicate values seen, you could just have a conditional statement that only adds one of the duplicate values

Hope this helps :)

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

3 Comments

Hello, turns out, although better than what I had before, both your answers don't work, because A, they change the order of the elements, eg. instead of STRING : a; NUMBER: 7; DIV : null, it may change to DIV: null; STRING: a; NUMBER: 7. The second problem is, if I have NUMBER : 5; SPACE : NULL; NUMBER : 5, I don't want to combine the first NUMBER with the second NUMBER, because they are separated by a SPACE.
those are things you can handle with conditional statements like my answer was saying, for the sorting you can do something like this: geeksforgeeks.org/sorting-hashmap-according-key-value-java
and with the numbers, you could say "if there is a space between them, count them as seperate entries"
0

You can do some simple List processing with a for loop. See the below code in action here.

final List<ADVAObject> result = new ArrayList<>(pattern.size());
for (int i = 0, size = pattern.size(); i < size; i++) {
    final ADVAObject curr = pattern.get(i);
    if (i == 0 || !pattern.get(i - 1).type.equals(curr.type)) {
        result.add(curr);
    } else {
        result.get(result.size() - 1).value += curr.value;
    }
}

2 Comments

Don't know if this is quite what he's asking for, he wants one value, to be pointed to many other values, that's why I suggested using a HashMap with a string as the key, and it assigned to an arraylist of strings
Hello, turns out, although better than what I had before, both your answers don't work, because A, they change the order of the elements, eg. instead of STRING : a; NUMBER: 7; DIV : null, it may change to DIV: null; STRING: a; NUMBER: 7. The second problem is, if I have NUMBER : 5; SPACE : NULL; NUMBER : 5, I don't want to combine the first NUMBER with the second NUMBER, because they are separated by a SPACE.

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.