java finds duplicate data instance details in list

  • 2020-06-03 06:22:05
  • OfStack

java finds duplicate data instance details in list

Requirements:

Find all duplicate data in an List set, which may be more than one heap, such as aa, bb, aa, bb, cc, dd, aa. If there are duplicates, number them and change to aa1, bb1, aa2, bb2, cc, dd.

The algorithm is as follows:


public static void same(List<String> list) {
    String [] indexArr ;
    Map<String, String> map = new HashMap<String, String>();
    for (int i = 0; i < list.size(); i++) {
      String key = list.get(i);
      String old = map.get(key);
      if (old != null) {
        map.put(key, old + "," + (i + 1));
      } else {
        map.put(key, "" + (i + 1));
      }
    }
    Iterator<String> it = map.keySet().iterator();
    int index = -1;
    while (it.hasNext()) {
      String key = it.next();
      String value = map.get(key);
      if (value.indexOf(",") != -1) {
        System.out.println(key + "  repeat , Line:  " + value);
        indexArr = value.split(",");

        for (int i = 0; i < indexArr.length; i++) {
          index = Integer.parseInt(indexArr[i])-1;
          list.set(index, list.get(index)+(1+i));
        }
      }
    }

    for (String val : list) {
      System.out.println("val = "+val);
    }
    System.out.println("..................");

  }

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: