当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

list中的数据根据类别进行分组的实现

发表于: 2011-08-12   作者:baalwolf   来源:转载   浏览:
摘要: public static void main(String[] args) { List studentAllList = new ArrayList();// 所以班級的学生 Article student1 = new Article(); student1.setArticleid(1); student1.setContent("1");
public static void main(String[] args) {

		List studentAllList = new ArrayList();// 所以班級的学生
		Article student1 = new Article();
		student1.setArticleid(1);
		student1.setContent("1");
		studentAllList.add(student1);
		Article student2 = new Article();
		student2.setArticleid(2);
		student2.setContent("1");
		studentAllList.add(student2);
		Article student3 = new Article();
		student3.setArticleid(3);
		student3.setContent("2");
		studentAllList.add(student3);
		Article student4 = new Article();
		student4.setArticleid(4);
		student4.setContent("1");
		studentAllList.add(student4);

		// 步骤1
		Map<String, List> map = new HashMap<String, List>();
		for (Iterator it = studentAllList.iterator(); it.hasNext();) {
			Article student = (Article) it.next();
			if (map.containsKey(student.getContent())) { // 如果已经存在这个数组,就放在这里
				List studentList = map.get(student.getContent());
				studentList.add(student);
				System.out.println("student.getContent()1111=="
						+ student.getContent());
			} else {
				List studentList = new ArrayList(); // 重新声明一个数组list
				studentList.add(student);
				map.put(student.getContent(), studentList);
				System.out.println("student.getContent()22222="
						+ student.getContent());
			}
		}
		Iterator iter = map.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String key = (String) entry.getKey();
			System.out.println("key==" + key);
			List val = (List) entry.getValue();
			for (int i = 0; i < val.size(); i++) {
				Article student = (Article) val.get(i);
				System.out.println(student.getContent() + "===="
						+ student.getArticleid());
			}
		}

	}

 

list中的数据根据类别进行分组的实现

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号