i am trying to store something but i can't figure out how, i have searched and read a lot but i still have one problem.
problem is, if i use: if(myPreferences.getBoolean(k, true) == true) when i am trying to get my preferences with key "k" why i have to add there value true/false? how can i get stored value if i wrote into get method new one next to the old key?
i am trying to get the stored value, why add new one? i guess i don't get the concept? idk.
hope it's clear, thanks a lot for answers :-)
The second parameter on your getBoolean call is the default value if k doesn't exist yet (documentation reference).
Presumably, your software requires a preference to be set to some value, and you'll supply a reasonable default.
If you want to test if a preference exists at all, you can use something like nodeExists().
Related
So i've got a bot that serves as a roleplaying mamager handeling combat, skill points and the like, i'm trying to make my code a bit more general so i can have less pages since they all do the same thing they just have different initilizers but i ran into a snag i need to check if the user has a minimum in a particular stat Strength, perceptions, agility, etc
so i call
mainSPECIAL = rows[0].Strength;
Here's the rub, weathers it strength, percpetion, intelligence, luck, whatever i'm always going to be checking Rows[0].that attribute ie Rows[0].Luck for luck perks, and i already set earlier in my initilizers
var PERKSPECIALName = "Strength";
But i can't call
mainSPECIAL = rows[0].PERKSPECIALName but there should be a way to do that right? so that when it sees "rows[0].PERKSPECIALName" it looks up "PERKSPECIALName" and then fetches the value of rows[0].Strength
For this you need to use reflection:
Field f1 = rows[0].getClass().getField(PERKSPECIALName);
Integer attribute = (Integer) f1.get(rows[0]);
Where "Integer" is the type of the element your pulling from the object (the type of strength)
The field must be declared as public! I think there is a way to obtain them when they are not public but it requires more code.
Seems like you have a set of integers that you need to identify with a constant identifier. You might find an EnumMap useful. Have a look at How to use enumMap in java.
Or if you want to only use a string to identify which perk you want to reference, just use a Map.
Java doesn't have reference-to-member like some other languages, so if you don't want to change your data structure, you are looking at using lambda functions or heavier language features to increase re-use, which seems like overkill for what you're trying to do.
I've been working on Neo4j recently and i know basic rules and how to select property names. However , i need to get the identifier name.
Here is the code:
CREATE (Jugan:Person {name:'George'})
I DON'T want to find 'George' name, but i wanna get the identifier name that is 'Jugan'.
When i write something with " match and return " stuff , i wanna get this "Jugan" name. I hope i explained clearly.
Identifiers are not persisted at all. Their lifetime is just the current statement and their main usage is to refer back to a known node e.g. for returning them.
So no luck finding Jugan in your example. Introduce a property for this.
This is called a label. When you return you can use the LABELS() function like so:
RETURN labels(node)
Since nodes can have zero or more labels, this will give you an array.
Might look weird but want to know if this is possible someway in java.
I have an entry(entrya) and I want to set the value to dao setter based on key of entrya, so basically, I want to set a value something like
myDao.set**SomeKeyValue**(entrya.getValue())
where SomeKeyValue value should be entrya.getKey()
Thanks in advance
I have kind of a really strange problem. I have a simple Map called vectors where I store StrategyPairs as keys and Vectors as the values. When I print it, I get this result:
{net.softwarepage.facharbeit.normalgame.logic.StrategyPair#131e56d7=(1.0;2.0), net.softwarepage.facharbeit.normalgame.logic.StrategyPair#1e1bc985=(2.0;2.0), net.softwarepage.facharbeit.normalgame.logic.StrategyPair#d5415975=(0.0;2.0), net.softwarepage.facharbeit.normalgame.logic.StrategyPair#5bf8c6e7=(2.0;1.0)}
As you can see StrategyPair#131e56d7 is mapped to a Vector (1,2).
Now I create a new StrategyPair. When I print it I get StrategyPair#131e56d7 (the same one as before).
However, if I now call vectors.get(strategyPair) it returns null.
This is somehow really strange as the key is the same (at least it prints the exact same thing out when I print it...)
The problem arises when I rename a strategy, e.g. I change the property name in the class "Strategy". Then suddenly the map which contains StrategyPairs (a wrapper class for two strategies) is messed up as I explained before...
EDIT:
When I print the HashMap I still get the same result as above, but the following code:
for(StrategyPair pair : vectors.keySet()) {
System.out.println(vectors.get(pair));
}
returns:
null
(2.0;2.0)
null
(2.0;1.0)
As #Rajendra Gujja mentioned in a comment, the "hashcode of your keys should not change after you keep them in the map". This is very true; once I changed all the hashcodes to simply use a UUID instead of the name property which changes, the problem is solved. Thanks for all of your answers!
This question already has answers here:
Dynamic variable names Java
(3 answers)
Closed 9 years ago.
I have a list of objects and I'll need to access a specific one based off of random user input corresponding to an id. My limited knowledge at this point led me to something like:
String id;
if(id.equals("apple")){ //fixed silly error, thanks
return objectList.id.memberName;
} //etc...
Is this possible, or is there a better approach?
Sounds like you want a HashMap.
It works like a telephone directory - you put in keys and values, and use the key to look up a value (much like you'd use someone's name to look up their number in a telephone directory.)
Your example also wouldn't work at all, because you're using = in the if statement, which is an assignment statement rather than an equality check. == is what you were probably trying to get, but in this case you shouldn't use that either - instead use .equals() which you should always use when strings are concerned.)
HashMap<String, SomeValueObject> myMap = new HashMap<String, SomeValueObject>();
myMap.put("apple", value);
String id;
if (id.equals("apple")) {
return myMap.get("apple");
}
Essentially you instantiate a HashMap that has two parts to it; a Key and a Value (respectively. When I do myMap.get("apple"), it searches for the KEY "apple". I did myMap.put("apple", value) which makes the Key "apple" be mapped to a certain value that you want.
Also you need to use id.equals("apple") because a String is an object, and if you tried id == "apple" (which I think you meant), it will not work because it will not compare the value of the String but rather the address of it.
#berry120 has provided a practical answer. In general, it sounds like you want to lookup values based on some key. Look at the Map interface and what the JDK offers for concrete implementations.
The value of a variable is only available at run time. On the other hand, a variable name must be known at compile time. So no, you cannot do this exactly the way you are asking. Using a Map will probably be the best solution to your problem.