求:Java实现下面的算法,要求尽量高的性能

算法要求:把下面两个list:

columnNames list: ["Comment", "Comment", "Cost", "Term", "Cost"]

columnValues list: ["my comment 1", "my comment 2", "my cost 1", "my term", "my cost 2"]

变成:

columnNames list: ["Comment", "Cost", "Term"]

columnValues list: [["my comment 1", "my comment 2"], ["my cost 1", "my cost 2"], "my test"]

其中columnNames list中的元素Comment和Comment是重复的,要求新的List里面不能重复。

谢谢!

13个回答

用hashtable,Names直接把字符串作为key,值无所谓。
Values,将字符串去掉数字作为Key,value是ArrayList,并且把自身添加进去。

看我下面代码, 狗服务到位不 .
List columnNames = Arrays.asList(new String[] {"Comment", "Comment", "Cost", "Term", "Cost"}) ;
List columnValues = Arrays.asList(new String[]{"my comment 1", "my comment 2", "my cost 1", "my term", "my cost 2"}) ;

    Map<String, List<String>> map = new HashMap<String, List<String>>() ;
    for (int x = 0; x < columnNames.size(); x++) {
        List<String> valueList = map.get(columnNames.get(x));
        if (valueList == null) {
            valueList = new ArrayList<String>();
            map.put(columnNames.get(x), valueList);
        }
        valueList.add(columnValues.get(x));
        map.put(columnNames.get(x), valueList);
    }

    List<String> newColumnNames = new ArrayList<String>() ;
    List<String> newColumnValues = new ArrayList<String>() ;

    for(Entry<String, List<String>> list:map.entrySet()) {
        newColumnNames.add(list.getKey()) ;
        newColumnValues.add(Arrays.toString(list.getValue().toArray())) ;
    }

    System.out.println(JSON.toJSONStringWithDateFormat(map, "yyyy-MM-dd hh:mm:ss"));
    System.out.println(JSON.toJSONStringWithDateFormat(newColumnNames, "yyyy-MM-dd hh:mm:ss"));
    System.out.println(JSON.toJSONStringWithDateFormat(newColumnValues, "yyyy-MM-dd hh:mm:ss"));

yongxu_16
yongxu_16 {"Comment":["my comment 1","my comment 2"],"Cost":["my cost 1","my cost 2"],"Term":["my term"]} ["Cost","Comment","Term"] ["[my cost 1, my cost 2]","[my comment 1, my comment 2]","[my term]"]
接近 5 年之前 回复
yongxu_16
yongxu_16 补充一下 打印结果
接近 5 年之前 回复

这个应该是字符串归类吧,相同字符串舍去?可以描述一下要求是什么么?

我想要的结果是:
key: "Comment", value: ["my comment 1", "my comment 2"]
key: "Cost", value: ["my cost 1", "my cost 2"]
key: "Term", value: "my test"

但用算法怎样实现呢?楼上的说了等于没说啊

自己搞定:
List columnNames = new ArrayList();
columnNames.add("Comment");
columnNames.add("Comment");
columnNames.add("Cost");
columnNames.add("Test");
columnNames.add("Cost");
List columnValues = new ArrayList();
columnValues.add("my comment 1");
columnValues.add("my comment 2");
columnValues.add("my cost 1");
columnValues.add("my test");
columnValues.add("my cost 2");

    Map map = new HashMap();
    for (int i = 0; i < columnNames.size(); i++) {
        boolean duplicatedColumn = false;
        String columnName = columnNames.get(i);
        if (map.keySet().contains(columnName)) {
            continue;
        }
        List list = new ArrayList();
        list.add(columnValues.get(i));
        for (int j = columnNames.size() - 1; j > i; j--) {
            if (columnNames.get(j).equals(columnNames.get(i))) {
                list.add(columnValues.get(j));
                map.put(columnName, list);
                duplicatedColumn = true;
            }
        }
        if (!duplicatedColumn) {
            map.put(columnName, columnValues.get(i));
        }
    }
    System.out.print(map);

楼上写的太啰嗦了, 还嵌套循环....

Map> map = new HashMap>() ; // 定义map
for (int x = 0; x < columnNames.size(); x++) {
List valueList = map.get(columnNames.get(x)) ;
if(valueList == null) {
valueList = new ArrayList () ;
}
valueList.add(columnValues.get(x)) ;
map.put(columnNames.get(x), valueList) ;
}


Map> map,定义这样的map 进行操作

定义如下map

Map<String,Set<String>> map 

首先这2个数组的长度是一样的,那么可以使用google guava 中的 ArrayListMultimap 类实现你的需求。至于速度么,你可以测试下!


com.google.guava
guava
18.0

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import java.util.*;

public static void main(String[] args) {
        List<String> keys = Lists.newArrayList("Comment", "Comment", "Cost", "Term", "Cost");
        List<String> values = Lists.newArrayList("my comment 1", "my comment 2", "my cost 1", "my term", "my cost 2");

        ArrayListMultimap<String, String> key4MultiValues = ArrayListMultimap.create();

        if (keys.size() == values.size()) {
            for (int i = 0; i < keys.size(); i++) {
                key4MultiValues.put(keys.get(i), values.get(i));
            }
        }
        Map<String, Collection<String>> asMap = key4MultiValues.asMap();

        System.out.println(asMap);

    }

输出

{Cost=[my cost 1, my cost 2], Comment=[my comment 1, my comment 2], Term=[my term]}

public static void main(String[] args) {
List keys = Arrays.asList("Comment", "Comment", "Cost", "Term", "Cost");
List values = Arrays.asList("my comment 1", "my comment 2", "my cost 1", "my term", "my cost 2");

     HashMap<String, List<String>> map = new HashMap<String, List<String>>();

     for (int i =0;i<keys.size();i++) {
         final String key = keys.get(i); 
         List<String> value = map.get(key);
         if(value==null){
             value = new ArrayList<String>();
             map.put(key, value);
         }
         value.add(values.get(i));
    }
     System.out.println(map.toString());
}
共13条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问