Example: Couples with Common Ancestors

You can use gedcom4j to find couples in your gedcom that share common ancestors (e.g., married cousins).

The algorithm (once you've parsed the gedcom) is:

  1. Loop through all the families where we have both a husband and wife on record.
  2. Use a org.gedcom4j.relationship.AncestryCalculator instance to find nearest common ancestors between the two spouses.
  3. If the AncestryCalculator finds any common ancestors, display the husband, wife, and their nearest common ancestors.
package com.mattharrah.genealogy;
 
import java.util.Set;
 
import org.gedcom4j.model.Family;
import org.gedcom4j.model.Gedcom;
import org.gedcom4j.model.Individual;
import org.gedcom4j.parser.GedcomParser;
import org.gedcom4j.relationship.AncestryCalculator;
 
/**
 * Find all couples in the gedcom that have common ancestors
 * 
 * @author frizbog
 */
public class CouplesWithCommonAncestors {
 
    /**
     * Main class
     * 
     * @param args
     *            command line arguments
     * @throws Exception if anything goes wrong
     */
    public static void main(String[] args) throws Exception {
        AncestryCalculator ac = new AncestryCalculator();
 
        GedcomParser gp = new GedcomParser();
        gp.load("/Users/frizbog/Harrah.ged");
        Gedcom g = gp.getGedcom();
 
        for (Family f : g.getFamilies().values()) {
            // Only process families where we know both the husband and
            // the wife
            if (f.getHusband() != null && f.getWife() != null) {
                // Get the nearest common ancestor(s), if any, between
                // the husband and wife
                Set<Individual> common = ac.getLowestCommonAncestors(f
                        .getHusband().getIndividual(), f.getWife().getIndividual());
 
                if (!common.isEmpty()) {
                    // We have a couple with common ancestors. Show
                    // them.
                    System.out.println(f.getHusband().getIndividual().getFormattedName()
                            + " and " + f.getWife().getIndividual().getFormattedName()
                            + " have " + common.size()
                            + " common ancestor(s):");
                    int commonAncNumber = 0;
                    for (Individual i : common) {
                        commonAncNumber++;
                        System.out.println("\t" + commonAncNumber + ") "
                                + i);
                    }
 
                    System.out.println();
                }
            }
        }
    }
}

This produces output like the following:

Peter /Silvester/ and Deborah /Torrey/ have 2 common ancestor(s):
	1) Ann /Hatch/, spouse of James /Torrey/, child of Jane /Young/ and William /Hatch/, b.03 DEC 1626 / 1697
	2) James /Torrey/, spouse of Ann /Hatch/, child of Alice /Richards/ and Philip /Torrey/, b.BET 1612 AND 1613 / 06 JUL 1665

Thomas /Torrey/ and Elizabeth /Torrey/ have 2 common ancestor(s):
	1) Ann /Hatch/, spouse of James /Torrey/, child of Jane /Young/ and William /Hatch/, b.03 DEC 1626 / 1697
	2) James /Torrey/, spouse of Ann /Hatch/, child of Alice /Richards/ and Philip /Torrey/, b.BET 1612 AND 1613 / 06 JUL 1665