Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Exploring the Java Collection Framework Sets

Martin Gallauner
Martin Gallauner
10,808 Points

Why import java.util.Set and java.util.HashSet?

Hi

I'm not fully understand why I have to import Set and HashSet since the class HashSet implements the Set Interface?

Look forward to any help

Martin

2 Answers

Tri Pham
Tri Pham
18,671 Points

If you did HashSet<String> set = new HashSet<String>() instead of Set<String> set = new HashSet<String>(), then you wouldn't need to import Set.

Sławomir Lasik
Sławomir Lasik
7,792 Points

implements key word provide the class everything that she uses. The "Implementation" of an Set interface is in java.util.Set; and "implementation" of HashSet is in java.util.HashSet; So you must include both of them in the when you use them in your class.

Here is a good explanation from JavaTutorial