public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("aa"); list.add("bb"); list.add("cc"); list.add("dd"); list.add("bb"); list.add("ee"); list.add("dd"); list.add("ff"); String temp = ""; for (int i = 0; i < list.size() - 1; i++) { temp = list.get(i); for (int j = i + 1; j < list.size(); j++) { if (temp.equals(list.get(j))) { System.out.println("第" + (i + 1) + "个跟第" + (j + 1) + "个重复,值是:" + temp); } } } }
package testCollection; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import entity.Student; public class ListRepeatData { public static void main(String[] args) { test(); } public static void test() { List<Student> list = new ArrayList<Student>(); Student s1 = new Student("1", "cxx1"); Student s2 = new Student("2", "cxx2"); Student s3 = new Student("3", "cxx1"); Student s4 = new Student("4", "cxx2"); Student s5 = new Student("5", "cxx1"); Student s6 = new Student("6", "cxx2"); Student s7 = new Student("7", "cxx3"); list.add(s1); list.add(s2); list.add(s3); list.add(s4); list.add(s5); list.add(s6); list.add(s7); Map<String, List<String>> map = new HashMap<String, List<String>>(); for (Student s : list) { if (map.containsKey(s.getName())) { List<String> rList = map.get(s.getName()); rList.add(s.getId()); } else { List<String> rList = new ArrayList<String>(); rList.add(s.getId()); map.put(s.getName(), rList); } } System.out.println(map.toString()); } }