Liondancer
Liondancer

Reputation: 16469

insert into Hashtable<Character, List<Boolean>>

I am not sure why I am getting the error under my put:

The method put(Character, List<Boolean>) in the type 
Hashtable<Character,List<Boolean>> is not applicable for the arguments (char, boolean)

I think I have all the matching types

Here is my code

    Hashtable<Character, List<Boolean>> charCheck =
         new Hashtable<Character, List<Boolean>>();
    List<Boolean> used = new ArrayList<Boolean>();
    //Set<String> nonDuplicate = new Set<String>();
    // make a hashtable of characters used
    char[] charArray = str.toCharArray(); 
    for (char c : charArray) {
        charCheck.put(c, used.add(false));

Upvotes: 1

Views: 1254

Answers (2)

PNS
PNS

Reputation: 19905

You need to treat each ArrayList separately, i.e. have one ArrayList per Character key.

List<Boolean> used;

char[] charArray = str.toCharArray(); 

for (char c : charArray) {

    used = charCheck.get(c); // Get the list of values for this character

    if (used == null) { // No values stored so far for this character
        used = new ArrayList<Boolean>(); // Create a new (empty) list of values
        charCheck.put(c, used); // Add the empty list of values to the map
    }

    used.add(false); // Add the value for this character to the value list
}

Also, I would suggest using a HashMap instead of a Hashtable:

HashMap<Character, List<Boolean>> charCheck = new HashMap<Character, List<Boolean>>();

Upvotes: 1

Mureinik
Mureinik

Reputation: 311528

The List#add method in Java returns a boolean indication of whether the value ws successfully added to the List or not. You should separate adding a new List to your Map from adding a new element to it:

Hashtable<Character, List<Boolean>> charCheck = new Hashtable<Character, List<Boolean>>();
char[] charArray = str.toCharArray(); 

for (char c : charArray) {
    List<Boolean> used = charCheck.get(c);

    // If the char isn't in the map yet, add a new list
    if (used == null) {
        used = new ArrayList<Boolean>();
        charCheck.put (c, used);
    }

    used.add(false);
}

Upvotes: 2

Related Questions