下面列出了java.util.Hashtable#keySet ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void dump(Writer w, Hashtable<String, Color> colors)
throws IOException
{
PrintWriter pw = new PrintWriter(w);
pw.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
pw.println("<resources>");
for (String key : colors.keySet())
{
Color value = colors.get(key);
String clr = String.format("#%02x%02x%02x%02x", value.getAlpha(),
value.getRed(), value.getGreen(), value.getBlue());
pw.println("<color name=\"" + key + "\">" + clr + "</color>");
}
pw.println("</resources>");
pw.flush();
w.flush();
}
/**
* Horizontal edges cause PROBLEMS!
*/
public void testHorizontal2() {
LineSweep dba = new LineSweep();
// showed a problem
ILineSegment[] segments = new ILineSegment[]{
new TwoDLineSegment(new TwoDPoint(2,20),new TwoDPoint(5,17)),
new TwoDLineSegment(new TwoDPoint(0,18),new TwoDPoint(6,18)),
new TwoDLineSegment(new TwoDPoint(6, 3),new TwoDPoint(8,4)),
};
Hashtable<IPoint, List<ILineSegment>> res = dba.intersections(segments);
for (IPoint ip : res.keySet()) {
List<ILineSegment> ilss = res.get(ip);
System.out.println (ip);
for (ILineSegment ils : ilss) {
System.out.println (" " + ils);
}
System.out.println();
}
assertEquals (1, res.size());
}
public boolean isValidInitialProbabilities(Vector<String> states, Hashtable<String, Double> initialProbabilities) {
if (states.size() != initialProbabilities.size())
return false;
for (int i = 0; i < states.size(); i++) {
boolean found = false;
for (String state : initialProbabilities.keySet()) {
if (state.equals(states.get(i))) {
found = true;
break;
}
}
if (!found)
return false;
}
return true;
}
/**
* Given an input file of "a b c d" values representing points of each
* line segment, generate all intersections and produce a report.
*
* @param f
*/
public boolean executeFile (File f) {
LineSweep dba = new LineSweep();
ArrayList<ILineSegment> als = new ArrayList<ILineSegment>();
try {
Scanner sc = new Scanner (f);
while (sc.hasNext()) {
double[]vals = new double[4];
for(int i = 0; i < vals.length;i++) {
vals[i] = sc.nextDouble();
}
als.add(new TwoDLineSegment(new TwoDPoint(vals[0], vals[1]),
new TwoDPoint(vals[2], vals[3])));
}
sc.close();
} catch (FileNotFoundException fnfe) {
System.err.println ("Unable to locate file:" + f);
return false;
}
Hashtable<IPoint, List<ILineSegment>> res = dba.intersections(als.iterator());
for (IPoint ip : res.keySet()) {
List<ILineSegment> ilss = res.get(ip);
System.out.println (ip);
for (ILineSegment ils : ilss) {
System.out.println (" " + ils);
}
System.out.println();
}
// something was calculated.
return true;
}
public static void dump(Writer w, Hashtable<String, String> strings)
throws IOException
{
PrintWriter pw = new PrintWriter(w);
pw.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
pw.println("<resources>");
for (String key : strings.keySet())
{
String value = strings.get(key);
pw.println("<string name=\"" + key + "\">" + value + "</string>");
}
pw.println("</resources>");
pw.flush();
w.flush();
}
/**
* Adds the totals of newTable into mainTable. Presumes keys are identical.
*/
private static void mergeTables(
Hashtable<String, int[]> newTable, Hashtable<String, int[]> mainTable) {
for (String key : mainTable.keySet()) {
int[] values = mainTable.get(key);
int[] newValues = newTable.get(key);
for (int i = 0; i < mainTable.get(key).length; ++i) {
values[i] += newValues[i];
}
}
}
private static void mergeTables(Hashtable<? super String, Object> props1,
Hashtable<? super String, Object> props2) {
for (Object key : props2.keySet()) {
String prop = (String)key;
Object val1 = props1.get(prop);
if (val1 == null) {
props1.put(prop, props2.get(prop));
} else if (isListProperty(prop)) {
String val2 = (String)props2.get(prop);
props1.put(prop, ((String)val1) + ":" + val2);
}
}
}
/**
* convertRealmConfigs: Maps the Object graph that we get from JNI to the
* object graph that Config expects. Also the items inside the kdc array
* are wrapped inside Hashtables
*/
@SuppressWarnings("unchecked")
private static Hashtable<String, Object>
convertRealmConfigs(Hashtable<String, ?> configs) {
Hashtable<String, Object> realmsTable = new Hashtable<String, Object>();
for (String realm : configs.keySet()) {
// get the kdc
Hashtable<String, Collection<?>> map =
(Hashtable<String, Collection<?>>) configs.get(realm);
Hashtable<String, Vector<String>> realmMap =
new Hashtable<String, Vector<String>>();
// put the kdc into the realmMap
Collection<Hashtable<String, String>> kdc =
(Collection<Hashtable<String, String>>) map.get("kdc");
if (kdc != null) realmMap.put("kdc", unwrapHost(kdc));
// put the admin server into the realmMap
Collection<Hashtable<String, String>> kadmin =
(Collection<Hashtable<String, String>>) map.get("kadmin");
if (kadmin != null) realmMap.put("admin_server", unwrapHost(kadmin));
// add the full entry to the realmTable
realmsTable.put(realm, realmMap);
}
return realmsTable;
}
private static void mergeTables(Hashtable<? super String, Object> props1,
Hashtable<? super String, Object> props2) {
for (Object key : props2.keySet()) {
String prop = (String)key;
Object val1 = props1.get(prop);
if (val1 == null) {
props1.put(prop, props2.get(prop));
} else if (isListProperty(prop)) {
String val2 = (String)props2.get(prop);
props1.put(prop, ((String)val1) + ":" + val2);
}
}
}
private static void mergeTables(Hashtable<? super String, Object> props1,
Hashtable<? super String, Object> props2) {
for (Object key : props2.keySet()) {
String prop = (String)key;
Object val1 = props1.get(prop);
if (val1 == null) {
props1.put(prop, props2.get(prop));
} else if (isListProperty(prop)) {
String val2 = (String)props2.get(prop);
props1.put(prop, ((String)val1) + ":" + val2);
}
}
}
public boolean isValidEmissionMatrix(Hashtable<Pair<String, String>, Double> emissionMatrix, Vector<String> states, Vector<String> observations) {
if (emissionMatrix.size() != observations.size() * states.size()) {
return false;
}
for (Pair<String, String> item : emissionMatrix.keySet()) {
boolean found = false;
double sum = 0.0;
int count = 0;
for (int i = 0; i < states.size(); i++) {
for (int j = 0; j < observations.size(); j++) {
if (item.getKey().equals(states.get(i)) && item.getValue().equals(observations.get(j))) {
found = true;
break;
}
}
if (found)
break;
}
if (!found)
return false;
for (Pair<String, String> item2 : emissionMatrix.keySet()) {
if (item.getKey().equals(item2.getKey())) {
sum += emissionMatrix.get(item2);
count++;
}
}
if (sum != 1.0 && count > 0)
return false;
}
return true;
}
@SuppressWarnings("unchecked")
private static void WrapAllStringInVector(
Hashtable<String, Object> stanzaTable) {
for (String s: stanzaTable.keySet()) {
Object v = stanzaTable.get(s);
if (v instanceof Hashtable) {
WrapAllStringInVector((Hashtable<String,Object>)v);
} else if (v instanceof String) {
Vector<String> vec = new Vector<>();
vec.add((String)v);
stanzaTable.put(s, vec);
}
}
}
private void removeMultiConstants(Hashtable h) {
for(Object k : h.keySet()) {
String key = (String)k;
if(key.startsWith("@")) {
Object val = h.get(k);
if(val instanceof MultiImage) {
h.put(k, ((MultiImage)val).getBest());
removeMultiConstants(h);
return;
}
}
}
}
private static void mergeTables(Hashtable<? super String, Object> props1,
Hashtable<? super String, Object> props2) {
for (Object key : props2.keySet()) {
String prop = (String)key;
Object val1 = props1.get(prop);
if (val1 == null) {
props1.put(prop, props2.get(prop));
} else if (isListProperty(prop)) {
String val2 = (String)props2.get(prop);
props1.put(prop, ((String)val1) + ":" + val2);
}
}
}
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");
/*
get Set of keys contained in Hashtable using
Set keySet() method of Hashtable class
*/
Set st = ht.keySet();
System.out.println("Set created from Hashtable Keys contains :");
//iterate through the Set of keys
Iterator itr = st.iterator();
while (itr.hasNext()) System.out.println(itr.next());
/*
Please note that resultant Set object is backed by the Hashtable.
Any key that is removed from Set will also be removed from
original Hashtable object. The same is not the case with the element
addition.
*/
//remove 2 from Set
st.remove("2");
//print keys of original Hashtable
System.out.println("Hashtable keys after removal from Set are :");
Enumeration e = ht.keys();
while (e.hasMoreElements()) System.out.println(e.nextElement());
}
/**
* Builds the URI from the URI parameter hash
* <p>
* This will append each of the {key, value} pairs in uriParamsHash to the URI.
*
* @param uriParamsHash the hashtable containing parameters to be added to the URI for making the call to TMDB
* @return URI for making the HTTP call to TMDB API
* @throws URISyntaxException if the uri being built is in the incorrect format
*/
private static URI buildUriStringFromParamsHash(Hashtable<String, String> uriParamsHash, String path) throws URISyntaxException {
URIBuilder urib = new URIBuilder();
urib.setScheme("http"); //$NON-NLS-1$
urib.setHost(TMDB_BASE_URL);
urib.setPath(path);
urib.addParameter("api_key", themoviedbapikey); //$NON-NLS-1$
if (uriParamsHash != null) {
Set<String> keys = uriParamsHash.keySet();
for (String key : keys) {
urib.addParameter(key, uriParamsHash.get(key));
}
}
return urib.build();
}
@SuppressWarnings("unchecked")
public static void dumpHashtable(final Log log, final Hashtable table){
Set<String> keys = table.keySet();
log.log("Dump Hashtable\n", Log.INFOS);
for (String key : keys) {
log.log(key + ": " + table.get(key).toString(), Log.INFOS);
}
log.log("End dump\n", Log.INFOS);
}
private SpannableStringBuilder injectBadges(XC_MethodHook.MethodHookParam param, Class mediaSpanClass, SpannableStringBuilder chatMsg, Hashtable customBadges) {
String chatSender = (String) callMethod(param.args[0], "getDisplayName");
int location = chatMsg.toString().indexOf(chatSender);
if (location == -1) { return chatMsg; }
int badgeCount;
if (location == 0) {
badgeCount = location;
} else {
badgeCount = chatMsg.toString().substring(0, location - 1).split(" ").length;
}
for (Object key : customBadges.keySet()) {
if (badgeCount >= 3) {
// Already at 3 badges, anymore will clog up chat box
return chatMsg;
}
String keyString = (String) key;
if (preferences.hiddenBadges().contains(keyString)) {
continue;
}
if (!((ArrayList) ((Hashtable) customBadges.get(keyString)).get("users")).contains(chatSender)) {
continue;
}
String url = (String) ((Hashtable) customBadges.get(keyString)).get("image");
SpannableString badgeSpan = (SpannableString) callMethod(param.thisObject, "a", param.thisObject, url, Enum.valueOf(mediaSpanClass, "Badge"), keyString + " ", null, true, 8, null);
chatMsg.insert(location, badgeSpan);
location += badgeSpan.length();
badgeCount++;
}
return chatMsg;
}
@Override
public void execute(Wandora wandora, Context context) throws TopicMapException {
Object contextSource = context.getContextSource();
if(contextSource instanceof OccurrenceTable) {
OccurrenceTable ot = (OccurrenceTable) contextSource;
ot.duplicateType();
}
else {
Iterator<Topic> topics = null;
if(masterTopic != null) {
ArrayList<Topic> topicArray = new ArrayList();
topicArray.add(masterTopic);
topics = topicArray.iterator();
}
else {
topics = context.getContextObjects();
}
if(topics != null) {
Topic type = occurrenceType;
if(type == null || type.isRemoved()) {
type = wandora.showTopicFinder("Select occurrence type to be duplicated");
}
if(type != null && !type.isRemoved()) {
Topic newType = wandora.showTopicFinder("Select new occurrence type");
if(newType != null && !newType.isRemoved()) {
while(topics.hasNext()) {
Topic topic = topics.next();
if(topic != null && !topic.isRemoved()) {
Hashtable<Topic,String> os = topic.getData(occurrenceType);
if(os != null && !os.isEmpty()) {
for(Topic scope : os.keySet()) {
if(scope != null && !scope.isRemoved()) {
String occurrenceText = os.get(scope);
topic.setData(newType, scope, occurrenceText);
}
}
}
}
}
}
}
}
}
}
public static AndroidSessionWrapper mockEasiestRoute(CommCarePlatform platform, String formNamespace, String selectedValue) {
AndroidSessionWrapper wrapper = null;
int curPredicates = -1;
Hashtable<String, Entry> menuMap = platform.getCommandToEntryMap();
for (String key : menuMap.keySet()) {
Entry e = menuMap.get(key);
if (!(e.isView() || e.isRemoteRequest()) && formNamespace.equals(((FormEntry)e).getXFormNamespace())) {
//We have an entry. Don't worry too much about how we're supposed to get there for now.
//The ideal is that we only need one piece of data
if (e.getSessionDataReqs().size() == 1) {
//This should fit the bill. Single selection.
SessionDatum datum = e.getSessionDataReqs().firstElement();
// we only know how to mock a single case selection
if (datum instanceof ComputedDatum) {
// Allow mocking of routes that need computed data, useful for case creation forms
wrapper = new AndroidSessionWrapper(platform);
wrapper.session.setCommand(platform.getModuleNameForEntry((FormEntry)e));
wrapper.session.setCommand(e.getCommandId());
wrapper.session.setComputedDatum(wrapper.getEvaluationContext());
} else if (datum instanceof EntityDatum) {
EntityDatum entityDatum = (EntityDatum)datum;
//The only thing we need to know now is whether we have a better option available
int countPredicates = CommCareUtil.countPreds(entityDatum.getNodeset());
if (wrapper == null) {
//No previous value! Yay.
//Record the degree of specificity of this selection for now (we'll
//actually create the wrapper later
curPredicates = countPredicates;
} else {
//There's already a path to this form. Only keep going
//if the current choice is less specific
if (countPredicates >= curPredicates) {
continue;
}
}
wrapper = new AndroidSessionWrapper(platform);
wrapper.session.setCommand(platform.getModuleNameForEntry((FormEntry)e));
wrapper.session.setCommand(e.getCommandId());
wrapper.session.setDatum(entityDatum.getDataId(), selectedValue);
}
}
//We don't really have a good thing to do with this yet. For now, just
//hope there's another easy path to this form
}
}
return wrapper;
}