记得我在以前找工作的经历中,遇到过一个面试官问过我一个很基础的问题。问题是:有一个List中有10个元素,我现在想从中删除3个元素,请问怎么做?我当时也没想,就直接说,List的有自带的remove方法,可以直接使用,他说请详细的说明一下,我说写一个for循环,循环的次数是List的长度,然后在循环里面直接删除掉想要删除的元素就可以了。
当时还想,这么简单的问题也问,面试官说,你回去自己试试就知道了,你看按照你说的那样写会不会报错。然后我就懵了,虽然这是个简单的问题但是日常的编码中,我还真没有注意过这个小细节,然后面试结果可想而知了。
我回去以后自己真的试了一次还真的会报错,原来在遍历的过程中是不对List操作进行修改的,无论是删除和添加,因为如果在遍历中一直向集合中新增加元素,会造成死循环的,还有就是如果在遍历过程中删除元素,会造成数组下表越界等问题。一般的操作方式是通过addAll方法和removeAll方法来实现的功能的。
例如下面的这种
@Test
public void myTestLearnMore()
{
List<String> testList = new ArrayList<>();
testList.add("1杨");
testList.add("1李");
testList.add("1王");
testList.add("1张");
testList.add("2杨");
testList.add("2孙");
testList.add("2赵");
List<String> temAddList = new ArrayList<>();
for(String test : testList)
{
if(test.startsWith("1"))
{
temAddList.add(test);
}
}
testList.removeAll(temAddList);
System.out.println(JSON.toJSONString(testList));
}
打印结果是:[“2杨”,”2孙”,”2赵”]
这才是真正的操作方式。不过今天我要说的其实Java8的新增的集合方法,像上面先创建一个临时集合然后再通过遍历将需要移除的元放到临时集合中,最后再整体从原始集合中删除。这样要写五六行的代码,在java8中用一行代码就可以搞定了。就是下面这行代码:
testList.removeIf(test->test.startsWith("1"));
这句代码的意思是移除符合removeIf参数格式的元素,所以在这行代码后面再打印testList,就不会打印出以1开头的元素了。
这些小细节其实都是在日常的编码过程中积累出来的,遇到的坑多了,以后再写的时候就会注意了,就像是java中在使用equals的时候,从来都是已知的常量放equals前面,防止出现空指针异常,在集合使用lambda表达式的时候,都要通过Objects.nonNull()先判断集合是不是null,在打印对象的时候不要直接调用对象的toString()方法,要将对象传递给Objects的toString方法,这样就算对象是个null也可以打印出来。Objects这个类是java7新增的工具类。
class User {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
代码如下:
List<User> users = Arrays.asList(new User(1, "Tomcat"), new User(2, "Apache"), new User(3, "Nginx"));
Map<Integer, User> map = users.stream().collect(Collectors.toMap(obj -> obj.getId() , obj -> obj));
System.out.println(map);
或者使用方法的引用
Map<Integer, User> map = users.stream().collect(Collectors.toMap(User::getId , obj -> obj));
最后,输出结果
{1=User [id=1, name=Tomcat], 2=User [id=2, name=Apache], 3=User [id=3, name=Nginx]}
有如下List<Map<String, String>>
List<Map<String, String>> list = new ArrayList<>();
Map<String, String> map1 = new HashMap<>();
map1.put("id", "101");
map1.put("name", "Tomcat");
Map<String, String> map2 = new HashMap<>();
map2.put("id", "102");
map2.put("name", "Apache");
Map<String, String> map3 = new HashMap<>();
map3.put("id", "103");
map3.put("name", "Nginx");
list.add(map1);
list.add(map2);
list.add(map3);
参考代码如下:
List<String> ids = list.stream().map(entity -> entity.get("id")).collect(Collectors.toList());
System.out.println(ids);
或者
List<Object> ids = Arrays.asList(list.stream().map(entity -> entity.get("id")).toArray());
输出结果:
[101, 102, 103]
参考代码:
String queryString = "type=1&from=APP&source=homePage";
Map<String, String> map = Stream.of(queryString.split("&")).map(obj -> obj.split("=")).collect(Collectors.toMap(entry -> entry[0], entry -> entry[1]));
System.out.println(map);
输出结果:
{from=APP, source=homePage, type=1}
List<String> strs = Arrays.asList("1","2","3");
List<Integer> ints = strs.stream().map(obj -> Integer.valueOf(obj)).collect(Collectors.toList());
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有