下面列出了java.util.Hashtable#contains ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) {
//create Hashtable object
Hashtable ht = new Hashtable();
//add key value pairs to Hashtable
ht.put("1", "One");
ht.put("2", "Two");
ht.put("3", "Three");
/*
To check whether a particular value exists in Hashtable use
boolean contains(Object key) method of Hashtable class.
It returns true if the value is mapped to one or more keys in the
Hashtable otherwise false.
*/
boolean blnExists = ht.contains("Two");
System.out.println("Two exists in Hashtable ? : " + blnExists);
}
public static void main(String[] args) {
//create Hashtable object
Hashtable ht = new Hashtable();
//add key value pairs to Hashtable
ht.put("1", "One");
ht.put("2", "Two");
ht.put("3", "Three");
/*
To check whether a particular value exists in Hashtable use
boolean contains(Object key) method of Hashtable class.
It returns true if the value is mapped to one or more keys in the
Hashtable otherwise false.
*/
boolean blnExists = ht.contains("Two");
System.out.println("Two exists in Hashtable ? : " + blnExists);
}
/**
* Check user's number with the system hidden number return the outcome with
* xAyB. x is the correct guess with the same digit in the exact position. y is
* the correct guess with the same digit but different position.
*
* @return outcome as string
*/
public String check(int[] digits) {
int numberOfA = 0;
int numberOfB = 0;
// table to record any checked digit
Hashtable<Integer, Integer> table = new Hashtable<Integer, Integer>();
// random numbers
int[] hiddenNumbers = numbers.getNumbers();
for (int i = 0; i < digits.length; i++) {
if (!table.contains(digits[i])) {
boolean foundA = false;
boolean foundB = false;
for (int j = 0; j < hiddenNumbers.length; j++) {
if (digits[i] == hiddenNumbers[j]) { // found this digit in the hidden number
if (i == j) { // same position
foundA = true;
} else { // different position
foundB = true;
}
}
}
if (foundA) { // same position
numberOfA++;
table.put(digits[i], digits[i]); // record this digit to avoid checking the same digit
} else {
if (foundB) { // different position
numberOfB++;
table.put(digits[i], digits[i]); // record this digit to avoid checking the same digit
}
}
}
}
return numberOfA + "A" + numberOfB + "B";
}
/**
* Check user's number with the system hidden number return the outcome with
* xAyB. x is the correct guess with the same digit in the exact position. y is
* the correct guess with the same digit but different position.
*
* @return outcome as string
*/
public String check(int[] digits) {
int numberOfA = 0;
int numberOfB = 0;
// table to record any checked digit
Hashtable<Integer, Integer> table = new Hashtable<Integer, Integer>();
for (int i = 0; i < digits.length; i++) {
if (!table.contains(digits[i])) {
boolean foundA = false;
boolean foundB = false;
for (int j = 0; j < numbers.length; j++) {
if (digits[i] == numbers[j]) { // found this digit in the hidden number
if (i == j) { // same position
foundA = true;
} else { // different position
foundB = true;
}
}
}
if (foundA) { // same position
numberOfA++;
table.put(digits[i], digits[i]); // record this digit to avoid checking the same digit
} else {
if (foundB) { // different position
numberOfB++;
table.put(digits[i], digits[i]); // record this digit to avoid checking the same digit
}
}
}
}
return numberOfA + "A" + numberOfB + "B";
}
/**
* Check user's number with the system hidden number return the outcome with
* xAyB. x is the correct guess with the same digit in the exact position. y is
* the correct guess with the same digit but different position.
*
* @return outcome as string
*/
public String check(int[] digits) {
int numberOfA = 0;
int numberOfB = 0;
// table to record any checked digit
Hashtable<Integer, Integer> table = new Hashtable<Integer, Integer>();
// random numbers
int[] hiddenNumbers = numbers.getNumbers();
for (int i = 0; i < digits.length; i++) {
if (!table.contains(digits[i])) {
boolean foundA = false;
boolean foundB = false;
for (int j = 0; j < hiddenNumbers.length; j++) {
if (digits[i] == hiddenNumbers[j]) { // found this digit in the hidden number
if (i == j) { // same position
foundA = true;
} else { // different position
foundB = true;
}
}
}
if (foundA) { // same position
numberOfA++;
table.put(digits[i], digits[i]); // record this digit to avoid checking the same digit
} else {
if (foundB) { // different position
numberOfB++;
table.put(digits[i], digits[i]); // record this digit to avoid checking the same digit
}
}
}
}
return numberOfA + "A" + numberOfB + "B";
}
private void saveLookupLists() throws FileNotFoundException {
PrintWriter pwCommands = new PrintWriter(commandDocPluginSaveFolder.getAbsolutePath() + "/lookup.list");
PrintWriter pwFunctions = new PrintWriter(functionDocPluginSaveFolder.getAbsolutePath() + "/lookup.list");
Hashtable<String, String> ignoredCommands = commandsDocRetriever.getIgnoredCommands();
for (PsiElementLinkType type : PsiElementLinkType.allTypes) {
if (type.type.equals(PSI_ELE_TYPE_COMMAND)) {
for (String command : type.linkNames) {
if (!ignoredCommands.contains(command)) {
pwCommands.println(command);
}
}
} else if (type.type.equals(PSI_ELE_TYPE_FUNCTION)) {
for (String function : type.linkNames) {
pwFunctions.println(function);
}
} else {
throw new IllegalStateException("dude");
}
}
pwCommands.flush();
pwCommands.close();
pwFunctions.flush();
pwFunctions.close();
System.out.println("\n\nSaved lookup lists");
}
public void hashtablesCantContainNull(Hashtable h) {
h.put("a", null);
h.put(null, "a");
h.get(null);
h.contains(null);
h.containsKey(null);
h.containsValue(null);
h.remove(null);
}
private void saveLookupLists() throws FileNotFoundException {
PrintWriter pwCommands = new PrintWriter(commandDocPluginSaveFolder.getAbsolutePath() + "/lookup.list");
PrintWriter pwFunctions = new PrintWriter(functionDocPluginSaveFolder.getAbsolutePath() + "/lookup.list");
Hashtable<String, String> ignoredCommands = commandsDocRetriever.getIgnoredCommands();
for (PsiElementLinkType type : PsiElementLinkType.allTypes) {
if (type.type.equals(PSI_ELE_TYPE_COMMAND)) {
for (String command : type.linkNames) {
if (!ignoredCommands.contains(command)) {
pwCommands.println(command);
}
}
} else if (type.type.equals(PSI_ELE_TYPE_FUNCTION)) {
for (String function : type.linkNames) {
pwFunctions.println(function);
}
} else {
throw new IllegalStateException("dude");
}
}
pwCommands.flush();
pwCommands.close();
pwFunctions.flush();
pwFunctions.close();
System.out.println("\n\nSaved lookup lists");
}
private void addToListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addToListActionPerformed
String name = (String)componentName.getSelectedItem();
if(name.length() > 0) {
name += ".";
}
switch(style.getSelectedIndex()) {
case 0:
name += "sel#";
break;
case 2:
name += "press#";
break;
case 3:
name += "dis#";
break;
}
name += "border";
if(!((DefaultListModel)appliesTo.getModel()).contains(name)) {
Hashtable h = res.getTheme(theme);
if(h.contains(name)) {
if(JOptionPane.showConfirmDialog(this,
"A border already exists for this component,\ndo you want to replace it?",
"Already Exists",
JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
return;
}
}
((DefaultListModel)appliesTo.getModel()).addElement(name);
}
}