0

I have this import statement:

import java.util.List

Input:

public class solution{ List<String> words (String text, List<String> bannedWords) {//BodyOfMethod } }

expected Output: list of strings

My code:

HashSet<String> bannedWords = new HashSet<>();
HashMap<String, Integer> validWordsCount = new HashMap<>();

List<Character> result = List<>();

It says error can't find symbol for the lines above. My syntax is wrong (I think it's out dated, from an old tutorial). Can I write it as Map instead of HashMap?

Can someone please tell me the correct syntax for the the variable to the input and output?

khelwood
  • 55,782
  • 14
  • 81
  • 108
Abraham
  • 19
  • 4

1 Answers1

0
HashSet<String> bannedWords = new HashSet<>();
HashMap<String, Integer> validWordsCount = new HashMap<>();

List<Character> result = List<>();

Typically you would type the variables as the interface types (Set, Map, List), and instantiate them with a particular implementation class (HashSet, HashMap, ArrayList). See What does it mean to “program to an interface”?

So this would be:

Set<String> bannedWords = new HashSet<>();
Map<String, Integer> validWordsCount = new HashMap<>();

List<Character> result = ArrayList<>();

All of these types can be imported from java.util, e.g.

import java.util.*;
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Do I need to import a library for that? I'm still learning it and I just read I might need that @khelwood 'import java.util.ArrayList' it wasn't imported – Abraham Aug 04 '20 at 08:43