Java List Remove Duplicate renameDuplicateStrings(List strNames)

Here you can find the source of renameDuplicateStrings(List strNames)

Description

rename Duplicate Strings

License

Open Source License

Declaration

public static List<String> renameDuplicateStrings(List<String> strNames) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

public class Main {
    public static List<String> renameDuplicateStrings(List<String> strNames) {
        if (strNames == null) {
            return null;
        }/*from w  ww .j a  v a  2 s . c  om*/
        HashMap<String, Integer> seenBefore = new HashMap<String, Integer>(4);
        List<String> results = new ArrayList<String>(strNames.size());
        for (String str : strNames) {
            Integer lookup = seenBefore.get(str);
            if (lookup == null) {
                results.add(str);
                seenBefore.put(str, 1);
            } else {
                results.add(str + "_" + lookup);
                seenBefore.put(str, lookup + 1);
            }
        }
        return results;
    }
}

Related

  1. removeDuplicateWithOrder(List list)
  2. removeDups(List in)
  3. removeEqualItems(List list)
  4. removeRepeat(List list)
  5. removeRepeat(List list)