list元素合并的问题

wangruizhi_11 2014-05-05 02:02:28

List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
list.add("1");
list.add("2");
list.add("1");
list.add("4");
list.add("5");
相同的元素合并,想要的结果: [1,1,1],[2,2],[3],[4],[5](存到一个新list中)
小弟新手,不知道如何实现
...全文
179 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangruizhi_11 2014-05-05
  • 打赏
  • 举报
回复
感谢各位帮忙
linhua11 2014-05-05
  • 打赏
  • 举报
回复
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<String> input = new ArrayList<String>();
        input.add("1");
        input.add("2");
        input.add("3");
        input.add("1");
        input.add("2");
        input.add("1");
        input.add("4");
        input.add("5");

        List<List<String>> result = new ArrayList<List<String>>();
        for (String key : input) {
            boolean found = false;
            for (List<String> resultList : result) {
                if (resultList.get(0).equals(key)) {
                    resultList.add(key);
                    found = true;
                }
            }
            if (!found) {
                List<String> lst = new ArrayList<String>();
                lst.add(key);
                result.add(lst);
            }
        }

        System.out.println(result);
    }

}
wangruizhi_11 2014-05-05
  • 打赏
  • 举报
回复
不用map 用for循环之类的。。。
wangruizhi_11 2014-05-05
  • 打赏
  • 举报
回复
若是用list遍历?要如何实现?
tony4geek 2014-05-05
  • 打赏
  • 举报
回复
楼上的可以不
reentrantlock 2014-05-05
  • 打赏
  • 举报
回复
Step1: create a map: the key is unduplicated value, the value is a list with duplicated value. Step2: fill in this map by iterating that list. Step3: iterating the map to generate a new list which will keep the same with your requirement.
linhua11 2014-05-05
  • 打赏
  • 举报
回复
import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.TreeMap; public class Test { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); list.add("1"); list.add("2"); list.add("1"); list.add("4"); list.add("5"); System.out.println(test(list)); } public static List<List<String>> test(List<String> input) { Map<String, List<String>> tmpMap = new TreeMap<String, List<String>>(); for (String key : input) { if (!tmpMap.containsKey(key)) { tmpMap.put(key, new ArrayList<String>()); } tmpMap.get(key).add(key); } return new ArrayList<List<String>>(tmpMap.values()); } }
Prayszzz 2014-05-05
  • 打赏
  • 举报
回复
用HashMap很容易实现
Prayszzz 2014-05-05
  • 打赏
  • 举报
回复
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Demo {
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("1");
		list.add("2");
		list.add("3");
		list.add("1");
		list.add("2");
		list.add("1");
		list.add("4");
		list.add("5");
		
		Map<String,List<String>> map = 
			new HashMap<String, List<String>>();
		
		for(String s : list){
			List<String> value = map.get(s);
			if(value == null){
				List<String> temp = new ArrayList<String>();
				temp.add(s);
				map.put(s, temp);
			}
			else{
				value.add(s);
			}
		}
		
		System.out.println(map);
	}
}
soyestrellafortuna 2014-05-05
  • 打赏
  • 举报
回复
1.先将list中的数据放置到map中。 2.遍历map,相同的value放置在一个list中

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧