This short speech uses many literary devices. Here are a few samples.
a) "to suffer / The slings and arrows of outrageous fortune"
Metaphor: Unfortunate events that can occur to a person are compared to slings and arrows.
b) "a sea of troubles"
Metaphor: the many troubles that a person might suffer from are compared to a sea
c) "To die, to sleep— / To sleep—perchance to dream"
Anaphora: The repetition of the infinitive form of the verb (to die, to sleep, etc.) can be considered a form of anaphora, or forceful repetition.
d) "The oppressor's wrong, the proud man's contumely,
The pangs of disprized love"
Alliteration: "Proud" and "pangs" are examples of simple alliteration; they are joined by "oppressor's" and "disprized," which have the "p" sound not in the beginning of the word, but in its stressed syllable.
Hamlet compares death to sleep, via metaphor, when he says,
To die, to sleep—
No more—and by a sleep to say we end
The heartache and the thousand natural shocks
That flesh is heir to—’tis a consummation
Devoutly to be wished!
Comparing death to sleep seems to lessen its finality, its permanence, as though Hamlet is trying to talk himself into it. He also uses metonymy, the substitution of a detail associated with a thing for the thing meant, when he describes the word "heartache" to describe emotional pain; we don't actually feel pain in our hearts, but we associate our hearts with emotion.
Moreover, Hamlet uses another metaphor to compare the afterlife to a dream when he says,
To sleep, perchance to dream—ay, there’s the rub,
For in that sleep of death what dreams may come
When we have shuffled off this mortal coil,
Must give us pause.
He means that thoughts of what might await us after death make us worry a little bit about dying (or going "to sleep"). Such concerns might make us hang on to life, painful as it is.
Hamlet's "To be or not to be" soliloquy uses several metaphors, including "to suffer/ The slings and arrows of outrageous fortune." In this metaphor, fortune is compared to an army that throws arrows at one. The next metaphor is "to take arms against a sea of troubles,/ And, by opposing, end them?" In this metaphor, which has become a bit of a cliche, Hamlet likens troubles to a sea in which it is difficult to swim against the tide so that by taking up arms against it, or fighting it, one might die and end one's troubles. In the metaphor, which is an extended metaphor that goes on for several lines--"To die, to sleep"--dying is likened to a long sleep that may or may not bring dreams. That is what worries Hamlet--that death might bring bad dreams.
The next part of the soliloquy features a great deal of anaphora, or the repetition of words at the beginning of clauses. Hamlet speaks of the horrors of life that death will end, including "the whips and scorns of time, /Th' oppressor’s wrong,/ the proud man’s contumely, /The pangs of despised love, the law’s delay," and the list goes on. Time is personified so that it is capable of whipping and scorning people. The word "the" is repeated to emphasize the many horrors present in life.
In a very interesting metaphor, death is compared to "The undiscovered country from whose bourn/ No traveler returns." In other words, death is the country from which no one ever comes back.
This speech in Shakespeare's Hamlet uses several different literary devices. The most important is its overarching generic shape, namely that of a soliloquy, a speech in which a single actor is alone on stage talking to himself in a manner that the audience overhears. In this genre, characters typically reflect on their inner feelings and thus this device works on stage in much the same manner as certain types of narration in novels.
The first metaphors of the speech compare suffering bad fortune to the suffering experienced by victims of war and violence and are found in the lines:
... to suffer
The slings and arrows of outrageous fortune
Or to take arms against a sea of troubles
Slings (i.e. the pellets flung from a slingshot) and arrows are projectile weapons and Hamlet is emphasizing that suffering bad fortune feels like being shot by these weapons. Next, he extends the metaphor by suggesting that one has a choice of simply suffering from being hit by projectiles from a distance or picking up arms (a sword, for example) and closing with ones' enemy for hand-to-hand fighting.
Alliteration, or the repetition of consonant sounds can be found in such phrases as "bare bodkin."
The phrase "shuffled off this mortal coil" is a euphemism for "died."