java学习笔记三
一:内容
List
Map
Set
序列化list对象,讲对象持久化到数据库中
Map不能用for
循环,因为不清楚类型
用equals判断,寻找两个集合的对象是否一致
用 tostring 重写,返回对象的字符串表示
二:e.g
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public
class ListTest<T> {
// 集合
泛型,列表
static List<String> list;
static List<User> users;
// key value 键值
static Map<String, User> map;
static class User implements Serializable {
/**
* 序列化 流
*/
private static final long serialVersionUID = 388254470794743333L;
/**
* 实体类 POJO JavaBean
*
* ORM 对象关系映射
*
*
*/
private String name;
private String password;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int
hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return name + " " + password;
}
}
public static void main(String[] args) {
// ListTest<String> test = new ListTest<String>();
// list = new ArrayList<String>();
// list.add("1sadfa");
// list.add("adsfas");
// list.add("asdfas");
// for (String str : list) {
// System.out.println(str);
// }
// users = new ArrayList<User>();
// users.add(new User("wanggang", "12312"));
// users.add(new User("wanggang2", "12234312"));
// users.add(new User("wanggang2", "123123423422"));
// for (User user : users) {
// System.out.println(user);
// }
// System.out.println(users.size());
// // System.out.println(users.remove(1));
// users.addAll(users);
// System.out.println(users.size());
map = new HashMap<String, ListTest.User>();
User user = new User("wanggang", "12312");
User user2 = new User("wanggang2", "333");
map.put("1", user);
map.put("2", user2);
Set<String> set = map.keySet();
for (String str : set) {
System.out.println(map.get(str));
}
// System.out.println(map.get("2"));
// System.out.println(map.get("1"));
}
}