Java Java Data Structures Exploring the Java Collection Framework Sets

Martin Gallauner
Martin Gallauner
10,806 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