1 编写一个Book类,该类至少有name和price两个属性。该类要实现Comparable接口,在接口的compareTo()方法中规定两个Book类实例的大小关系为二者的price属性的大小关系。在主函数中,选择合适的集合类型存放Book类的若干个对象,然后创建一个新的Book类的对象,并检查该对象与集合中的哪些对象相等。查询结果如下图:
代码:
book类:
package bookS;
public class book implements Comparable{
String name;
double price;
public book(String name,double price) {
this.name=name;
this.price=price;
}
public int compareTo(Object b) {
book book=( book)b;
return (int) (this.price-book.price);
}
}
TestBook类:
package bookS;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class TestBook {
public static void main(String[] args) {
List<book>list=new LinkedList<book>();
list.add(new book("Java",25));
list.add(new book("数据库技术",30));
list.add(new book("C++",29));
book book1=new book("计算机网络技术",29);
Iterator<book> it=list.iterator();//通过迭代器读取书名
System.out.println("新书:计算机网络技术与下列图书:");
while(it.hasNext()){
book book=it.next();
if(book1.price==book.price)//比较两书的价格
System.out.println(book.name);
}
System.out.println("具体价格为:"+book1.price);
}
}
编写一个应用程序,用户分别从两个文本框输入学生的姓名和分数,程序按成绩排序将这些学生的姓名和分数显示在一个文本区中。
程序运行效果如图:
代码:package score;
import java.awt.BorderLayout;
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.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Grade extends JFrame {
JLabel lName,lScore; //姓名和成绩标签
JTextField tName,tScore; //文本框
JTextArea taShow; //用于显示的文本区域
JButton button;
JPanel pan;
Map<String,String> sMap,rMap;
//主函数
public static void main(String[] args) {
new Grade();
}
//构造方法
public Grade() {
init();
click();
}
//初始化方法
public void init() {
lName=new JLabel("姓名"); //实例化
lScore=new JLabel("成绩");
tName=new JTextField(10);
tScore=new JTextField(10);
button=new JButton("确定");
pan=new JPanel();
taShow=new JTextArea();
pan.add(lName); //控件组合
pan.add(tName);
pan.add(lScore);
pan.add(tScore);
pan.add(button);
add(pan,BorderLayout.NORTH); //设置位置
add(taShow, BorderLayout.CENTER);
setTitle("统计学生姓名和分数"); //设置窗口基本属性
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
validate();
sMap=new HashMap<String,String>(); //非窗口属性初始化
}
//确定按钮被点击
private void click() {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
save();
showMap();
}
});
}
//点击按钮调用的保存方法
private void save() {
sMap.put(tName.getText(),tScore.getText());
rMap = sortMapByValue(sMap); //按Value进行排序
tName.setText(""); //文本框内容清空
tScore.setText("");
}
//按值排序
public static Map<String, String> sortMapByValue(Map<String, String> map) {
if (map == null || map.isEmpty()) {
return null;
}
Map<String, String> sortedMap = new LinkedHashMap<String, String>();
List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(map.entrySet()); //将元素存入List中,类型为entry
Collections.sort(entryList, new MapValueComparator());
Iterator<Map.Entry<String, String>> iter = entryList.iterator();
Map.Entry<String, String> tmpEntry = null;
while (iter.hasNext()) {
tmpEntry = iter.next();
sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue()); //将List中的元素遍历出来存入map
}
return sortedMap;
}
//打印列表
private void showMap() {
taShow.setText("");
for(Map.Entry<String,String> entry:rMap.entrySet()) {
taShow.append("姓名:"+entry.getKey()+" 成绩:"+entry.getValue()+"\n");
}
}
}
//比较器类
class MapValueComparator implements Comparator<Map.Entry<String, String>> {
public int compare(Entry<String, String> s1, Entry<String, String> s2) {
return s1.getValue().compareTo(s2.getValue());
}
}