Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Arcee Palabrica
Arcee Palabrica
8,100 Points

What's a "key"?

Hey guys, Quick question. What's the role of the key "name" in this code?

intent.putExtra("name", name);

Thanks so much :)

2 Answers

Hi there,

Data is often held in unordered collections in pairs. A key and a value. The data you want is the value which is accessed by passing the corresponding key to the collection.

The data can be anything, from a simple string to large chunks of data. The key is usually a simple representation of what it is accessing.

So, Intents can have multiple Extras. They are each made up of a key & a value. You access the piece of extra you want by passing the key in; the data you want is then returned. Unlike an ordered collection, e.g. an array where you can access the first, second, 50th element, unordered collections, e.g. dictionaries, are accessed by passing keys to unlock the value.

I hope that helps!

Steve.

Arcee Palabrica
Arcee Palabrica
8,100 Points

Steve Hunter Alright thanks man! that explains it well... So when you say "a simple representation" of what it's accessing you mean you can name it whatever you want right?

Absolutely, yes. You can call it whatever you want. Here's a dictionary in the Swift language:

var iceCream = ["CC":"Chocolate Chip", "AP": "Apple Pie", "PB": "PeanutButter"]

Python would be similar:

values = {"name": "Michelangelo", "food": "PIZZA"}

Ruby would look like:

stuff = {'name' => 'Zed', 'age' => 39, 'height' => 6 * 12 + 2}

:smile:

Arcee Palabrica
Arcee Palabrica
8,100 Points

Steve Hunter awesome! thanks again man... have a great day! :)

:+1: