Here you can find the source of generateUniqueName(String aName, Collection
Parameter | Description |
---|---|
aName | a parameter |
aStringCollection | a parameter |
public static String generateUniqueName(String aName, Collection<String> aStringCollection)
//package com.java2s; import java.util.*; public class Main { /**// w ww . ja v a 2 s .c o m * Generates a unique name for the child. * * @param aName * @param aStringCollection */ public static String generateUniqueName(String aName, Collection<String> aStringCollection) { return generateUniqueName(aName, aStringCollection, false); } /** * Generates a unique name for the child. * * @param aPrefix * @param aStringCollection * @param aPreserveIfAlreadyUnique */ public static String generateUniqueName(String aPrefix, Collection<String> aStringCollection, boolean aPreserveIfAlreadyUnique) { Set<String> set = new HashSet<>(); set.addAll(aStringCollection); if (aPreserveIfAlreadyUnique && !set.contains(aPrefix)) return aPrefix; int counter = 1; while (set.contains(aPrefix + String.valueOf(counter))) counter++; return aPrefix + counter; } }