Yes in most places it is illegal to collect cans or any other items from a recycling bin.
The items in the recycling bin are considered to be owned by the city, county or whoever picks up the recycling in the recycling bins.
Your local city should be able to tell you but in most cases and most places going through recycling bins is illegal.
Now as for trash cans that are out on the curb going through those trash cans to get cans or other items that are discarded is not illegal.
That is unless the city has made it illegal to go through peoples trash.
But in most places when you put your trash out by the curb it becomes public property and you gave up your rights to ownership of those trash items.