java week12

T1

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class T1 {
    public static void main(String[] args) {
        Book b1 = new Book(1000,"b1",30.5,"bjsxt");
        Book b1_1 = new Book(1000,"b1",30,"bjsxt");
        Book b2 = new Book(1000,"b2",50,"bjsxt");
        Book b3 = new Book(1001,"b3",30.5,"bjsxt");
        Book b4 = new Book(1002,"b4",30.5,"bjsxt");
        Book b5 = new Book(1003,"b5",50,"bjsxt");
        //Hashset存储和遍历
        List<Book> bookList= new ArrayList<Book>();
        bookList.add(b1);
        bookList.add(b1);
        bookList.add(b2);
        bookList.add(b3);
        bookList.add(b4);
        bookList.add(b5);
        bookList.add(b1_1);
        System.out.println("遍历输出hashset");
        System.out.println(bookList.size());
        for (Book book:bookList){
            System.out.println(book.toString());
        }
        //TreeSet存储和遍历
        Map<Integer,Book>bookMap = new HashMap<Integer, Book>();
        bookMap.put(b1.getId(),b1);
        bookMap.put(b1.getId(),b1);
        bookMap.put(b2.getId(),b2);
        bookMap.put(b3.getId(),b3);
        bookMap.put(b4.getId(),b4);
        bookMap.put(b5.getId(),b5);
        bookMap.put(b1_1.getId(),b1_1);
        System.out.println("遍历输出Treeset");
        for(Map.Entry<Integer,Book> entry:bookMap.entrySet()){
            System.out.println(entry.getKey()+"--------->"+entry.getValue());
        }
    }
}

class Book{
    public int id;
    public String name;
    public double price;
    public String press;

    public Book (int id,String name,double price,String press){
        this.id=id;
        this.name=name;
        this.price=price;
        this.press=press;
    }
    public int getId(){
        return id;
    }
    public void setId(int id){
        this.id = id;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name=name;
    }
    public double getPrice(){
        return price;
    }
    public void setPrice(double price){
        this.price=price;
    }
    public String getPress(){
        return press;
    }
    public void setPress(String press){
        this.press=press;
    }
    @Override
    public String toString(){
        return "Book[id=" + id + ",name=" + name + ",press=" + press + ",price="+price+"]";
    }
}

T2

import jdk.nashorn.internal.codegen.Compiler;

import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

class Student implements Comparable<Student>
{
    private Integer num;
    private String name;
    private Integer age;

    public Student(int num, String name, int age) {
        super();
        this.num=num;
        this.name=name;
        this.age=age;
    }

    //省略getter和setter和构造方法
    public int compareTo(Student stu){
        return this.age-stu.age;
    }
    public String toString(){
        return "Student [age="+age+",name="+name+",num="+num+"]";
    }
}

public class T2 {
    public static void main(String[] args) {
        //保存信息到set中
        Set<Student> stuSet = saveStudentInfo();
        //遍历set
        Iterator<Student> it=stuSet.iterator();
        while(it.hasNext()){
            String info = it.next().toString();
            System.out.println(info);
        }
    }
    private static Set<Student> saveStudentInfo(){
        Scanner input = new Scanner(System.in);
        //保存学生信息的TreeSet集合对象
        Set<Student> stuSet= new TreeSet<Student>();
        while(true){
            //输入提示
            System.out.println("请输入学生信息:(学号#姓名#年龄)");
            String inputData = input.nextLine();
            //判断是否退出 inputData。equals(“exit”)
            if ("exit".equals(inputData)){
                break;
            }
            //用户信息分割为String【】
            String[] info = inputData.split("#");

            Student stu
                    = new Student(Integer.parseInt(info[0]),info[1],Integer.parseInt(info[2]));
            stuSet.add(stu);
        }
        return stuSet;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值