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;
    }
    
  • 相关阅读:
    Vue's Demo
    安装informatic过程中的错误
    linux系统字符集
    netstat
    查看linux系统的信息
    以太坊私有链的搭建
    $0 $1
    WordPaster-Joomla_3.4.7-tinymce 4.1.7示例发布
    Joomla3x-CKEditor4x-WordPaster整合示例
    Firefox 43无法安装xpi的问题
  • 原文地址:https://www.cnblogs.com/sunBinary/p/12001850.html
Copyright © 2011-2022 走看看