本文整理汇总了C#中HashMap.ContainsKey方法的典型用法代码示例。如果您正苦于以下问题:C# HashMap.ContainsKey方法的具体用法?C# HashMap.ContainsKey怎么用?C# HashMap.ContainsKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashMap
的用法示例。
在下文中一共展示了HashMap.ContainsKey方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main()
{
Console.Write("Please, enter some text: ");
string text = Console.ReadLine();
var chars = text.AsEnumerable();
var charCounts = new HashMap<char, int>();
foreach (var character in chars)
{
if (charCounts.ContainsKey(character))
{
charCounts[character]++;
}
else
{
charCounts[character] = 1;
}
}
var sortedChars = charCounts.Keys.OrderBy(k => k).ToList();
foreach (var character in sortedChars)
{
Console.WriteLine("{0}: {1} time(s)", character, charCounts[character]);
}
}
示例2: Main
public static void Main()
{
Console.WriteLine("Enter contacts (name - number) or search existing contacts (type 'search'):");
HashMap<string, string> phonebook = new HashMap<string, string>();
string entry = Console.ReadLine();
while (entry != "search")
{
string[] contactInfo = entry
.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim())
.ToArray();
if (!phonebook.ContainsKey(contactInfo[0]))
{
phonebook.Add(contactInfo[0], contactInfo[1]);
}
entry = Console.ReadLine();
}
List<string> searchWords = new List<string>();
string word = Console.ReadLine();
while (word != string.Empty)
{
searchWords.Add(word);
word = Console.ReadLine();
}
foreach (var searchWord in searchWords)
{
if (phonebook.ContainsKey(searchWord))
{
Console.WriteLine("{0} -> {1}", searchWord, phonebook[searchWord]);
}
else
{
Console.WriteLine("Contact {0} does not exist.", searchWord);
}
}
}
示例3: Main
static void Main(string[] args)
{
IMap<string, int> map = new HashMap<string, int>();
map.Put("yellow", 1);
map.Put("blue", 10);
map.Put("red", 67);
while(true)
{
string[] command = Console.ReadLine().Split(' ');
try
{
switch (command[0].ToLower())
{
case "clear":
map.Clear();
break;
case "put":
map.Put(command[1], Convert.ToInt32(command[2]));
break;
case "remove":
map.Remove(command[1]);
break;
case "containskey":
Console.WriteLine(map.ContainsKey(command[1]));
break;
case "containsvalue":
Console.WriteLine(map.ContainsValue(Convert.ToInt32(command[1])));
break;
case "list":
foreach (IEntry<string, int> e in map)
Console.WriteLine(e.ToString());
break;
case "keys":
foreach (string s in map.Keys)
Console.WriteLine(s);
break;
case "values":
foreach (int i in map.Values)
Console.WriteLine(i.ToString());
break;
case "testum": //test
UnmutableMap<string, int> um = new UnmutableMap<string, int>(map);
Console.WriteLine(um["red"].ToString());
um["red"] = 3;
break;
case "testfind":
map = MapUtilsGeneric<string, int>.FindAll(map,
new MapUtilsGeneric<string, int>.CheckDelegate( (Entry<string, int> e) => { return e.Key[0] == 'r'; } ),
MapUtilsGeneric<string, int>.ArrayMapConstructor);
break;
default:
throw new Exception("Unknown command.");
}
} catch (Exception ex)
{
Console.WriteLine(ex.GetType().ToString() + ": " + ex.Message);
}
}
}
示例4: DirectoryReader
/// <summary>This constructor is only used for <see cref="Reopen()" /> </summary>
internal DirectoryReader(Directory directory, SegmentInfos infos, SegmentReader[] oldReaders, int[] oldStarts,
IEnumerable<KeyValuePair<string, byte[]>> oldNormsCache, bool readOnly, bool doClone, int termInfosIndexDivisor)
{
this.internalDirectory = directory;
this.readOnly = readOnly;
this.segmentInfos = infos;
this.termInfosIndexDivisor = termInfosIndexDivisor;
if (!readOnly)
{
// We assume that this segments_N was previously
// properly sync'd:
synced.UnionWith(infos.Files(directory, true));
}
// we put the old SegmentReaders in a map, that allows us
// to lookup a reader using its segment name
IDictionary<string, int> segmentReaders = new HashMap<string, int>();
if (oldReaders != null)
{
// create a Map SegmentName->SegmentReader
for (int i = 0; i < oldReaders.Length; i++)
{
segmentReaders[oldReaders[i].SegmentName] = i;
}
}
var newReaders = new SegmentReader[infos.Count];
// remember which readers are shared between the old and the re-opened
// DirectoryReader - we have to incRef those readers
var readerShared = new bool[infos.Count];
for (int i = infos.Count - 1; i >= 0; i--)
{
// find SegmentReader for this segment
if (!segmentReaders.ContainsKey(infos.Info(i).name))
{
// this is a new segment, no old SegmentReader can be reused
newReaders[i] = null;
}
else
{
// there is an old reader for this segment - we'll try to reopen it
newReaders[i] = oldReaders[segmentReaders[infos.Info(i).name]];
}
bool success = false;
try
{
SegmentReader newReader;
if (newReaders[i] == null || infos.Info(i).GetUseCompoundFile() != newReaders[i].SegmentInfo.GetUseCompoundFile())
{
// We should never see a totally new segment during cloning
System.Diagnostics.Debug.Assert(!doClone);
// this is a new reader; in case we hit an exception we can close it safely
newReader = SegmentReader.Get(readOnly, infos.Info(i), termInfosIndexDivisor);
}
else
{
newReader = newReaders[i].ReopenSegment(infos.Info(i), doClone, readOnly);
}
if (newReader == newReaders[i])
{
// this reader will be shared between the old and the new one,
// so we must incRef it
readerShared[i] = true;
newReader.IncRef();
}
else
{
readerShared[i] = false;
newReaders[i] = newReader;
}
success = true;
}
finally
{
if (!success)
{
for (i++; i < infos.Count; i++)
{
if (newReaders[i] != null)
{
try
{
if (!readerShared[i])
{
// this is a new subReader that is not used by the old one,
// we can close it
newReaders[i].Close();
}
else
{
// this subReader is also used by the old reader, so instead
// closing we must decRef it
newReaders[i].DecRef();
//.........这里部分代码省略.........
示例5: ProcessTerms
private void ProcessTerms(System.String[] queryTerms)
{
if (queryTerms != null)
{
System.Array.Sort(queryTerms);
IDictionary<string, int> tmpSet = new HashMap<string, int>(queryTerms.Length);
//filter out duplicates
IList<string> tmpList = new List<string>(queryTerms.Length);
IList<int> tmpFreqs = new List<int>(queryTerms.Length);
int j = 0;
for (int i = 0; i < queryTerms.Length; i++)
{
var term = queryTerms[i];
var position = tmpSet[term];
if (!tmpSet.ContainsKey(term)) // if temp_position == null
{
tmpSet[term] = j++;
tmpList.Add(term);
tmpFreqs.Add(1);
}
else
{
int integer = tmpFreqs[position];
tmpFreqs[position] = (integer + 1);
}
}
terms = tmpList.ToArray();
//termFreqs = (int[])tmpFreqs.toArray(termFreqs);
termFreqs = new int[tmpFreqs.Count];
int i2 = 0;
foreach (int integer in tmpFreqs)
{
termFreqs[i2++] = integer;
}
}
}
示例6: TestLazy
public void TestLazy()
{
int id = Random().nextInt(NUM_DOCS);
IndexReader reader = DirectoryReader.Open(dir);
try
{
Query q = new TermQuery(new Term("docid", "" + id));
IndexSearcher searcher = NewSearcher(reader);
ScoreDoc[] hits = searcher.Search(q, 100).ScoreDocs;
assertEquals("Too many docs", 1, hits.Length);
LazyTestingStoredFieldVisitor visitor
= new LazyTestingStoredFieldVisitor(new LazyDocument(reader, hits[0].Doc),
FIELDS);
reader.Document(hits[0].Doc, visitor);
Document d = visitor.doc;
int numFieldValues = 0;
IDictionary<string, int> fieldValueCounts = new HashMap<string, int>();
// at this point, all FIELDS should be Lazy and unrealized
foreach (IndexableField f in d)
{
numFieldValues++;
if (f.Name.equals("never_load"))
{
fail("never_load was loaded");
}
if (f.Name.equals("load_later"))
{
fail("load_later was loaded on first pass");
}
if (f.Name.equals("docid"))
{
assertFalse(f.Name, f is LazyDocument.LazyField);
}
else
{
int count = fieldValueCounts.ContainsKey(f.Name) ?
fieldValueCounts[f.Name] : 0;
count++;
fieldValueCounts.Put(f.Name, count);
assertTrue(f.Name + " is " + f.GetType(),
f is LazyDocument.LazyField);
LazyDocument.LazyField lf = (LazyDocument.LazyField)f;
assertFalse(f.Name + " is loaded", lf.HasBeenLoaded);
}
}
Console.WriteLine("numFieldValues == " + numFieldValues);
assertEquals("numFieldValues", 1 + (NUM_VALUES * FIELDS.Length), // LUCENENET TODO: Failing here 1 too small, but what field is the + 1 here supposed to represent?
numFieldValues);
foreach (string field in fieldValueCounts.Keys)
{
assertEquals("fieldName count: " + field,
NUM_VALUES, fieldValueCounts[field]);
}
// pick a single field name to load a single value
string fieldName = FIELDS[Random().nextInt(FIELDS.Length)];
IndexableField[] fieldValues = d.GetFields(fieldName);
assertEquals("#vals in field: " + fieldName,
NUM_VALUES, fieldValues.Length);
int valNum = Random().nextInt(fieldValues.Length);
assertEquals(id + "_" + fieldName + "_" + valNum,
fieldValues[valNum].StringValue);
// now every value of fieldName should be loaded
foreach (IndexableField f in d)
{
if (f.Name.equals("never_load"))
{
fail("never_load was loaded");
}
if (f.Name.equals("load_later"))
{
fail("load_later was loaded too soon");
}
if (f.Name.equals("docid"))
{
assertFalse(f.Name, f is LazyDocument.LazyField);
}
else
{
assertTrue(f.Name + " is " + f.GetType(),
f is LazyDocument.LazyField);
LazyDocument.LazyField lf = (LazyDocument.LazyField)f;
assertEquals(f.Name + " is loaded?",
lf.Name.equals(fieldName), lf.HasBeenLoaded);
}
}
// use the same LazyDoc to ask for one more lazy field
visitor = new LazyTestingStoredFieldVisitor(new LazyDocument(reader, hits[0].Doc),
"load_later");
reader.Document(hits[0].Doc, visitor);
d = visitor.doc;
// ensure we have all the values we expect now, and that
// adding one more lazy field didn't "unload" the existing LazyField's
// we already loaded.
//.........这里部分代码省略.........