首页 > 代码库 > 0926-----homework(4,5,6)

0926-----homework(4,5,6)

技术分享

  1 /**  2   *homework0926  3   *@author:kai li  4   */  5 package com.kai.li.homework0926;  6 import java.util.List;  7 import java.util.ArrayList;  8 import java.util.Arrays;  9 import java.util.Collections; 10 import java.util.Random; 11 import java.util.Map; 12 import java.util.HashMap; 13 import static java.util.stream.Collectors.toMap; 14 /** 15   *following class is client  16   */ 17 public class HomeWork0926{ 18     public static void main(String[] args)throws Exception{ 19          20         /** 21           *question four 22           */ 23      24     List<String> strList=new ArrayList<String>(); 25     strList.add("hello"); 26     strList.add("world"); 27     strList.add("learn"); 28     Collections.reverse(strList); 29     System.out.println(strList);   30  31     /** 32         *question five 33       */    34      35     /*create data source*/ 36     List<Account> accounts=Arrays.asList(new Account(10.00,"1234"),new Account(15.00,"5678"),new Account(0.00,"1010")); 37     Map<Long,Account> accountMap=new HashMap<>(); 38     accountMap=accounts.stream().collect(toMap(i->i.getId(),i->i)); 39     System.out.println(accountMap); 40  41     /** 42       *question six 43       */ 44      45     /*creat data source*/ 46  47     List<Worker> workerList=new ArrayList<>(); 48     workerList.add(new Worker("zhang3",18,3000)); 49     workerList.add(new Worker("li4",25,3500)); 50     workerList.add(new Worker("wang5",22,3200)); 51  52     /*operate*/ 53      54     workerList.add(1,new Worker("zhao6",24,3300)); 55     workerList.remove(3); 56      57     //iterator 58     workerList.stream().forEach(System.out::println); 59     workerList.stream().forEach(Worker::work); 60     } 61 } 62  63 /** 64   *class for question five 65   */ 66 class Account{ 67     private long id; 68     private double balance; 69     private String password; 70     Account(double balance,String password){ 71         Random random=new Random(); 72         this.id=random.nextInt(100000)+100000; 73         this.balance=balance; 74         this.password=password; 75     } 76     public long getId(){ 77         return id; 78     } 79     public double getBalance(){ 80         return balance; 81     } 82     public String getPassword(){ 83         return password; 84     } 85     @Override 86     public String toString(){ 87         return "id:"+id+",balance:"+balance+"\n\r"; 88     } 89 } 90  91 /** 92   *class for question six 93   */ 94 class Worker{ 95     private int age; 96     private String name; 97     private double salary; 98     public Worker(){} 99     public Worker(String name,int age,double salary){100         this.name=name;101         this.age=age;102         this.salary=salary;103     }104     public int getAge(){105         return age;106     }107     public String getName(){108         return name;109     }110     public double getSalary(){111         return salary;112     }113     @Override114     public String toString(){115         return "name:"+name+",age:"+age+",salary:"+salary;116     }117     public void work(){118         System.out.println(name+" work ");119     }120 }

 

0926-----homework(4,5,6)