-2
public class MyClass extends Activity {
    public static final String DEFAULT_ID = "def";
    public static final LinkedHashSet<String> DEF_IDS = new LinkedHashSet<>(Arrays.asList(DEFAULT_ID));

    private boolean isDefault(String currentId) {
        Log.v(TAG,"isdefault("+currentId+") = " + DEF_IDS.contains(currentId));
        return DEF_IDS.contains(currentId);
    }
}

In log:

isdefault(profile0) = true

WTF? If DEF_IDS doesn't contain "profile0", why does it says that it contains?

kandi
  • 1,098
  • 2
  • 12
  • 24

2 Answers2

0

The fault is not in the code you posted. The following produces the expected result.

public static final String DEFAULT_ID = "def";
public static final LinkedHashSet<String> DEF_IDS = new LinkedHashSet<>(Arrays.asList(DEFAULT_ID));

private static boolean isDefault(String currentId) {
    return DEF_IDS.contains(currentId);
}

private void test(String def) {
    System.out.println("isDefault(" + def + ") = " + isDefault(def));
}

public void test() {
    test("def");
    test(DEFAULT_ID);
    test("NOT");
}

by printing

isDefault(def) = true
isDefault(def) = true
isDefault(NOT) = false
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

Executed the code below and it gave me the correct result.
Problem seems to be that your using static LinkedHashSet and it will retain previous values untill you'll not explicitly clear it i.e. initialized only once.

Add more details or close your question as it is not at all clear and don't provide the complete context on how exactly you are using this code.

  public static final String DEFAULT_ID = "def";
            public static final LinkedHashSet<String> DEF_IDS = new LinkedHashSet<>(Arrays.asList(DEFAULT_ID));


        public static void main(String[] args){
            isDefault("profile0");
        } 
            private static boolean isDefault(String currentId) {
               System.out.println("isdefault("+currentId+") = " + DEF_IDS.contains(currentId));
                return DEF_IDS.contains(currentId);
            }

Output:- isdefault(profile0) = false

Amit Bhati
  • 5,569
  • 1
  • 24
  • 45