A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© TomZhou 中级黑马   /  2016-6-3 21:41  /  701 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

         *在当前项目根目录下有一个“qq.txt文件”里面存放的内容如下:
(项目根目录,假设qq号的长度最大为11位)
        12345
        67891
        12347809933
        98765432102
        67891
        12347809933       
        a.将该文件里面的所有qq号都存放在list中
        b.将list中重复元素删除                                  
        c.将剩余元素进行排序(按照长度由小到大)                                 
        d.将list中所有元素用两种方式打印出来
         * @throws IOException
         * @throws IOException
         * @throws ClassNotFoundException

         */

        public static void main(String[] args) throws IOException, ClassNotFoundException {
                List<String> ls = new ArrayList<>();
                BufferedReader br = new BufferedReader(new FileReader("qq.txt"));
                String line;
                while ((line = br.readLine()) != null) {
                        ls.add(line);
                }
                //去重复前
               
                getSingle(ls);
                System.out.println(ls);
                sort(ls);
                //去重复后
                for (String string : ls) {
                        System.out.println(string);
                }
        }

        private static void sort(List<String> ls) {
                TreeSet<String> ts = new TreeSet<>(new Comparator<String>() {

                        @Override
                        public int compare(String s1, String s2) {
                                int num = s1.length() - s2.length();
                                return num == 0 ? s1.compareTo(s2) : num;
                        }
                });
        }

        private static void getSingle(List<String> ls) {
                LinkedHashSet<String> lhs = new LinkedHashSet<>();
                lhs.addAll(ls);
                ls.clear();
                ls.addAll(lhs);
        }

1 个回复

倒序浏览
顶一个!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马