day16集合的使用

  1. 使⽤LinkedList模拟栈
public static void main(String[] args) {
        LinkedList<String> strings = new LinkedList<>();
        strings.push("1");
        strings.push("2");
        strings.push("3");
        System.out.println(strings);
        while (strings.size()!=0){
            System.out.print(strings.removeLast()+" ");
        }
    }
  1. 【中】设计⼀个⽅法,随机⽣成10个不重复的10以内的数字,存⼊⼀个List集 合。
public static List test2(int count){
        Random random = new Random();
        ArrayList<Integer> list = new ArrayList<>();
        int t;
        while (list.size()!=count){
            t = random.nextInt(10);
            if(!list.contains(t))
                list.add(t);
        }
        return list;
    }
  1. 【中】程序设计:图书管理器,设计⼀个图书管理器,实现对图书进⾏的存储管 理操作,实现功能
    1.添加⼀本图书(书名、作者(姓名,年龄,性别)、售价)
    2.删除⼀本图书(通过书名删除)
    3.删除所有的指定作者的书(通过作者姓名删除)
    4.将所有的图书按照图书售价降序排序。若售价相同,按照作者年龄升序)

    图书类

    public class Book {
        private String bookName;
        private Author author;
        private Double price;
    
        public Book() {
        }
    
        public Book(String bookName, Author author, Double price) {
            this.bookName = bookName;
            this.author = author;
            this.price = price;
        }
    
        public String getBookName() {
            return bookName;
        }
    
        public void setBookName(String bookName) {
            this.bookName = bookName;
        }
    
        public Author getAuthor() {
            return author;
        }
    
        public void setAuthor(Author author) {
            this.author = author;
        }
    
        public Double getPrice() {
            return price;
        }
    
        public void setPrice(Double price) {
            this.price = price;
        }
    
        @Override
        public String toString() {
            return "Book{" +
                    "bookName='" + bookName + '\'' +
                    ", author=" + author +
                    ", price=" + price +
                    '}';
        }
    }
    
    

    作者类

    public class Author {
        private String name;
        private int age;
        private String sex;
    
        public Author() {
        }
        public Author(String name, int age, String sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        @Override
        public String toString() {
            return "Author{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", sex='" + sex + '\'' +
                    '}';
        }
    }
    
    

    图书管理器类

    public class LibraryManagement {
        private List<Book> books;
    
        public LibraryManagement(List<Book> books) {
            this.books = books;
        }
    
        public LibraryManagement() {
        }
    
        public List<Book> getBooks() {
            return books;
        }
    
        public void setBooks(List<Book> books) {
            this.books = books;
        }
    
        public boolean deleteByName(String name){
            if(books.contains(name)){
                books.remove(name);
                return true;
            }
            return false;
        }
        public boolean deleteByAuthorName(String name){
            ListIterator<Book> bookListIterator = books.listIterator();
            int size = books.size();
            while (bookListIterator.hasNext()){
                if(bookListIterator.next().getAuthor().getName().equals(name)){
                    bookListIterator.remove();
                }
            }
            return size!=books.size();
        }
        public void sortBook(){
            Collections.sort(books,(o1,o2)->{
                if(o1.getPrice()!=o2.getPrice())
                    return (int)(o1.getPrice() - o2.getPrice());
                else return o1.getAuthor().getName().compareTo(o2.getAuthor().getName());
            });
    
        }
    }
    

    测试类

    public class Test {
        public static void main(String[] args) {
            LibraryManagement libraryManagement = new LibraryManagement();
            ArrayList<Book> books = new ArrayList<>();
            books.add(new Book("三国演义",new Author("罗贯中",1000,"男"),399.0));
            books.add(new Book("水浒传前",new Author("施耐庵",999,"男"),199.9));
            books.add(new Book("水浒传后",new Author("罗贯中",999,"男"),299.9));
            books.add(new Book("红楼梦",new Author("曹雪芹",9000,"男"),499.9));
            libraryManagement.setBooks(books);
            System.out.println("排序前"+books);
            libraryManagement.sortBook();
            System.out.println("排序后"+books);
            libraryManagement.deleteByName("红楼梦");
            System.out.println(books);
            libraryManagement.deleteByAuthorName("罗贯中");
            System.out.println(books);
        }
    }
    
    1. 创建⼀个Map集合,存⼊5个学⽣信息,使⽤学号和学⽣基本信息进⾏对应,学 ⽣基本信息包括姓名,年龄,专业,遍历输出。

      public static void test3(){
              HashMap<Long, Object[]> map = new HashMap<>();
              map.put(2101L,new Object[]{"张三",21,"计算机"});
              map.put(2102L,new Object[]{"李四",21,"英语"});
              map.put(2103L,new Object[]{"王五",21,"金融"});
              for(Long id: map.keySet()){
                  System.out.println(id+" "+map.get(id)[0]+" "+map.get(id)[1]+" "+map.get(id)[2]);
              }
          }
      
      1. 有两个班级,⼀班和⼆班,每个班级有若⼲学⽣,包含学号和姓名信息,要求能 够完全保存班级和学⽣信息,遍历输出。
      public static void test4(){
              HashMap<Integer, HashMap<String, String>> map = new HashMap<>();
              HashMap<String, String> stuMap1 = new HashMap<>();
              stuMap1.put("101","张三");
              stuMap1.put("102","李四");
              stuMap1.put("103","王五");
      
              HashMap<String, String> stuMap2 = new HashMap<>();
              stuMap2.put("201","张二三");
              stuMap2.put("202","李二四");
              stuMap2.put("203","王二五");
      
              map.put(2101,stuMap1);
              map.put(2102,stuMap2);
      
              System.out.println(map);
          }
      
      1. 有⻓度为5的字符串数组,数组中的每个元素均为⼀个标准英⽂句⼦,要求借助 Map集合统计每个单词出现的次数。
      public static void test5(){
              String[] strings = new String[5];
              strings[0] = "I hava an apple";
              strings[1] = "I hava a apple";
              strings[2] = "hello world";
              strings[3] = "how are you";
              strings[4] = "I hava a dog";
      
              HashMap<String, Integer> map = new HashMap<>();
              for (String string : strings) {
                  String[] split = string.split(" ");
                  for (String s : split) {
                      if(map.get(s.toLowerCase())==null){
                          map.put(s.toLowerCase(),1);
                      }else map.put(s.toLowerCase(),map.get(s.toLowerCase())+1);
                  }
              }
              System.out.println(map);
          }
      
      1. 假如有以下email数据“aa@sohu.com,bb@163.com,cc@sina.com”现需要把 email中的⽤户部分和邮件地址部分分离,分离后以键值对应的⽅式放⼊ HashMap?
      public static void test6(){
              String str = "aa@sohu.com,bb@163.com,cc@sina.com";
              String[] split = str.split(",");
              HashMap<String, String> map = new HashMap<>();
              for (String s : split) {
                  String[] split1 = s.split("@");
                  map.put(split1[0],split1[1]);
              }
              System.out.println(map);
          }
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值