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())
}
}