zoukankan      html  css  js  c++  java
  • 查找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;
    }
    
  • 相关阅读:
    一些经验
    倍增(在线)求LCA
    IDA*算法——骑士精神
    A*算法——第K短路
    (持续更新)一些黑科技和技巧
    逆元
    方便人类——信息学训练专用库
    PHP单点登陆
    PHP 中运用 elasticsearch
    PHP斐波那契数列
  • 原文地址:https://www.cnblogs.com/sunBinary/p/12001850.html
Copyright © 2011-2022 走看看