thashset_intersectwith.lpr 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. program thashset_intersectwith;
  2. {$MODE DELPHI}
  3. {$APPTYPE CONSOLE}
  4. uses
  5. SysUtils, Generics.Collections;
  6. function SetToStr(ASet: THashSet<string>): string;
  7. var
  8. i: string;
  9. begin
  10. Result := '(';
  11. for i in ASet do
  12. Result := Result + ' ' + i;
  13. Result := Result + ' )';
  14. end;
  15. procedure WriteLnHashSet(const AName: string; AHashSet: THashSet<string>);
  16. begin
  17. WriteLn(Format('%0:s.Count = %1:d %0:s = %2:s', [AName, AHashSet.Count, SetToStr(AHashSet)]));
  18. end;
  19. var
  20. Group1: THashSet<string>;
  21. Group2: THashSet<string>;
  22. Group3: THashSet<string>;
  23. begin
  24. Group1 := THashSet<string>.Create;
  25. Group2 := THashSet<string>.Create;
  26. Group1.Add('User1');
  27. Group1.Add('User2');
  28. Group1.Add('User3');
  29. Group2.Add('User3');
  30. Group2.Add('User4');
  31. Group2.Add('User5');
  32. WriteLnHashSet('Group1', Group1);
  33. WriteLnHashSet('Group2', Group2);
  34. WriteLn('< Group3 IntersectWith Group2 >');
  35. Group3 := THashSet<string>.Create(Group1);
  36. Group3.IntersectWith(Group2);
  37. WriteLnHashSet('Group3', Group3);
  38. Group3.Free;
  39. Group2.Free;
  40. Group1.Free;
  41. ReadLn;
  42. end.