如何按降序打印特定字符串的发生量?
我希望使用一个日志的字符串[],详细介绍已连接到网站的用户以降序打印出每个用户连接的次数。每个日志包含一些信息,但是唯一的用户ID始终在数组的第一个索引之内。
我正在尝试通过O(n)时间中的数组,计算每个用户通过将它们添加到hashmap中连接的次数,然后根据其连接的次数将连接的用户打印出连接的用户。怎么办?如果有一种更简单的方法来跟踪字符串[]中的出现数量,我就可以切换整个实现。我附上了下面的示例:
// Connection logs in the form [Username, Location, time]
String[] logs = {
"name1,Chicago,8pm",
"name2,New York,6pm",
"name3,Los Angeles,2am",
"name1,Chicago,3pm",
"name1,Chicago,12pm",
"name4,Miami,3pm"
"name4,Miami,6pm"
};
printConnections(logs);
/* Desired output:
name1: 3
name2: 2
name4: 2
name3: 1
*/
public static void printConnections(String[] connections){
HashMap<String, Integer> hashmap = new HashMap<String, Integer>();
for (String log : connections){
String name = log.split(",")[0];
if (hashmap.containsKey(name)){
hashmap.replace(name, hashmap.get(name) + 1);
}
else{
hashmap.put(name, 1);
}
}
// Print all key/values in descending order of value
}
I'm looking to use a String[] of logs detailing users who have connected to a website to print out in descending order the amount of times each user has connected. Each log contains some information but the unique userID is always within the first index of the array.
I'm trying to go through the array in O(N) time, calculate how many times each user has connected by adding them to a HashMap, then print out the connected users in descending order based on how many times they have connected. How can this be done? I'm open to switching up my entire implementation if there is an easier way to track the amount of occurrences within the String[]. I have attached an example below:
// Connection logs in the form [Username, Location, time]
String[] logs = {
"name1,Chicago,8pm",
"name2,New York,6pm",
"name3,Los Angeles,2am",
"name1,Chicago,3pm",
"name1,Chicago,12pm",
"name4,Miami,3pm"
"name4,Miami,6pm"
};
printConnections(logs);
/* Desired output:
name1: 3
name2: 2
name4: 2
name3: 1
*/
public static void printConnections(String[] connections){
HashMap<String, Integer> hashmap = new HashMap<String, Integer>();
for (String log : connections){
String name = log.split(",")[0];
if (hashmap.containsKey(name)){
hashmap.replace(name, hashmap.get(name) + 1);
}
else{
hashmap.put(name, 1);
}
}
// Print all key/values in descending order of value
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我假设o(n)是指o(n),其中n =唯一用户的数量(即
hashmap.keyset()。size。size()
)。不幸的是,您的问题涉及对O(n*log(n))复杂性充其量进行排序,因此我认为无法在O(n)时间进行此操作。但是,我还有一些代码可以完成工作:First of all, I'm assuming that by O(n) you mean O(n) where n = number of unique users (ie.
hashmap.keySet().size()
). Unfortunately, your problem involves sorting which is at best O(n*log(n)) complexity, so I don't think it is possible to do this in O(n) time. However, I have some code to still get the job done:tldr:
输出:
它如何工作?
hashmap.entryset()
将返回一组.entry
对象.stream()
将返回这些对象的流.sorted()
将按map.entry
对象的值对流进行排序。
TLDR:
Output:
How it works?
hashmap.entrySet()
will return a set ofMap.Entry
objects.stream()
will return a stream of these objects.sorted()
will sort the stream by the value of theMap.Entry
objectsFull:
您必须使用 java stream api 用于解决此问题。
You have to use Java Stream Api for solve this problem.