1

I have an ArrayList of Strings with duplicate elements. Every 3 rows make an entity:

2
550111.55
1, 3
3
550111.55
1, 2, 3
2
155.55
1, 2
3
550111.55
2, 3, 1
2
155.55
2, 1
2
550111.55
3, 1
3
550111.55
3, 1, 2

All the elements are Strings. The first number says the number of participants, the second says the value, and the third which elements make it up. So I have duplicates like

3
550111.55
3, 1, 2

and

3
550111.55
1, 2, 3

and

3
550111.55
2, 3, 1

The number of elements says the number of duplicates, so I must remove all the duplicates, in this case, delete all but one instance, for example:

3
550111.55
2, 3, 1

I am trying to do it like this :

ArrayList<String> lista_rezultata1=new ArrayList<String>();
lista_rezultata1=lista_rezultata;
//set to count to 3 in arraylist since data I need to split and evaluate are always on the third place
int number1=0;
int number2=0;
String[] part1=null;
String[] part2=null;
int removefromlist=0;
for (int i = 0; i < lista_rezultata.size(); i++) {
    if(number1==3)number1=0;
    if(number1==2)//If it is a third element it is the one with "," that I split
    {
        part1=lista_rezultata.get(i).toString().split(",");
    }

    for (int j = 0; j < lista_rezultata1.size(); j++) {
        if(number2==3)number2=0; 

        if(number2==2 && i!=j && number1==2)//If it is a third element it is the one with "," that I split, first must already be splited
        {
            part2=lista_rezultata1.get(j).toString().split(",");

            for (int k = 0; k < part1.length; k++) {
                for (int l = 0; l < part2.length; l++) {
                    if(part1.length==part2.length)   //if they have the same number of elements
                    { 
                        if(part2[l].equals(part1[j]))
                        {
                            .....some code
                        }
                    }
                }
            }
        }

        number2=number2+1;   
    }
    number1=number1+1; 
}

So I am hoping there is a better way ...

3 Answers 3

3

Your approach is all wrong I'm afraid...

  • Create a class to represent your items
  • Override the equals() method that returns true if your item values are the same
  • Override the hashCode() method to produce an int value based on the same values you compared in your equals() method (so hashCode "agrees with" equals)
  • Use a Set<Item> to save your items - sets use equals() to ensure there are no duplicates

You may wish to use a LinkedHashSet for you Set implementation:

Set<Item> set = new LinkedHashSet<Item>();

A LinkedHashSet will ensure the iteration order of its elements in the same order they were inserted.

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

2 Comments

You are right about the classes but what am I to do with Strings that go like 1,2,3 and 2,3,1 how am I to present these and do not mess up the order in which they go...
Use a LinkedHashSet, which iterate in inserted order
0

If the elements parts had their members in order, then you would have actual duplicates, which should simplify things.

1 Comment

It is importand that they are kept in the same order if I have 1,2,3 then 2,3,1 and 3,1,2 it is important that I am left with at lest one combination and they must be within same entity - the group of 3 , first is the number of participants , second is the value and the third are the ones that make the circle , these are in a fact results from graph database ...
0

You you should probably use ArrayLists instead. However even if you prefer not to, you can check if an array contains a certain element like this:

Arrays.asList(yourArray).contains(element);

1 Comment

I am splitting the String 1,2,3 it may be that there is a combination like 1,2,3,4 and it is not the same ...It does contain it , all the elements but it is not for removing..

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.