P. Izzo
P. Izzo

Reputation: 25

Java string array.contains(Object with same attribute name)

I have a string array that gets parameter values from checkboxes in a jsp:

String[] myStringArray = req.getParameterValues("checkboxValues");

and currently a loop to see if any of the strings in the array match the names of attributes I have in a class:

public static class Prefs
    {
        @Getter @Setter private boolean matchingChar;
        @Getter @Setter private boolean acqVals;
        @Getter @Setter private boolean allTrades;
        @Getter @Setter private boolean documents;
    }
for(String value : myStringArray)
            {
                if("Matching Char".contains(value))
                {
                    prefs.setMatchingChar(true);
                }
                else if("Acq Vals".contains(value))
                {
                    prefs.setAcqVals(true);
                }
                else if("All Trades".contains(value))
                {
                    prefs.setAllTrades(true);
                }
                else if("Documents".contains(value))
                {
                    prefs.setDocuments(true);
                }
            }

I realize this is highly inefficient and can cause problems down the line.

Is there is an easier way of checking if the values that come back from the checked-boxes match the names of the attributes?

I'm not sure if something like

for(String value : myStringArray){
    if(Prefs.hasAttribute.contains(value){
        set(thisAttribute)
    }
}

is even possible. I'd like to prevent having to hardcode things every time another attribute is added.

The checkbox values are populated from labels equal to a different but associated class.

Upvotes: 0

Views: 2744

Answers (2)

Jonck van der Kogel
Jonck van der Kogel

Reputation: 3293

You're going to have to make a mapping somewhere, where you need to know which string value corresponds to which preferences attribute, especially if you want it in the form like you have in your sample code, with a Prefs object with boolean values wether or not a flag is set.

You could do something like this which at least gets rid of the ugly list of if statements but it does not rid you of having to map your values to Prefs attributes somewhere.

import lombok.Getter;
import java.util.Arrays;
import java.util.Map;

@Getter
public class Prefs {
    private Map<String, Runnable> actions = Map.of(
            "Matching Char", () -> this.matchingChar = true,
            "Acq Vals", () -> this.acqVals = true,
            "All Trades", () -> this.allTrades = true,
            "Documents", () -> this.documents = true
    );

    public Prefs(String[] myStringArray) {
        Arrays.stream(myStringArray)
                .forEach(s -> actions.get(s).run());
    }

    private boolean matchingChar;
    private boolean acqVals;
    private boolean allTrades;
    private boolean documents;
}

And here a unit test to demonstrate the usage:

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class PrefsTest {
    @Test
    public void demonstrateUsage() {
        String[] myStringArray = {"Acq Vals", "Documents"};

        Prefs prefs = new Prefs(myStringArray);

        assertTrue(prefs.isAcqVals());
        assertTrue(prefs.isDocuments());
        assertFalse(prefs.isAllTrades());
        assertFalse(prefs.isMatchingChar());
    }
}

Upvotes: 1

Sachin Mamoru
Sachin Mamoru

Reputation: 486

Here you can use the contains() method on the resulting ArrayList, which returns a boolean signifying if the list contains the element we've passed to it or not.

String[] myStringArray= new String[]{"Matching Char", "Acq Vals", "All Trades", "Documents"};

List<String> myStringList = new ArrayList<>(Arrays.asList(myStringArray));

System.out.println(myStringList.contains(value));

Upvotes: 1

Related Questions