如果我通过相同的密钥多次HashMap的put方法,会发生什么原始值?如果价值重复怎么办?我没有找到任何关于此的文件.
案例1:密钥的覆盖值
Map mymap = new HashMap();
mymap.put("1","one");
mymap.put("1","not one");
mymap.put("1","surely not one");
System.out.println(mymap.get("1"));
Run Code Online (Sandbox Code Playgroud)
我们得到了surely not one.
案例2:重复值
Map mymap = new HashMap();
mymap.put("1","one");
mymap.put("1","not one");
mymap.put("1","surely not one");
// The following line was added:
mymap.put("1","one");
System.out.println(mymap.get("1"));
Run Code Online (Sandbox Code Playgroud)
我们得到了one.
但是其他价值观会发生什么?我正在向学生教授基础知识,我被问到这个问题.是Map一个桶,其中引用了最后一个值(但在内存中)?
我有一个List<Valuta>可以表示(简化)JSON风格:
[{codice = EUR,description = Euro,ratio = 1},{codice = USD,description = Dollars,ratio = 1.1}]
我想用这样的方式改变它Map<String, Valuta>:
{EUR = {codice = EUR,description = Euro,ratio = 1},USD = {codice = USD,description = Dollars,ratio = 1.1}}
我写了这个单行:
getValute().stream().collect(Collectors.groupingBy(Valuta::getCodice));
Run Code Online (Sandbox Code Playgroud)
但这会返回一个Map<String, List<Valuta>>而不是我需要的东西.
我认为mapping()功能对我有用,但不知道如何.
我想创建一个Map从List的PointS和有地图从里面用相同的parentId,如映射列表中的所有条目Map<Long, List<Point>>.
我用过Collectors.toMap()但不编译:
Map<Long, List<Point>> pointByParentId = chargePoints.stream()
.collect(Collectors.toMap(Point::getParentId, c -> c));
Run Code Online (Sandbox Code Playgroud) 我有一个HashMap,我需要使用一些函数进行过滤:
HashMap<Set<Integer>, Double> container
Map.Entry<Set<Integer>, Double> map = container.entrySet()
.stream()
.filter(k -> k.getKey().size() == size)
Run Code Online (Sandbox Code Playgroud)
对于size = 2,以下内容应该有效:
containerBeforeFilter = {1,2,3} -> 1.5, {1,2} -> 1.3
containerAfterFilter = {1,2} -> 1.3
Run Code Online (Sandbox Code Playgroud)
在过滤器中应用函数后,我想再次将结果收集到HashMap中.但是,当我尝试应用此处建议的方法时,我会收到非法声明.
因此,在过滤器之后应用的以下语句是非法的:
.collect(Collectors.toMap((entry) -> entry.getKey(), (entry) -> entry.getValue()));
Run Code Online (Sandbox Code Playgroud)
收集未更改的地图值的正确方法是什么,唯一的标准是满足某些关键?
UPDATE
上面代码中的错误是变量的类型map.它应该是Map而不是Map.Entry.
所以功能代码是:
Map<Set<Integer>, Double> map = container.entrySet()
.stream()
.filter(k -> k.getKey().size() == size)
.collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue()));
Run Code Online (Sandbox Code Playgroud) 我想使用Java Stream来运行POJO列表,例如List<A>下面的列表,并将其转换为Map Map<String, Set<String>>.
例如,A类是:
class A {
public String name;
public String property;
}
Run Code Online (Sandbox Code Playgroud)
我编写了下面的代码,将值收集到地图中Map<String, String>:
final List<A> as = new ArrayList<>();
// the list as is populated ...
// works if there are no duplicates for name
final Map<String, String> m = as.stream().collect(Collectors.toMap(x -> x.name, x -> x.property));
Run Code Online (Sandbox Code Playgroud)
但是,因为可能有多个相同的POJO name,我希望地图的值为a Set.property同一个键的所有字符串name都应该放在同一个字符集中.
如何才能做到这一点?
// how do i create a stream such that all properties of the same name …Run Code Online (Sandbox Code Playgroud) 我有这个代码来获取地图:
List<MyObject> myList = myMethod.getList();
myList.stream().collect(
Collectors.toMap(
MyObject::getKey,
MyObject::getValue,
(e1, e2) -> {
System.out.println("Duplicate keys !!!");
return e1;
},
LinkedHashMap::new
)
);
Run Code Online (Sandbox Code Playgroud)
如何用重复键打印消息"重复键"?
嗨,我在执行代码时遇到错误:
java.lang.IllegalStateException: Duplicate key ad.ecs.core.scriptengine.SiteIDAndNPI@3a75ce6f
at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1253)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at ad.ecs.core.scriptengine.Utils.getInterfaceIDsWithSiteIDAndNPI(Utils.java:689)
Run Code Online (Sandbox Code Playgroud)
我正在尝试将我的列表分组到地图中:
public static Map<Integer, SiteIDAndNPI> getInterfaceIDsWithSiteIDAndNPI(Session session) {
@SuppressWarnings("unchecked")
List<Srvsetup> srvsetups = session.createCriteria(Srvsetup.class)
.add(Restrictions.gt("scriptinterfaceid", 0))
.list();
Map<Integer, SiteIDAndNPI> interfaceIDs = srvsetups.stream()
.collect(Collectors.toMap(srv -> srv.getScriptinterfaceid(),
srv -> getSiteIDAndNPI(srv.getId().getSiteid(), session)));
@SuppressWarnings("unchecked")
List<Facsetup> facsetups = session.createCriteria(Facsetup.class)
.add(Restrictions.gt("scriptinterfaceid", 0))
.list();
interfaceIDs.putAll(facsetups.stream()
.collect(Collectors.toMap(fac -> fac.getScriptinterfaceid(),
fac -> getSiteIDAndNPI(fac.getPlid(), session))));
return interfaceIDs;
}
public static SiteIDAndNPI getSiteIDAndNPI(int siteID, Session …Run Code Online (Sandbox Code Playgroud) 有没有办法Map<Long, String>直接从groupingByJava 8 中的函数创建,而不是创建Map<Long, Set<String>>?
我有以下代码:
List<Object[]> list = new ArrayList<>();
//row[0] is the primary key and row[1] is the name of the employee
Object[] input1 = { 1l, "employee1", null};
Object[] input2 = { 1l, "employee1", "vorw2"};
Object[] input3 = { 2l, "employee2", "vorw3"};
Object[] input4 = { 3l, "employee3", "vorw1"};
list.add(input1);
list.add(input2);
list.add(input3);
list.add(input4);
//code to replaced
Map<String, Set<String>> byPK= list.stream().collect(Collectors.groupingBy(row -> row[0].longValue(), Collectors.mapping(row -> row[1].toString(),Collectors.toSet() )));
Run Code Online (Sandbox Code Playgroud)
我需要像这样的 o/p:
List ==> (1l, …Run Code Online (Sandbox Code Playgroud) java ×8
java-8 ×6
java-stream ×4
collectors ×3
hashmap ×2
lambda ×2
dictionary ×1
grouping ×1
hashtable ×1