It seems you could use a map for your use case:
Map<String, A> map = new HashMap<> ();
and put items in the map like this:
map.put(someA.s, someA);
That should turn your O(n^2) algoritm into an O(n) algorithm.
When you receive a newA
, you can use the following:
A a = map.get(newA.getS());
if (a == null) {
map.put(newA.getS(), newA); //new string => new item in the map
} else {
a.setD(a.getD() + newA.getD()); //found string => updating the existing item
}