Azazel
Azazel

Reputation: 167

Java - Sort Alphabetically as well as collate unique elements together

I have a array list filled with objects which each have info about them. For Example :

{Term(C,2), Term(H,2), Term(C,1), Term(O,3)}

The goal right now is to change the arraylist so the result is something such as this :

{Term(C,3), Term(H,2), Term(O,3)}

We want to collate the like terms together. I have the following at this stage.

char element = 'A';
int amt_atom = 0;
int index = 0;

char alphabet = 'A';
char alpha = 'A';

for (; alpha <= 'Z' ; alpha++){
    search:
    for ( ; alphabet <= 'Z' ; alphabet++ ) {
        for ( int i = 0 ; i < terms.size() ; i++){
            if ( terms.get(i).getElement() == alphabet){
                element = terms.get(i).getElement();
                amt_atom = terms.get(i).getAtoms();
                index = i;


                System.out.println(element + " , " + amt_atom + " , " + index );
                alphabet++;
                break search;
            }
        }
    }

    for (int i = 0 ; i < terms.size() ; i++){
        if ( terms.get(i).getElement() == element && i != index){
            int amt_atoms2 = terms.get(i).getAtoms();
            terms.set(index, new Term ( element, amt_atom + amt_atoms2 ));
            terms.remove(i);
        }
    }
}

Also, the array given might not be alphabetic and we must also put it in alphabetic order. Another example would be: CH3CH2CH2CH2CH2CH3 and the result = C6H14.

Edit : Sorry, should have specified what was wrong with the original code. So, Doing C2H2HO3 results in C2H3O3which is fantastic because its right but when i try CH3CH2CH2CH2CH2CH3 then i get C2H5 which is not at all correct. I am not sure why that is .

Am I even on the right track? Could someone maybe tell me what i am doing wrong? I am fairly new to Java and therefore have an extremely basic knowledge so it would be good if you can explain something while using structures and elements of java that i might have used above. Thanks

Upvotes: 0

Views: 370

Answers (2)

Cortwave
Cortwave

Reputation: 4907

It's very easy do with Java 8:

//grouping by alphabet
Map<Character, List<Integer>> groupedTerms = terms.stream().collect(Collectors.groupingBy(Term::getElement)))
//Calculate count
Map<Character, Int> calculated = groupedTerms.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().stream().mapToInt(Integer::intValue).sum()));
//And sort
Map<Character, Int> result = calculated.stream().sorted(Map.Entry.byKeyComparator()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

As result you have sorted map Character - Int:

["A"-> 10, "C" -> 5]

Upvotes: 0

dvaergiller
dvaergiller

Reputation: 815

How about first sort, then collate? You could make your Term's implement the Comparable interface and then just call terms.sort();. Then go over the list and collate, which is easier once it's sorted.

Don't worry too much about performance here. It should be fast.

Upvotes: 3

Related Questions