zoukankan      html  css  js  c++  java
  • java里有没有专门判断List里有重复的数据

    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());
        }
    }
  • 相关阅读:
    des加密
    http请求报错
    js生成二维码(jquery自带)
    tomcat跨域请求
    jsp读取properties文件
    spring+mybatis整合读取不了配置文件
    svn提交报e200007错误
    firefox兼容性问题
    Oracle学习笔记(2)
    Spring设置定时器:quartz
  • 原文地址:https://www.cnblogs.com/cxxjohnson/p/5655890.html
Copyright © 2011-2022 走看看