1.过滤代码格式
提供以下可直接使用的User类,直接使用getter/setter方法。
编写测试类主函数,模拟创建5个用户,分属3个不同城市
创建Set集合,添加以上元素对象。思考,为什么使用Set集合?
public class User {
private int id;
private String name;
private String city;
public User(int id, String name, String city) {
this.id = id;
this.name = name;
this.city = city;
}
// 省略getter/setter方
}
2集合转换 创建静态方法,传入封装User类型元素的Set集合,转为List集合返回。不同集合类型对象相互转换的简单方式?
3 迭代器的使用 创建静态方法,传入封装User类型元素的Set集合,以及用户ID,基于迭代器,在集合移除指定ID用户。方法参数?
it.next()
向下走,并返回当前元素
为啥????:
/删除最近一次已近迭代出出去的那个元素。
只有当next执行完后,才能调用remove函数。
比如你要删除第一个元素,不能直接调用 remove() 而要先next一下( );
在没有先调用next 就调用remove方法是会抛出异常的。/
listIterator迭代器实现向前向后遍历
这个是重点:Java迭代器Iterator浅析——next/remove操作
另一种说法:完全不同??? Iterator主要有三个方法:hasNext()、next()、remove()详解
啥也不说了,看遍源代码比啥都强(Java迭代器Iterator浅析——next/remove操作是对的):
配合使用
java8的注释:Removes from the underlying collection the last element returned
* by this iterator
4 创建静态方法,传入封装User类型元素的Set集合,将集合中元素,以城市名称为键,相同城市用户集合为值,分传入测试集合,调用测试
相同键会不会覆盖?,网上搜会,ppt没说不会,用的list,ppt是可能会有hash值重复
Map<String,String>map=new HashMap<>();
String n1=new String("BO");
String n2=new String("BO");
/* map.put(user1.getCity(),user1.getName());
map.put(user4.getCity(),user4.getName());*/
map.put(n1,"78");
map.put(n2,"65");
System.out.println(map.size());
结果是1
Main
import java.util.*;
public class Main {
public static void main(String[] args) {
User user1=new User(1,"a","cityA");
User user2=new User(2,"b","cityB");
User user3=new User(3,"c","cityC");
User user4=new User(4,"a2","cityA");
User user5=new User(5,"b2","cityB");
//user1.getter();
///1
Set<User> usersSet=new HashSet<>();
usersSet.add(user1);
usersSet.add(user2);
usersSet.add(user3);
usersSet.add(user4);
usersSet.add(user5);
///2
usersSet.forEach(u->{
u.getter();
});
System.out.println("------------this is the split line------------------------------");
User.turn(usersSet).forEach(
u->{
u.getter();
}
);
///
/* System.out.println("------------this is the split line------------------------------");
*/
3
/* usersSet=User.del(3,usersSet);
usersSet.forEach(u->{
u.getter();
});*/
Iterator<User> user=usersSet.iterator();
4
System.out.println("------------this is the split line------------------------------");
Map<String,List<User> >map=User.toMapp(usersSet);
System.out.println(map.size()+":should be 3");
System.out.println("test of Arraylen"+":should be 2");
List<User>ll=map.get(user1.getCity());
System.out.println(ll);
}
}
User
import java.util.*;
public class User {
private int id;
private String name;
private String city;
public User(int id, String name, String city) {
this.id = id;
this.name = name;
this.city = city;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public String getCity() {
return city;
}
public void getter() {
System.out.println(getId()+" "+getCity()+" "+getName());
}
/2
public static List<User> turn(Set<User> userSet){
List<User> users=new ArrayList<>(userSet);
return users;
}
///3
public static Set<User> del(int id,Set<User>users){
Iterator<User> it=users.iterator();
while (it.hasNext()){
User user=it.next();
if(user.id==id){
it.remove();
}
}
return users;
}
public static Map<String,List<User>> toMapp(Set<User>users){
Map<String,List<User>> map=new HashMap<>();
users.forEach(u->{
if(!map.containsKey(u.city)){
List <User> a=new ArrayList<>();
a.add(u);
map.put(u.city,a);
}else {
List<User> a=map.get(u.city);
a.add(u);
map.put(u.city,a);
}
});
return map;
}
// 省略getter/setter方法
}