Chinaunix首页 | 论坛 | 博客
  • 博客访问: 429384
  • 博文数量: 79
  • 博客积分: 8385
  • 博客等级: 中将
  • 技术积分: 3625
  • 用 户 组: 普通用户
  • 注册时间: 2005-09-26 14:42
文章分类

全部博文(79)

文章存档

2011年(10)

2010年(40)

2009年(21)

2008年(8)

分类: Java

2010-04-15 10:59:17

*此示例演示HashMap的用法

***/
package maps;
import java.util.HashMap;
public class Test {


private HashMap map;

public Test()
{
map=new HashMap();
}

//添加Map
public void addMap()
{
Student stu1=new Student(1,"name1",60.08);
Student stu2=new Student(2,"name2",70.08);
Student stu3=new Student(3,"name3",80.08);
Student stu4=new Student(4,"name4",90.08);
map.put(stu1.getSno(), stu1);
map.put(stu2.getSno(), stu2);
map.put(stu3.getSno(), stu3);
map.put(stu4.getSno(), stu4);

}
//添加Map,如果Key存在则替换key对应的值,并返回替换的值。
public Object addMap(Object key,Object value)
{
return this.map.put(key, value);
}
//检查KEY
public boolean ContainsKey(Object key)
{
if(map.containsKey(key))
{
return true;
}else
{
return false;
}
}
//检查Value
public boolean ContainsValue(Object value)
{
if(map.containsValue(value))
{
return true;
}else
{
return false;
}
}
//获取Map大小
public int getSize()
{
return this.map.size();
}
//根据Key值移除元素,并返回移除的元素
public Object remove(Object key)
{
if(this.map.containsKey(key))
{
return this.map.remove(key);
}else
{
return null;
}
}
public static void main(String[] arges)
{

}
}

//Student 类

package maps;

public class Student {

private Integer sno;
private String sname;
private Double smark;

public Student(){}

public Student(Integer sno,String sname,Double smark)
{
this.sno=sno;
this.sname=sname;
this.smark=smark;
}
public Integer getSno() {
return sno;
}
public void setSno(Integer sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Double getSmark() {
return smark;
}
public void setSmark(Double smark) {
this.smark = smark;
}
}

阅读(951) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

chinaunix网友2010-04-15 11:34:31

java HashMap的使用 Java中怎样遍历Map的所有的元素: JDK1.4中 view plaincopy to clipboardprint? Map map = new HashMap(); Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); Object key = entry.getKey(); Object value = entry.getValue(); } Map map = new HashMap(); Iterator it = map.entrySet().iterator(); while (it.hasNex