62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.HashMap;
import java.util.Map;
public class Student2
{
private String name;
private String sex;
public Student2(String name, String sex)
{
this.name = name;
this.sex = sex;
}
@Override
public String toString()
{
return "[name:" + name + ";sex:" + sex + "]";
}
public static void main(String[] args)
{
Student2 student1=new Student2("李明","男");
Student2 student2=new Student2("刘丽","女");
Map stu=new HashMap();
stu.put("Jack",student1);
stu.put("Rose", student2);
System.out.println("键集"+stu.keySet());
System.out.println("值集"+stu.values());
}
}
public Collection<V> values() {
if (values == null) {
values = new AbstractCollection<V>() {
public Iterator<V> iterator() {
return new Iterator<V>() {
private Iterator<Entry<K,V>> i = entrySet().iterator();
public boolean hasNext() {
return i.hasNext();
}
public V next() {
return i.next().getValue();
}
public void remove() {
i.remove();
}
};
}
public int size() {
return AbstractMap.this.size();
}
public boolean isEmpty() {
return AbstractMap.this.isEmpty();
}
public void clear() {
AbstractMap.this.clear();
}
public boolean contains(Object v) {
return AbstractMap.this.containsValue(v);
}
};
}
return values;
}
values就是一个"值"的Collection集合而已
而且输出也没问题啊,System.out.....会自动调用Object的toString方法