class ExpiringSet {

    public boolean contains(final E key) {
        synchronized (set) {
            if (set.contains(key)) {
                return true;
            }
            set.add(key);   // not found, so add it
        }
        scheduler.schedule(new Runnable() {   // schedule automatic key removal
            @Override
            public void run() {
                synchronized (set) {
                    set.remove(key);
                }
            }
        }, expireSeconds, TimeUnit.SECONDS);
        return false;
    }

}

class ExpiringSetTest {

	@Test
	public void testAdd() throws InterruptedException {

		ExpiringSet<String> expiringSet = new ExpiringSet<String>(2);   // seconds
		assertFalse(expiringSet.contains("hello"));
		assertTrue(expiringSet.contains("hello"));
	}

}