Java:java.util.Map compute用法(compute,computeIfAbsent,computeIfPresent)

java.util.Map compute用于计算指定键映射的值(如果键没有映射时,值为null)。应用场景,向Map的键映射的字符串后连接新的字符串。

map.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))

如果函数返回空值,则移除映射(如果最初不存在映射,则保持不存在)。

如果函数本身抛出异常,则该异常将被重新引发,并且当前映射保持不变。

compute提供了3个方法

方法场景
compute无论是否有值,BiFunction一定会执行
computeIfPresent

如果指定键的值存在且不为空,则尝试计算给定键及其当前映射值的新映射。

PS:如果指定键不存在BiFunction不会被执行

computeIfAbsent

如果指定的键尚未与值关联(或映射为空),则尝试使用给定的映射函数计算其值,并将其输入此映射,除非为空。

PS:如果指定键存在值,则不会被执行

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Betweenness is a centrality measure used in graph theory to identify the importance of a node in a network. It measures the number of shortest paths that go through a node. In Java, you can calculate the betweenness centrality of a node in a graph using the JUNG (Java Universal Network/Graph Framework) library. Here is an example code snippet: ``` import edu.uci.ics.jung.algorithms.shortestpath.BFSDistanceLabeler; import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath; import edu.uci.ics.jung.graph.Graph; import edu.uci.ics.jung.graph.UndirectedSparseGraph; import edu.uci.ics.jung.graph.util.EdgeType; import edu.uci.ics.jung.graph.util.Pair; import java.util.HashMap; import java.util.Map; public class BetweennessCentralityExample { public static void main(String[] args) { // create an undirected graph Graph<Integer, String> graph = new UndirectedSparseGraph<>(); // add vertices graph.addVertex(1); graph.addVertex(2); graph.addVertex(3); graph.addVertex(4); // add edges graph.addEdge("e1", new Pair<>(1, 2), EdgeType.UNDIRECTED); graph.addEdge("e2", new Pair<>(1, 3), EdgeType.UNDIRECTED); graph.addEdge("e3", new Pair<>(2, 3), EdgeType.UNDIRECTED); graph.addEdge("e4", new Pair<>(3, 4), EdgeType.UNDIRECTED); // calculate the betweenness centrality Map<Integer, Double> betweenness = new HashMap<>(); for (Integer v : graph.getVertices()) { BFSDistanceLabeler<Integer, String> labeler = new BFSDistanceLabeler<>(); labeler.labelDistances(graph, v); for (Integer u : graph.getVertices()) { if (!v.equals(u)) { DijkstraShortestPath<Integer, String> shortestPath = new DijkstraShortestPath<>(graph); shortestPath.reset(); shortestPath.setSource(u); shortestPath.setGoal(v); shortestPath.compute(); double numShortestPaths = shortestPath.getPaths(u).size(); if (numShortestPaths > 0) { double numShortestPathsContainingV = 0; for (String path : shortestPath.getPaths(u)) { if (path.contains(Integer.toString(v))) { numShortestPathsContainingV++; } } double betweennessCentrality = numShortestPathsContainingV / numShortestPaths; if (betweenness.containsKey(v)) { betweenness.put(v, betweenness.get(v) + betweennessCentrality); } else { betweenness.put(v, betweennessCentrality); } } } } } // print the betweenness centrality of each node for (Integer v : betweenness.keySet()) { System.out.println("Node " + v + ": " + betweenness.get(v)); } } } ``` This code creates an undirected graph with four vertices and four edges. It then calculates the betweenness centrality of each node using a modified version of the Brandes algorithm. Finally, it prints the betweenness centrality of each node to the console.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值