黑马程序员技术交流社区
标题:
List转成Map输出错误
[打印本页]
作者:
294645832
时间:
2015-1-29 15:12
标题:
List转成Map输出错误
package mapDemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class AccountDemo {
public static void main(String[] args) {
List<Account> list = new ArrayList<Account>();
list.add(new Account(10, "1234"));
list.add(new Account(15, "5678"));
list.add(new Account(0, "1010"));
Map<Long, Account> map = new HashMap<>();
for(int i =0;i<list.size();i++){
Account a = list.get(i);
map.put(a.getId(), a);
}
Set<Map.Entry<Long, Account>> entry = map.entrySet();
Iterator<Entry<Long, Account>> iter = entry.iterator();
while (iter.hasNext()) {
Map.Entry<java.lang.Long,mapDemo.Account> entry2 = (Map.Entry<java.lang.Long,mapDemo.Account>) iter
.next();
Long id = entry2.getKey();
Account a = entry2.getValue();
String password = a.getPassword();
double balance = a.getBalance();
System.out.println("id="+id+",password="+password+",balance="+balance);
}
}
}
class Account{
private Long id =0l;
private double balance;
private String password;
public Account(double balance, String password) {
super();
++id;
this.balance = balance;
this.password = password;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
复制代码
为什么只有id=1,password=1010,balance=0.0
没有
list.add(new Account(10, "1234"));
list.add(new Account(15, "5678"));
list.add(new Account(0, "1010"));
作者:
294645832
时间:
2015-1-30 13:08
默默顶一个。。。。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2