c#中的hashmap等效 [英] hashmap equivalent in c#

查看:326
本文介绍了c#中的hashmap等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试将此代码转换为c#



Hello I am trying to convert this code to c#

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

public class TestModulo {	
	static HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
	static int modulo = 10;
	static String fileListEquipment = "C:\\equipments.txt";
	
	public static void main(String[] args) throws IOException {
	int nbEquipments = 0;
		int max = 0;
		int min = 1000000;
		
		BufferedReader br = new BufferedReader(new FileReader(fileListEquipment));
		String equipment = null;
		while ((equipment = br.readLine()) != null) {
			nbEquipments++;
			int asciiSum = 0;
			if (equipment != null) {
				for (int i=0;i<equipment.length();i++) {
					asciiSum  += equipment.charAt(i);
				}
			}
			int index = asciiSum % modulo;
			if (hashMap.get(index) != null) {
				int val = hashMap.get(index);
				val++;
				hashMap.put(index, val);
			} else hashMap.put(index, 1);
		}
	
	System.out.println("Nb Equipments : " + nbEquipments);
	
	  for (int j=0; j < hashMap.size();j++) {
		  System.out.println("INDEX:" + j + " nb:" + hashMap.get(j));
	  }
	  for (int j=0; j < hashMap.size();j++) {
		  int val = hashMap.get(j);
		 // System.out.print(val + " ");
		  if (val > max) max = val;
		  if (val < min ) min = val;
	  }
	  System.out.println();
	  float idelaDelta =  nbEquipments/modulo;
	  float mindelta = 100 - ((100/idelaDelta)*min);
	  float maxdelta = 100 - ((100/idelaDelta)*max);
	  System.out.println("Delta Equipments min:" +  mindelta + " max:" + maxdelta);
	  
	}

}





我尝试过以下代码,但是我找不到满足获取





I have tried the following code, however I cannot find a replacement that satisfies the get

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestModulo
{
    class Program
    {
        public static Dictionary<int, int> hashMap = new Dictionary<int, int>();
        public static int modulo = 9;
        public static string fileListEquipment = "C:\\equipments.txt";


        static void Main(string[] args)
        {
            int nbEquipments = 0;
            int max = 0;
            int min = 1000000;

            System.IO.StreamReader file = new System.IO.StreamReader(fileListEquipment);
            String equipment = null;
            while ((equipment = file.ReadLine()) != null)
            {
                nbEquipments++;
                int asciiSum = 0;
                if (equipment != null)
                {
                    for (int i = 0; i < equipment.Length; i++)
                    {
                        asciiSum += equipment[i];
                    }
                }
                int index = asciiSum % modulo;
                if (hashMap[index] != null)
                {
                    int val = hashMap.TryGetValue[index];
                    
                    val++;
                    hashMap.Add(index, val);
                }
                else hashMap.Add(index, 1);
            }

            Console.WriteLine("Nb Equipments : " + nbEquipments);

            for (int j = 0; j < hashMap.Count(); j++)
            {
                Console.WriteLine("INDEX:" + j + " nb:" + hashMap.ContainsValue(j));
            }
            for (int j = 0; j < hashMap.Count(); j++)
            {
                int val = hashMap[j];
                
                if (val > max) max = val;
                if (val < min) min = val;
            }
            Console.WriteLine();
            float idelaDelta = nbEquipments / modulo;
            float mindelta = 100 - ((100 / idelaDelta) * min);
            float maxdelta = 100 - ((100 / idelaDelta) * max);
            Console.WriteLine("Delta Equipments min:" + mindelta + " max:" + maxdelta);
        }
    }
}





任何有关我可以使用的帮助。谢谢



Any help please of what I can use. Thanks

推荐答案



Either
if(hashMap.ContainsKey(index))
{
   int val = hashMap[index];
   val++;
   hashMap[index] = val; // can't use hashMap.Add(index, val) if index is already contained

   // or the short version for the 3 lines above:
   // hashMap[index]++;
}






or

int val;
if(hashMap.TryGetValue(index, out val))
{
   val++;
   hashMap[index] = val;
}





编辑:第一个版本(使用ContainsKey方法)的性能比第二个因为你要求字典三次查找密钥(而不是两次)。

Richards解决方案(省略if-check)是最简洁的,因为它使得别的 - 部分已经过时了。但是使用/不模糊是唯一安全的,因为你的Dictionary的值类型的默认值(在这种情况下为0表示int)不能是有效的条目。



The first version (using the method ContainsKey) has worse performance than the second because you're asking the Dictionary three times to look up the key (instead of two times).
Richards solution above (omitting the if-check) is the most concise one because it makes the else-part obsolete. But it's only safe to use/not ambiguous because the default value of your value type of your Dictionary (in this case 0 for int) can't be a valid entry.


尝试像这个:

Try something like this:
int index = asciiSum % modulo;

int val;
hashMap.TryGetValue(index, out val);

// TryGetValue returns a bool indicating whether the index was found, but we don't care:
// * If the index is in the dictionary, val will be the current value.
// * If the index is missing, val will be set to 0.

val++;
hashMap[index] = val;



字典(TKey,TValue).TryGetValue方法(TKey,TValue)(System.Collections.Generic) [ ^ ]


这篇关于c#中的hashmap等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆