• 装箱拆箱排序comparactor


    package com.conatructor;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class Box {
        
        public static void main(String[] args)
        {    //装箱
            List<Apple> al = new ArrayList<Apple>();
            al.add(new Apple("app1",19.3,25.4));
            al.add(new Apple("app2",20.7,25.8));
            al.add(new Apple("app3",22.5,25.6));
            
            Collections.sort(al, new Comparator<Apple>() {
                @Override
            public int compare(Apple o1, Apple o2) {
                    double c =o1.getheight()-o2.getheight();
                    if(c>0)
                        return 1;//正序
                    else if(c<0)
                        return -1;//倒序
                    else 
                        return 0;
                }
            });
            try {
                ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream("D:/1.txt"));
                oo.writeObject(al);
                oo.flush();
                oo.close();    
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
            //拆箱
    //        List<Apple> read= null;
    //        try {
    //            ObjectInputStream oi = new ObjectInputStream(new FileInputStream("D:/1.txt"));
    //             try {
    //                read =(List<Apple>) oi.readObject();
    //            } catch (ClassNotFoundException e) {
    //                // TODO Auto-generated catch block
    //                e.printStackTrace();
    //            }
    //        } catch (FileNotFoundException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        } catch (IOException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
            for(Apple x:al)
            {
                System.out.println(x);
            }
        }
        }
  • 相关阅读:
    Xshell如何配置并远程连接Linux服务器详解
    VMware如何克隆一个虚拟机
    如何通过VMware安装Linux CentOS 7.7系统
    如何VMware创建Linux虚拟机并设置虚拟机网络
    自动化运维工具Ansible之LNMP实践环境部署
    自动化运维工具Ansible之Roles角色详解
    自动化运维工具Ansible之Tests测验详解
    Ansible Jinja2 模板使用
    Ansible playbook Vault 加密
    Ansible playbook 编程
  • 原文地址:https://www.cnblogs.com/kiskistian/p/7305015.html
Copyright © 2020-2023  润新知