课程: Learning Java Collections

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Map methods

Map methods

- [Instructor] Let's work through a few code examples to get more familiar with the methods on a map. To help us with that, we'll build a HashMap that tracks which room the guests at the London Hotel are staying in. So we'll start out by creating a map that uses a room as its key and stores guests as its values. I'm going to name this map assignments and then I'll use a HashMap implementation for my map. Okay, so now we have the map. Let's go ahead and add the guests into their rooms. So first I'm going to place Maria in Oxford by passing in Oxford as the key and then Maria as the value. Okay, then we can put John in his room. It's going to be Piccadilly, so we'll go ahead and use the put method to associate John with Piccadilly. Now at this point, let's retrieve some of the values in the map and print them to the console. So I'll use the print stream and then I'll just say we're getting the guest from Piccadilly.…

内容