1. forward 和 redirect区别
2. servlet的几个方法 doget dopost区别
3. ArrayList里装了Person类对象,怎样根据Person类的属性年龄来排序
4. HashMap的键值是否可以是任意对象大家看看吧关于第三题,网上有原题,结果小弟没看到,其实我一回查下API就会了,只是个人太老实了,就说不会,真应该和主管说声我虽然不会,我会查API的。开个玩笑啦第四题我看了API,觉得可以是任意对象,但是如果是同种对象,搜索起来是不是更好些? 请牛人帮忙结果一下。当然了,也问了点设计模式J2EE的问题。不多说。
2. servlet的几个方法 doget dopost区别
3. ArrayList里装了Person类对象,怎样根据Person类的属性年龄来排序
4. HashMap的键值是否可以是任意对象大家看看吧关于第三题,网上有原题,结果小弟没看到,其实我一回查下API就会了,只是个人太老实了,就说不会,真应该和主管说声我虽然不会,我会查API的。开个玩笑啦第四题我看了API,觉得可以是任意对象,但是如果是同种对象,搜索起来是不是更好些? 请牛人帮忙结果一下。当然了,也问了点设计模式J2EE的问题。不多说。
解决方案 »
- 关于yyyy-MM-DD HH:MM:SS 字符串转 Date后 输出问题
- 新手求助 exit的监听器无法实现退出
- 多线程同步问题 新开一贴 标题还是要长...................................
- 写一个SQL语句,直接显示记录
- 为什么我的java文件的图标还是记事本的格式!
- 怎样改如下代码才能使图形从10行10列开始显示
- 请问如何用 Applet 调用 JavaScript 的函数?
- 请教,如何将一个应用程序封装为一个.exe程序
- 《think in java》第五章的问题
- 我的问题怎么没人看?关于Collection
- 关于DocumentListener的insertUpdate问题
- 生产消费都问题,运行时卡死了。。。
很简单,不用说
2. servlet的几个方法 doget dopost区别
很简单,不用说
3. ArrayList里装了Person类对象,怎样根据Person类的属性年龄来排序
实现compare接口,重新定义比较原则,用年龄来比较
4. HashMap的键值是否可以是任意对象
值可以,键不可以,键必须是可比较的对象,换句话说是实现了equals的!
以上是自己的理解,及以前老师提到的,大家多指教!
HashMap的键不可以么?equals比较的是hashCode的返回值吧,应该可以是任意对象吧。
但是,要想表现出正确的行为,这个对象必须实现hashCode(),保证相同的对象具有相同的hashcode,不同的对象具有不同的hashCode。这个hashCode就是给这些容器使用的比较方法,通常int hashCode()与boolean equals(Object anObject)应该具有相同的表现
public class customer { /**
* @param args
*/
public static void main(String[] args) {
//创建两个对象
customer c1 = new customer(1, "haha");
customer c2 = new customer(1, "haha");
Map<customer, String> m = new HashMap<customer, String>();
//把c1当作键值
m.put(c1, "我是"+c1.name);
//使用c2取c1
String cname = (String)m.get(c2);
System.out.println(cname);
} public customer(int i, String n) {
id = i;
name = n;
} int id;
String name; @Override
public int hashCode() {
return (this.name + this.id).hashCode();
} @Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o instanceof customer) {
customer anotherCustomer = (customer) o; return anotherCustomer.id == this.id
&& anotherCustomer.name.equals(this.name);
}
return false;
}
}这里的public boolean equals(Object o)没有使用
有两种方式:
1.Person类直接实现comparable这个接口,那Person类本身就具有了比较的功能
2.自己写一个类,实现comparator这个接口,那就是一个比较器,然后在Person类中排序的时候指定这个比较器就行了
值可以,键不可以,键必须是可比较的对象,换句话说是实现了equals的! 应该是可比较的,实现comparable的
String name;
int age;public Person(String name,int age){
this.name = name;
this.age = age;}public int getAge() {
return age;
}public void setAge(int age) {
this.age = age;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}}
2:Mycomparator.java-------------------------------
//实现Comparator接口,也就是定义排序规则,你几乎可以定义任何规则
package com.infoearth;
import java.util.*;
public class Mycomparator implements Comparator{ public int compare(Object o1,Object o2) {
Person p1=(Person)o1;
Person p2=(Person)o2;
if(p1.age<p2.age)
return 1;
else
return 0;
}}3:ListSort.java------------------------------------package com.infoearth;import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;public class ListSort {
public static void main(String[] args){
ArrayList list = new ArrayList();
list.add(new Person("lcl",28));
list.add(new Person("fx",23));
list.add(new Person("wqx",29));
Comparator comp = new Mycomparator();
Collections.sort(list,comp);
for(int i = 0;i<list.size();i++){
Person p = (Person)list.get(i);
System.out.println(p.getName());
} }}或者写成实现Comparator接口的匿名类Collections.sort(list);Collections.sort(list, new Comparator() { public int compare(Object o1,Object o2) {
Person p1=(Person)o1;
Person p2=(Person)o2;
if(p1.age<p2.age)
return 1;
else
return 0;
}
});