在集合中查找单个对象,HashMap vs List过滤器

时间:2015-05-15 13:21:51

标签: java java-8 java-stream

我从我读过的文件中生成/* Pure CSS Picture Frame */ html { overflow: hidden; background-color: #653845; background-image: linear-gradient(45deg, hsla(0,0%,0%,.1) 50%, transparent 50%), linear-gradient(-45deg, hsla(0,0%,0%,.1) 50%, transparent 50%); background-size: .25em .25em; box-shadow: inset 0 0 500px hsla(0,0%,0%,.5); height: 100%; padding: 1px; } body { height: 300px; width: 400px; margin: -150px -200px; position: absolute; left: 50%; top: 50%; background: gray url(http://dribbble.com/system/users/13774/screenshots/423481/_111.jpg?1329144172); border: 1px solid; border-color: #bbb #999; box-shadow: 0 2px 5px hsla(0,0%,0%,.4), inset 0 1px 0 #ccc, inset 1px 0 0 #aaa, inset 0 -1px 0 #ccc, inset -1px 0 0 #aaa, inset 0 2px 0 #c6c6c6, inset 2px 0 0 #a6a6a6, inset 0 -2px 0 #c6c6c6, inset -2px 0 0 #a6a6a6, inset 0 3px 0 #c0c0c0, inset 3px 0 0 #a0a0a0, inset 0 -3px 0 #c0c0c0, inset -3px 0 0 #a0a0a0, inset 0 4px 0 #b9b9b9, inset 4px 0 0 #999, inset 0 -4px 0 #b9b9b9, inset -4px 0 0 #999, inset 0 5px 0 #b6b6b6, inset 5px 0 0 #969696, inset 0 -5px 0 #b6b6b6, inset -5px 0 0 #969696, inset 0 6px 0 #b0b0b0, inset 6px 0 0 #909090, inset 0 -6px 0 #b0b0b0, inset -6px 0 0 #909090, inset 0 7px 0 #a9a9a9, inset 7px 0 0 #898989, inset 0 -7px 0 #a9a9a9, inset -7px 0 0 #898989, inset 0 8px 0 #a6a6a6, inset 8px 0 0 #868686, inset 0 -8px 0 #a6a6a6, inset -8px 0 0 #868686, inset 0 9px 0 #a0a0a0, inset 9px 0 0 #808080, inset 0 -9px 0 #a0a0a0, inset -9px 0 0 #808080, inset 0 10px 0 #888, inset 10px 0 0 #666, inset 0 -10px 0 #888, inset -10px 0 0 #666, inset 0 0 10px 10px hsla(0,0%,0%,.5); }列表。我将这些客户存储在Customer中,其中密钥是唯一ID:

HashMap

从第二个文件中,我获得了用于更新Map<String, Customer> customers = readCustomers(); //For each object created customers.put(c.getCustomerId(), c);中对象的数据。我使用密钥来查找要更新的对象:

HashMap

在java 8中,我可以使用:

//get the details informations
customers.get(customerId).setDetails(details);

使用Java 8方法会有性能提升吗?这些方法之间的最佳实践是什么?

1 个答案:

答案 0 :(得分:12)

在HashMap中按键搜索值需要O(1)预期时间,这比搜索List中相同值所需的O(n)要快。

使用Java 8 Streams并没有改变它,因为在花哨的新语法的幕后,它仍然遍历List的元素,直到找到匹配。