Example: Finding Unrelated Individuals


This is an example of using gedcom4j to find and list out people who are unrelated to an individual, so they can be removed and time can be saved by not researching those unrelated people any further.


I actually used this program to help prune unnecessary branches from my tree. I decided that I wanted to stop going down rabbit holes for people who are unrelated to me. For example, I have an uncle "Jack" who is married to a woman named "Mary". Mary is only related to me through Jack's marriage, and her ancestry is not really important to me. But I had records about her grandparents, siblings, aunts, uncles, nieces, nephews, etc. I'm pretty sure they got into my database in the first place through merging other files into my tree without being selective.

So, I wrote this to find a single person of interest, go up the tree finding all the direct ancestors of that person (parents, parents of parents, etc), and then back down the tree finding all the spouses and descendants of all the ancestors, building a large list of relatives. Then I go through the entire GEDCOM and if they are not in the list of relatives, print them out so I can decide whether or not to delete that person from my tree.

Of course, you may not agree with my definition of what a related person is, but this program shows that the tool is capable of helping you discover things about your database that your regular UI tool might not.

package com.mattharrah.genealogy;
import java.io.IOException;
import java.util.*;
import org.gedcom4j.comparators.IndividualByLastNameFirstNameComparator;
import org.gedcom4j.exception.GedcomParserException;
import org.gedcom4j.model.Gedcom;
import org.gedcom4j.model.Individual;
import org.gedcom4j.parser.GedcomParser;
import org.gedcom4j.query.Finder;
public class NotRelated {
    public static void main(String[] args) throws IOException,
            GedcomParserException {
        // Load the file
        GedcomParser p = new GedcomParser();
        Gedcom g = p.getGedcom();
        // Find the main person of interest
        Finder f = new Finder(g);
        List<Individual> found = f.findByName("Harrah",
                "Matthew Robert");
        Individual personOfInterest = found.iterator().next();
        // Building list of people related to person of interest.
        // Start with all the person's ancestors
        Set<Individual> bigList = personOfInterest.getAncestors();
        // Add all the spouses and descendants of everyone in the list so far
        int count = 0;
        // Keep adding the spouses and descendants until the list stops growing
        while (bigList.size() > count) {
            count = bigList.size(); // This is the size of the list before
                                    // possible adding to it
            // Make a copy to prevent concurrent modification exceptions
            Set<Individual> family = new HashSet<Individual>(bigList);
            // For everyone in the list, add their spouses and their descendants
            for (Individual i : family) {
        // At this point, bigList contains a list of all the relatives of
        // personOfInterest
        // Get a list of everyone and sort them last name first
        List<Individual> everybody = new ArrayList<Individual>(g
                new IndividualByLastNameFirstNameComparator());
        // Now go through the list of everybody, and write out their names if
        // they are not in the list of relatives of the person of interest
        int k = 0;
        for (Individual i : everybody) {
            if (!bigList.contains(i)) {
        System.out.println(k + " name(s)");