0

What I have is an array of String objects called Id, I'm then looping through the Id objects to get the email address associated with that Id (then concatenate them). I now want to remove the String from the array that only has a duplicate email address (ignore by looking at the Id in the String).

For example the String array contains the following Id objects:

{1, 2, 3}

Now I'm concatenating the email address associated with that Id, the String array becomes:

{1 - [email protected], 2 - [email protected], 3 - [email protected]}

So I need to remove duplicate emails (along with the Id concatenated to it) to give me this:

{1 - [email protected], 2 - [email protected]}

Then after that I will remove the email address by using split to give me the final result of:

{1, 2}

So the issue I'm having is, it off course look for the whole string including email and Id, but I need only to look for email address then remove the whole `String from the array.

I've done this bit of code:

//Remove all duplicate email addresses from list
ArrayList<String> duplicateEmails = new ArrayList<String>();

//looping through cform.getConsumers which is a String[] array of Id's, finding email address of that Id and concatenate it and add to the array list
for (String conId : cform.getToConsumers()){
    Long consId = Long.parseLong(conId);
    Consumer cons = af.getSingleConsumerId(consId);
    duplicateEmails.add(conId + " - " + cons.getEmail());
}

//convert arraylist to String array
String[] stringArray = duplicateEmails.toArray(new String[0]);
//remove the duplicates
Set<String> findDuplicates = new HashSet<String>(Arrays.asList(stringArray));
String[] removedEmails = findDuplicates.toArray(new String[0]);
3
  • Why don't you use LinkedHashMap<ArrayList<String>,String> Commented Jun 21, 2016 at 8:59
  • Seems like the end result of this function is just a list of duplicate emails or a list of ids that belong to duplicate emails. Why not add every email to a list and before adding it check if it's already in that list. If it is already in the list add the id or the email to a separate list containing only the duplicates. Commented Jun 21, 2016 at 9:10
  • I have not worked with linkedhashmap before, can you give me an example of it please? Commented Jun 21, 2016 at 10:00

3 Answers 3

2

You can do like following :

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

public class Testing {

    public static void main(String[] args) {
        HashMap<String, String> hm = new LinkedHashMap<String, String>();

        String[] values = {"1","2","3"};
        ArrayList<String> ar = new ArrayList<String>();
        ar.add("[email protected]");
        ar.add("[email protected]");
        ar.add("[email protected]");


        for (int i = 0; i<ar.size();i++) {

            if (!hm.containsKey(ar.get(i)))
                hm.put(ar.get(i), values[i]);
        } 
        System.out.println(hm);

    }

}

Output :

[1, 2]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mandeep, exactly what I was looking for! Going to try it out now.
2

Instead of writing Strings to the array, make an array of your own objects. Example:

class MyPair{
    String id;
    String email;
    public String toString(){
        return id + " - " + email;
    }
}

Then you can compare emails without parsing the whole String again.

Full example with overriding hashCode() and equals(Object):

package main;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        new Test().deduplicate();
    }

    public class MyPair {
         public final String id;
            public final String email;

            public MyPair(String id, String email) {
                super();
                this.id = id;
                this.email = email;
            }

            public String toString(){
                return id + " - " + email;
            }

            @Override
            public boolean equals(Object obj){
                if (!(obj instanceof MyPair))
                    return false;
                if (obj == this)
                    return true;
                return this.email.equals(((MyPair)obj).email);
            }
            @Override
            public int hashCode(){
                return this.email.hashCode();
            }
    }

    public void deduplicate() {
        List<MyPair> pairs = new ArrayList<MyPair>();

        // test data
        MyPair p1 = new MyPair("1", "[email protected]");
        MyPair p2 = new MyPair("2", "[email protected]");
        MyPair p3 = new MyPair("3", "[email protected]");
        pairs.add(p1);
        pairs.add(p2);
        pairs.add(p3);

        // just to demonstrate the overridden methods
        System.out.println(p1.equals(p2));
        System.out.println(p1.equals(p3));
        System.out.println(p2.equals(p3));
        System.out.println(p1.hashCode());
        System.out.println(p2.hashCode());
        System.out.println(p3.hashCode());

        // dedup will be done by HashSet
        // This only works because we have overridden
        // hashCode and equals!
        HashSet<MyPair> deduped = new HashSet<MyPair>();
        for (MyPair pair : pairs) {
            deduped.add(pair);
        }

        System.out.println(deduped);

    }
}

Output: [2 - [email protected], 1 - [email protected]] (note the changed order! This happens because of the hashing)

2 Comments

Thanks for this idea! Just trying to understand how would you compare the email string inside the object? Can you give an example?
I added a full example. Mandeeps solution is less code. My code is the full OO-approach :)
1

Why not using 3 arrays in a way that you keep the indexes for the duplicated eMails in the third array. And then go through the first (ID's) Array and remove those indexes from it.

So you have ID array, eMail Array, Indexes to remove array.

Then release those arrays from memory :)

In that way you dont need to concat strings and then search strings

Just an idea.

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.