Java集合框架上机练习题

1 编写一个Book类,该类至少有name和price两个属性。该类要实现Comparable接口,在接口的compareTo()方法中规定两个Book类实例的大小关系为二者的price属性的大小关系。在主函数中,选择合适的集合类型存放Book类的若干个对象,然后创建一个新的Book类的对象,并检查该对象与集合中的哪些对象相等。查询结果如下图:
这里写图片描述

import java.util.*;
public class BookTest {
    public static void main(String[] args) {
        ArrayList<Book> arraylist= new ArrayList<>();
        arraylist.add(new Book("java基础教程", 30.0));
        arraylist.add(new Book("数据库技术", 29.0));
        arraylist.add(new Book("C++基础教程", 20.0));

        Book book = new Book("模式识别", 29.0);
        System.out.println("新书:《"+book.getName()+"》与下列图书:");
        for (Book b : arraylist){

            System.out.println(b.getName());
            if(b.compareTo(book)==0){
                System.out.println("价格相同,具体价格为:"+book.getPrice());
            }
        }
    }
}
class Book implements Comparable<Book> {
    private String name;  
    private double price;  
    public Book(String n, double p) {
        name = n;
        price = p;
    }
    public String getName() {
        return name;
    }
    public double getPrice() {
        return price;
    }
    public int compareTo(Book other) {
        return ((this.price-other.price==0)?0:1);
    }

}

这里写图片描述

2 编写一个应用程序,用户分别从两个文本框输入学术的姓名和分数,程序按成绩排序将这些学生的姓名和分数显示在一个文本区中。

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.*;
import javax.swing.border.EmptyBorder;


public class Collection2 extends JFrame {
    JButton button;

    HashMap<String,Integer> map=new HashMap<String,Integer>();

    public static void main(String[] args) {
         new Collection2();
    }
    public Collection2(){
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);//设置界面关闭方式
            JPanel contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 10, 5));
            this.setContentPane(contentPane);
            contentPane.setLayout(new GridLayout(5, 1, 10, 5));
            JPanel pane1 = new JPanel();
            pane1.setBackground(Color.red);
            contentPane.add(pane1);
            JPanel pane2 = new JPanel();
            contentPane.add(pane2);
            JPanel pane3 = new JPanel();
            contentPane.add(pane3);
            JLabel label1 = new JLabel("姓名:");

            final JTextField textField1 = new JTextField();
            textField1.setColumns(10);
            pane1.add(label1);
            pane1.add(textField1);
            JLabel label2 = new JLabel("分数:");
            final JTextField textField2 = new JTextField();
            textField2.setColumns(10);
            pane2.add(label2);
            pane2.add(textField2);
            button = new JButton("递交");
            pane3.add(button);
            JScrollPane j = new JScrollPane();

            final JTextArea t = new JTextArea(10, 10);
            j.setViewportView(t);
            contentPane.add(j);
            setSize(300, 500);
            setVisible(true);

            button.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    String name = textField1.getText();
                    String g = textField2.getText();
                    map.put(name, Integer.valueOf(g));
                    sort(map, t);

                }
            });

        }

        public void sort(HashMap<String, Integer> map, JTextArea t) {

            List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
            // 然后通过比较器来实现排序
            Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {

                @Override
                public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
                    // TODO Auto-generated method stub
                    return o2.getValue().compareTo(o1.getValue());
                }
                // 升序排序

            });
            String txt = "";

            for (Map.Entry<String, Integer> mapping : list) {
                String s = mapping.getKey() + ":" + mapping.getValue() + "\n";
                txt += s;
            }

            t.setText(txt);
            // map.entrySet().stream().sorted(Map.Entry.comparingByKey())

        }
}

这里写图片描述这里写图片描述这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值