Java tutorial
//package com.java2s; //License from project: Apache License import java.util.Collection; import java.util.HashSet; import java.util.Set; public class Main { public static <T> T getFirstDuplicateValue(Collection<T> collection) { Set<T> set = new HashSet<T>(); // Set#add returns false if the set does not change, which // indicates that a duplicate element has been added. for (T each : collection) { if (!set.add(each)) return each; } return null; } }