You can get one of those plastic contraptions that encases the cap within it so it is safe to toss into the washing machine OR you can put the cap in the top rack of your dishwasher. (You can probably find the thing at big box stores such as Target or Walmart or try a sporting goods store. They don't cost much.)
Either way should work.
But if there are any heavily stained areas, be sure to scrub them beforehand so as to make it easier for the hat to come clean in the wash cycle. (You can just use an old toothbrush and a mild soap and water solution, no need to use special laundry treatment products.)