Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace SortedDictionaryDemo
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. SortedDictionary<int, int> sd = new SortedDictionary<int, int>();
  12. // Add some sample data
  13. sd.Add(6, 20);
  14. sd.Add(1, 11);
  15. sd.Add(11, 99);
  16. sd.Add(7,3);
  17.  
  18. // get min key in sorted dictionary
  19. Console.WriteLine("The min key in sorted dictionary {0} should be 1", sd.Keys.First());
  20.  
  21. // get max key in sorted dictionary
  22. Console.WriteLine("The max key in sorted dictionary {0} should be 11", sd.Keys.Last());
  23.  
  24. // remove the key-value pair with min key from sorted dictionary and then check the min key again
  25. sd.Remove(sd.Keys.First());
  26. Console.WriteLine("The min key in sorted dictionary {0} should be 6", sd.Keys.First());
  27.  
  28. // add a new key-value pair and check the max key again
  29. sd.Add(15, 6);
  30. Console.WriteLine("The max key in sorted dictionary {0} should be 15", sd.Keys.Last());
  31.  
  32. // loop through the sorted dictionary
  33. foreach (var i in sd) {
  34. Console.WriteLine("Key is {0}, Value is {1}", i.Key, i.Value);
  35. }
  36.  
  37. Console.WriteLine("\n\n\n");
  38. Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  39. Console.WriteLine("\n\n\n");
  40.  
  41.  
  42. //############### Sorted Dictionary with Custom Comparer: sorted by key DESC #############
  43. SortedDictionary<int, int> desc_sd = new SortedDictionary<int, int>( new MyComparer() );
  44.  
  45. // add some sample data
  46. desc_sd.Add(6,20);
  47. desc_sd.Add(1,11);
  48. desc_sd.Add(11,99);
  49. desc_sd.Add(7,3);
  50.  
  51. // get min key in sorted dictionary
  52. Console.WriteLine("The min key in reversed sorted dictionary {0} should be 1", desc_sd.Keys.Last());
  53.  
  54. // get max key in sorted dictionary
  55. Console.WriteLine("The max key in reversed sorted dictionary {0} should be 11", desc_sd.Keys.First());
  56.  
  57. // remove the key-value pair with min key from sorted dictionary and then check the min key again
  58. desc_sd.Remove(desc_sd.Keys.Last());
  59. Console.WriteLine("The min key in reversed sorted dictionary {0} should be 6", desc_sd.Keys.Last());
  60.  
  61. // add a new key-value pair and check the max key again
  62. desc_sd.Add(15, 6);
  63. Console.WriteLine("The max key in reversed sorted dictionary {0} should be 15", desc_sd.Keys.First());
  64.  
  65. // loop through the reversed sorted dictionary
  66. foreach (var i in desc_sd)
  67. {
  68. Console.WriteLine("Key is {0}, Value is {1}", i.Key, i.Value);
  69. }
  70.  
  71. Console.ReadKey();
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement