Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Comparing two string arrays and removing elements that exist in both arrays

This is mainly a performance questions. I have a master list of all users existing in a String array AllUids. I also have a list of all end dated users existing in a String array EndUids.

I am working in Java and my goal is to remove any users that exist in the end dated array from the master list AllUids. I know PHP has a function called array_diff.

I was curious if Java has anything that will compare two arrays and remove elements that are similar in both. My objective is performance here which is why I asked about a built in function. I do not want to add any special packages.

I thought about writing a recursive function but it just seems like it will be inefficient. There are thousands of users in both lists. In order to exist in the end dated list, you must exist in the AllUids list, that is until removed.

Example:

String[] AllUids = {"Joe", "Tom", "Dan", "Bill", "Hector", "Ron"};

String[] EndUids = {"Dan", "Hector", "Ron"};

Functionality I am looking for:

String[] ActiveUids = AllUids.RemoveSimilar(EndUids);

ActiveUids would look like this:

{"Joe", "Tom", "Bill"}

Thank you all, Obviously I can come up with loops and such but I am not confident that it will be efficient. This is something that will run on production machines everyday.

like image 680
user84786 Avatar asked Aug 05 '09 18:08

user84786


2 Answers

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Bireswhar
 */
import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Repeated {

    public static void main(String[] args) {
//        Collection listOne = new ArrayList(Arrays.asList("milan","dingo", "elpha", "hafil", "meat", "iga", "neeta.peeta"));
//        Collection listTwo = new ArrayList(Arrays.asList("hafil", "iga", "binga", "mike", "dingo"));
//
//        listOne.retainAll( listTwo );
//        System.out.println( listOne );

        String[] s1 = {"ram", "raju", "seetha"};
        String[] s2 = {"ram"};
        List<String> s1List = new ArrayList(Arrays.asList(s1));
        for (String s : s2) {
            if (s1List.contains(s)) {
                s1List.remove(s);
            } else {
                s1List.add(s);
            }
             System.out.println("intersect on " + s1List);
        }
    }
}
like image 145
Bireshwar Avatar answered Sep 25 '22 21:09

Bireshwar


You can't "remove" elements from arrays. You can set them to null, but arrays are of fixed size.

You could use java.util.Set and removeAll to take one set away from another, but I'd prefer to use the Google Collections Library:

Set<String> allUids = Sets.newHashSet("Joe", "Tom", "Dan",
                                      "Bill", "Hector", "Ron");
Set<String> endUids = Sets.newHashSet("Dan", "Hector", "Ron");
Set<String> activeUids = Sets.difference(allUids, endUids);

That has a more functional feel to it.

like image 44
Jon Skeet Avatar answered Sep 24 '22 21:09

Jon Skeet