[英]Converting singly linked list to a map
我被賦予了改變以升級現有的任務。
在假設問題的大小由輸入線的數量而不是500個終端線控制的情況下,弄清楚如何使用每個終端線的Map重新編碼合格的考試問題
該程序接收一個具有數字,名稱的文本文件。 該號碼是PC號碼,名稱是登錄的用戶。 該程序返回登錄最多的每台PC的用戶。 這是現有的代碼
public class LineUsageData {
SinglyLinkedList<Usage> singly = new SinglyLinkedList<Usage>();
//function to add a user to the linked list or to increment count by 1
public void addObservation(Usage usage){
for(int i = 0; i < singly.size(); ++i){
if(usage.getName().equals(singly.get(i).getName())){
singly.get(i).incrementCount(1);
return;
}
}
singly.add(usage);
}
//returns the user with the most connections to the PC
public String getMaxUsage(){
int tempHigh = 0;
int high = 0;
String userAndCount = "";
for(int i = 0; i < singly.size(); ++i){//goes through list and keeps highest
tempHigh = singly.get(i).getCount();
if(tempHigh > high){
high = tempHigh;
userAndCount = singly.get(i).getName() + " " + singly.get(i).getCount();
}
}
return userAndCount;
}
}
我在理論方面遇到麻煩。 我們可以使用hashmap或treemap。 我正在考慮如何構建一個能夠保存每台PC用戶列表的地圖? 我可以重用Usage對象,該對象將保存用戶的名稱和計數。 我不應該改變那個對象
檢查列表中是否存在Usage
,每次都執行線性搜索( O(N)
)。 如果使用Map<String,Usage>
替換列表,則可以在次線性時間內搜索name
。 TreeMap
具有O(log N)
時間用於搜索和更新, HashMap
具有分攤O(1)
(常數)時間。
因此,在這種情況下最有效的數據結構是HashMap
。
import java.util.*;
public class LineUsageData {
Map<String, Usage> map = new HashMap<String, Usage>();
//function to add a user to the map or to increment count by 1
public void addObservation(Usage usage) {
Usage existentUsage = map.get(usage.getName());
if (existentUsage == null) {
map.put(usage.getName(), usage);
} else {
existentUsage.incrementCount(1);
}
}
//returns the user with the most connections to the PC
public String getMaxUsage() {
Usage maxUsage = null;
for (Usage usage : map.values()) {
if (maxUsage == null || usage.getCount() > maxUsage.getCount()) {
maxUsage = usage;
}
}
return maxUsage == null ? null : maxUsage.getName() + " " + maxUsage.getCount();
}
// alternative version that uses Collections.max
public String getMaxUsageAlt() {
Usage maxUsage = map.isEmpty() ? null :
Collections.max(map.values(), new Comparator<Usage>() {
@Override
public int compare(Usage o1, Usage o2) {
return o1.getCount() - o2.getCount();
}
});
return maxUsage == null ? null : maxUsage.getName() + " " + maxUsage.getCount();
}
}
Map
也可以在與其大小成比例的時間內迭代,因此您可以使用相同的過程來查找其中的最大元素。 我給了你兩個選項,手動方法或Collections.max
實用方法的使用。
用簡單的單詞:當你有一個項目列表時,你使用LinkedList
(單獨或雙重),你通常計划遍歷它們,當你有“類字典”條目時,你可以使用一個Map
實現,其中一個鍵對應一個值並且您計划使用密鑰訪問該值。
要將SinglyLinkedList
轉換為HashMap
或TreeMap
,您需要找出項目的哪個屬性將用作您的鍵(它必須是具有唯一值的元素)。
假設您正在使用Usage類中的name屬性,您可以這樣做(一個簡單的示例):
//You could also use TreeMap, depending on your needs.
Map<String, Usage> usageMap = new HashMap<String, Usage>();
//Iterate through your SinglyLinkedList.
for(Usage usage : singly) {
//Add all items to the Map
usageMap.put(usage.getName(), usage);
}
//Access a value using its name as the key of the Map.
Usage accessedUsage = usageMap.get("AUsageName");
另請注意:
Map<string, Usage> usageMap = new HashMap<>();
由於鑽石推斷 ,有效。
我離線解決了這個問題,沒有機會看到一些看起來非常有幫助的答案。 對Nick和Aivean抱歉,並感謝您的回復。 這是我最終編寫的代碼,以使其工作。
public class LineUsageData {
Map<Integer, Usage> map = new HashMap<Integer, Usage>();
int hash = 0;
public void addObservation(Usage usage){
hash = usage.getName().hashCode();
System.out.println(hash);
while((map.get(hash)) != null){
if(map.get(hash).getName().equals(usage.name)){
map.get(hash).count++;
return;
}else{
hash++;
}
}
map.put(hash, usage);
}
public String getMaxUsage(){
String str = "";
int tempHigh = 0;
int high = 0;
//for loop
for(Integer key : map.keySet()){
tempHigh = map.get(key).getCount();
if(tempHigh > high){
high = tempHigh;
str = map.get(key).getName() + " " + map.get(key).getCount();
}
}
return str;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.