查找list中的重复数据,并得到不重复数据索引位置
public static void main(String[] args) { List<String> strLs = new ArrayList<String>(); strLs.add("香蕉"); strLs.add("苹果"); strLs.add("苹果"); strLs.add("橘子"); strLs.add("橘子"); strLs.add("橘子"); strLs.add("哈密瓜"); strLs.add("西瓜"); same(strLs); } public static List<Integer> same(List<String> list) { 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(); List<Integer> list2 = new ArrayList<>(); StringBuffer buffer = new StringBuffer(); while (it.hasNext()) { String key = it.next(); String value = map.get(key); if (value.indexOf(",") == -1) { System.out.println(key + "不重复,行: " + value); buffer.append(value).append(","); } } String string = buffer.deleteCharAt(buffer.length()-1).toString(); String[] split = string.split(","); for (String string2 : split) { list2.add(Integer.parseInt(string2.trim())); } Collections.sort(list2); return list2; }
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25