求救关于list合并成map的问题

a8836246 2013-11-19 12:35:04
有4个string数组

s1={0,1}

s2={0,2}

s3={1,1}

s4={1,2}

list <string【】>={s1,s2,s3,s4}

现在想对list中的string数组的第一个元素相同的合并为map的key值,对string数组第二个元素组成新的数组,作为value值。

得到的map为

map<string,string【】>= {key=0 value={1,2},key=1 value={1,2}}
希望各位大神多帮帮忙。


...全文
155 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
a8836246 2013-11-19
  • 打赏
  • 举报
回复
2楼是对的,问题的关键是要用一个list装后面的string,多谢。
a8836246 2013-11-19
  • 打赏
  • 举报
回复
引用 2 楼 develop_design_level 的回复:

	public static void testMap() {

		List<String[]> list = createDatas();
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		for (String[] arr : list) {
			if (2 < arr.length) {
				throw new IllegalArgumentException("out of index");
			}
			String a = arr[0];
			String b = arr[1];
			if (!map.containsKey(a)) {
				List<String> tmpList = new ArrayList<String>();
				tmpList.add(b);
				map.put(a, tmpList);
			} else {
				map.get(a).add(b);
			}
		}

		Iterator<Entry<String, List<String>>> it = map.entrySet().iterator();
		while (it.hasNext()) {
			Entry<String, List<String>> entry = it.next();
			System.out.println(entry);
		}
		// ========= output=========
		// 1=[1, 2, 2, 2]
		// 0=[1, 2]

	}

	private static List<String[]> createDatas() {
		String[] s1 = { "0", "1" };

		String[] s2 = { "0", "2" };

		String[] s3 = { "1", "1" };

		String[] s4 = { "1", "2" };
		String[] s5 = { "1", "2" };
		String[] s6 = { "1", "2" };
		List<String[]> list = new ArrayList<String[]>();
		list.add(s1);
		list.add(s2);
		list.add(s3);
		list.add(s4);
		list.add(s5);
		list.add(s6);
		return list;
	}
2楼你好,感谢你的回复,但是我有个疑问。 我试试您的方法           
a8836246 2013-11-19
  • 打赏
  • 举报
回复
引用 1 楼 develop_design_level 的回复:
[quote=引用 楼主 a8836246 的回复:] 有4个string数组 s1={0,1} s2={0,2} s3={1,1} s4={1,2} list <string【】>={s1,s2,s3,s4} 现在想对list中的string数组的第一个元素相同的合并为map的key值,对string数组第二个元素组成新的数组,作为value值。 得到的map为 map<string,string【】>= {key=0 value={1,2},key=1 value={1,2}} 希望各位大神多帮帮忙。
哥哥,你给出的代码是Java的么?在IDE里面写你上面的代码不 报错?先好好整理一下你的代码,然后发出来,在提问。[/ quote] 实际的算法要比我举例要复杂很多,我只要用一个最简单的例子,让大家帮我想想怎么实现。
  • 打赏
  • 举报
回复

	public static void testMap() {

		List<String[]> list = createDatas();
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		for (String[] arr : list) {
			if (2 < arr.length) {
				throw new IllegalArgumentException("out of index");
			}
			String a = arr[0];
			String b = arr[1];
			if (!map.containsKey(a)) {
				List<String> tmpList = new ArrayList<String>();
				tmpList.add(b);
				map.put(a, tmpList);
			} else {
				map.get(a).add(b);
			}
		}

		Iterator<Entry<String, List<String>>> it = map.entrySet().iterator();
		while (it.hasNext()) {
			Entry<String, List<String>> entry = it.next();
			System.out.println(entry);
		}
		// ========= output=========
		// 1=[1, 2, 2, 2]
		// 0=[1, 2]

	}

	private static List<String[]> createDatas() {
		String[] s1 = { "0", "1" };

		String[] s2 = { "0", "2" };

		String[] s3 = { "1", "1" };

		String[] s4 = { "1", "2" };
		String[] s5 = { "1", "2" };
		String[] s6 = { "1", "2" };
		List<String[]> list = new ArrayList<String[]>();
		list.add(s1);
		list.add(s2);
		list.add(s3);
		list.add(s4);
		list.add(s5);
		list.add(s6);
		return list;
	}
  • 打赏
  • 举报
回复
引用 楼主 a8836246 的回复:
有4个string数组 s1={0,1} s2={0,2} s3={1,1} s4={1,2} list <string【】>={s1,s2,s3,s4} 现在想对list中的string数组的第一个元素相同的合并为map的key值,对string数组第二个元素组成新的数组,作为value值。 得到的map为 map<string,string【】>= {key=0 value={1,2},key=1 value={1,2}} 希望各位大神多帮帮忙。
哥哥,你给出的代码是Java的么?在IDE里面写你上面的代码不报错?先好好整理一下你的代码,然后发出来,在提问。

62,614

社区成员

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

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