zoukankan      html  css  js  c++  java
  • 装箱/拆箱 对象排序

    package one;
    
    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.Comparator;
    import java.util.List;
    
    public class Sort22  {
        public static void main(String[] args) throws FileNotFoundException, IOException {
            List<Apple> list = new ArrayList<Apple>();
    
            list.add(new Apple(1, "ehe", 1.1));
            list.add(new Apple(2, "ehe", 1.1));
            list.add(new Apple(3, "ehe", 1.1));
            list.add(new Apple(4, "ehe", 1.1));
            list.add(new Apple(5, "ehe", 1.1));
           //排序规则
            list.sort(new Comparator<Apple>() {
    
                @Override
                public int compare(Apple o1, Apple o2) {
                    return (o1.getId()-o2.getId())*(-1);
                }
            });
            System.out.println("*******************************************************************");
            System.out.println("对象排序");
            for (Apple x : list) {
                System.out.println(x);
            }
            System.out.println("********************************************************************");
            //装箱
            ObjectOutputStream oo= new ObjectOutputStream(new FileOutputStream("D:/1.txt"));//对象输出流
    
            oo.writeObject(list);
            oo.flush();
            oo.close();
            System.out.println("对象输出到文件");
            System.out.println("****************************************************************************");
    //-------------------------------------------------------------------------------------------------        
            //拆箱
            List <Apple> read=null;
            
            try {
                ObjectInputStream oi= new ObjectInputStream(new FileInputStream("D:/1.txt"));//输入流
                read =(List<Apple>)oi.readObject();
                
                for(Apple x:read){
                    
                    System.out.println(x);
                    
                }
                 oi.close();
                
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                                                                                                                    
        }
    }
  • 相关阅读:
    使用POI读取excel文件内容
    有序链表
    jQuery Validate验证框架详解
    怎样在VS2010中打开VS2012的项目
    在Win8.1系统下如何安装运行SQL Server 2005
    SQL2005 2008配置错误,无法识别的配置节 system.serviceModel machine.config配置文件有问题
    深入浅出学Spring Data JPA
    Java 学习摘要
    JFinal
    spring 4 + jpa(hibernate 3/4) + spring mvc 多数据源配置
  • 原文地址:https://www.cnblogs.com/-strong/p/7300875.html
Copyright © 2011-2022 走看看