下面列出了java.util.Hashtable#elements ( ) 实例代码,或者点击链接到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 remove a key value pair from Hashtable use
Object remove(Object key) method of Hashtable class.
It returns either the value mapped with the key or null if no value
was mapped.
*/
Object obj = ht.remove("2");
System.out.println(obj + " Removed from Hashtable");
//print remaining Hashtable values
Enumeration e = ht.elements();
//iterate through Hashtable values Enumeration
while (e.hasMoreElements()) System.out.println(e.nextElement());
}
/**
* Write forward references for referenced interfaces and valuetypes
* ...but not if the reference is to a boxed IDLEntity,
* @param refHash Hashtable loaded with referenced types
* @param p The output stream.
*/
protected void writeForwardReferences(
Hashtable refHash,
IndentingWriter p )
throws IOException {
Enumeration refEnum = refHash.elements();
nextReference:
while ( refEnum.hasMoreElements() ) {
Type t = (Type)refEnum.nextElement();
if ( t.isCompound() ) {
CompoundType ct = (CompoundType)t;
if ( ct.isIDLEntity() )
continue nextReference; //ignore IDLEntity reference
}
writeForwardReference( t,p );
}
}
public Vector getActiveParticipants()
{
Vector vector1 = new Vector();
RTPSourceInfoCache rtpsourceinfocache = cache.getRTPSICache();
Hashtable hashtable = rtpsourceinfocache.getCacheTable();
for(Enumeration enumeration = hashtable.elements(); enumeration.hasMoreElements();)
{
Participant participant = (Participant)enumeration.nextElement();
if(participant == null || !(participant instanceof LocalParticipant) || !nonparticipating)
{
Vector vector = participant.getStreams();
if(vector.size() > 0)
{
vector1.addElement(participant);
}
}
}
return vector1;
}
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 Enumeration of values contained in Hashtable using
Enumeration elements() method of Hashtable class
*/
Enumeration e = ht.elements();
//iterate through Hashtable values Enumeration
while (e.hasMoreElements()) System.out.println(e.nextElement());
}
@Test
public void filteredHeaderNames() throws Exception {
Hashtable<String, String> hashtable = new Hashtable<String, String>();
hashtable.put("a", "aa");
hashtable.put("b", Header.HTTP_FLAGS.toString());
hashtable.put("c", "cc");
Enumeration<String> elements = hashtable.elements();
Enumeration enumeration = new DelegateEnumeration(elements, Header.FILTER);
int count = 0;
while (enumeration.hasMoreElements()) {
count++;
Assert.assertFalse(Header.startWithPinpointHeader((String) enumeration.nextElement()));
}
Assert.assertEquals(count, 2);
}
/**
* Write includes for boxedRMI valuetypes for IDL sequences.
* Write only the maximum dimension found for an ArrayType.
* @param arrHash Hashtable loaded with array types
* @param p The output stream.
*/
protected void writeBoxedRMIIncludes(
Hashtable arrHash,
IndentingWriter p)
throws IOException {
Enumeration e1 = arrHash.elements();
nextSequence:
while ( e1.hasMoreElements() ) {
ArrayType at = (ArrayType)e1.nextElement();
int dim = at.getArrayDimension();
Type et = at.getElementType();
Enumeration e2 = arrHash.elements();
while ( e2.hasMoreElements() ) { //eliminate duplicates
ArrayType at2 = (ArrayType)e2.nextElement();
if ( et == at2.getElementType() && //same element type &
dim < at2.getArrayDimension() ) //smaller dimension?
continue nextSequence; //ignore this one
}
writeInclude( at,dim,!isThrown,p );
}
}
/**
* Write #includes
* @param incHash Hashtable loaded with Types to include
* @param p The output stream.
*/
protected void writeInheritedIncludes(
Hashtable inhHash,
IndentingWriter p )
throws IOException {
Enumeration inhEnum = inhHash.elements();
while ( inhEnum.hasMoreElements() ) {
CompoundType t = (CompoundType)inhEnum.nextElement();
writeInclude( t,0,!isThrown,p );
}
}
static void entest2(Hashtable ht, int size) {
int sum = 0;
timer.start("Iter Enumeration Value ", size);
for (Enumeration en = ht.elements(); en.hasMoreElements(); ) {
if (en.nextElement() != MISSING)
++sum;
}
timer.finish();
reallyAssert (sum == size);
}
/**
* Write #includes
* @param incHash Hashtable loaded with Types to include
* @param isThrown true if Types are thrown exceptions
* @param p The output stream.
*/
protected void writeIncludes(
Hashtable incHash,
boolean isThrown,
IndentingWriter p )
throws IOException {
Enumeration incEnum = incHash.elements();
while ( incEnum.hasMoreElements() ) {
CompoundType t = (CompoundType)incEnum.nextElement();
writeInclude( t,0,isThrown,p );
}
}
/**
* readObject is called to restore the state of the
* BasicPermissionCollection from a stream.
*/
@java.io.Serial
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException
{
// Don't call defaultReadObject()
// Read in serialized fields
ObjectInputStream.GetField gfields = in.readFields();
// Get permissions
// writeObject writes a Hashtable<String, Permission> for the
// permissions key, so this cast is safe, unless the data is corrupt.
@SuppressWarnings("unchecked")
Hashtable<String, Permission> permissions =
(Hashtable<String, Permission>)gfields.get("permissions", null);
perms = new ConcurrentHashMap<>(permissions.size()*2);
perms.putAll(permissions);
// Get all_allowed
all_allowed = gfields.get("all_allowed", false);
// Get permClass
permClass = (Class<?>) gfields.get("permClass", null);
if (permClass == null) {
// set permClass
Enumeration<Permission> e = permissions.elements();
if (e.hasMoreElements()) {
Permission p = e.nextElement();
permClass = p.getClass();
}
}
}
private static boolean checkAll(Hashtable inboundFlight)
{
Enumeration e = inboundFlight.elements();
while (e.hasMoreElements())
{
if (((DTLSReassembler)e.nextElement()).getBodyIfComplete() == null)
{
return false;
}
}
return true;
}
/**
* readObject is called to restore the state of the
* BasicPermissionCollection from a stream.
*/
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException
{
// Don't call defaultReadObject()
// Read in serialized fields
ObjectInputStream.GetField gfields = in.readFields();
// Get permissions
// writeObject writes a Hashtable<String, Permission> for the
// permissions key, so this cast is safe, unless the data is corrupt.
@SuppressWarnings("unchecked")
Hashtable<String, Permission> permissions =
(Hashtable<String, Permission>)gfields.get("permissions", null);
perms = new HashMap<String, Permission>(permissions.size()*2);
perms.putAll(permissions);
// Get all_allowed
all_allowed = gfields.get("all_allowed", false);
// Get permClass
permClass = (Class<?>) gfields.get("permClass", null);
if (permClass == null) {
// set permClass
Enumeration<Permission> e = permissions.elements();
if (e.hasMoreElements()) {
Permission p = e.nextElement();
permClass = p.getClass();
}
}
}
/**
* Write #includes
* @param incHash Hashtable loaded with Types to include
* @param p The output stream.
*/
protected void writeInheritedIncludes(
Hashtable inhHash,
IndentingWriter p )
throws IOException {
Enumeration inhEnum = inhHash.elements();
while ( inhEnum.hasMoreElements() ) {
CompoundType t = (CompoundType)inhEnum.nextElement();
writeInclude( t,0,!isThrown,p );
}
}
/**
* readObject is called to restore the state of the
* BasicPermissionCollection from a stream.
*/
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException
{
// Don't call defaultReadObject()
// Read in serialized fields
ObjectInputStream.GetField gfields = in.readFields();
// Get permissions
// writeObject writes a Hashtable<String, Permission> for the
// permissions key, so this cast is safe, unless the data is corrupt.
@SuppressWarnings("unchecked")
Hashtable<String, Permission> permissions =
(Hashtable<String, Permission>)gfields.get("permissions", null);
perms = new HashMap<String, Permission>(permissions.size()*2);
perms.putAll(permissions);
// Get all_allowed
all_allowed = gfields.get("all_allowed", false);
// Get permClass
permClass = (Class<?>) gfields.get("permClass", null);
if (permClass == null) {
// set permClass
Enumeration<Permission> e = permissions.elements();
if (e.hasMoreElements()) {
Permission p = e.nextElement();
permClass = p.getClass();
}
}
}
/**
* readObject is called to restore the state of the
* BasicPermissionCollection from a stream.
*/
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException
{
// Don't call defaultReadObject()
// Read in serialized fields
ObjectInputStream.GetField gfields = in.readFields();
// Get permissions
// writeObject writes a Hashtable<String, Permission> for the
// permissions key, so this cast is safe, unless the data is corrupt.
@SuppressWarnings("unchecked")
Hashtable<String, Permission> permissions =
(Hashtable<String, Permission>)gfields.get("permissions", null);
perms = new HashMap<String, Permission>(permissions.size()*2);
perms.putAll(permissions);
// Get all_allowed
all_allowed = gfields.get("all_allowed", false);
// Get permClass
permClass = (Class<?>) gfields.get("permClass", null);
if (permClass == null) {
// set permClass
Enumeration<Permission> e = permissions.elements();
if (e.hasMoreElements()) {
Permission p = e.nextElement();
permClass = p.getClass();
}
}
}
static void entest2(Hashtable ht, int size) {
int sum = 0;
timer.start("Iter Enumeration Value ", size);
for (Enumeration en = ht.elements(); en.hasMoreElements(); ) {
if (en.nextElement() != MISSING)
++sum;
}
timer.finish();
reallyAssert (sum == size);
}
public ReachableObjects(JavaHeapObject root,
final ReachableExcludes excludes) {
this.root = root;
final Hashtable<JavaHeapObject, JavaHeapObject> bag = new Hashtable<JavaHeapObject, JavaHeapObject>();
final Hashtable<String, String> fieldsExcluded = new Hashtable<String, String>(); //Bag<String>
final Hashtable<String, String> fieldsUsed = new Hashtable<String, String>(); // Bag<String>
JavaHeapObjectVisitor visitor = new AbstractJavaHeapObjectVisitor() {
public void visit(JavaHeapObject t) {
// Size is zero for things like integer fields
if (t != null && t.getSize() > 0 && bag.get(t) == null) {
bag.put(t, t);
t.visitReferencedObjects(this);
}
}
public boolean mightExclude() {
return excludes != null;
}
public boolean exclude(JavaClass clazz, JavaField f) {
if (excludes == null) {
return false;
}
String nm = clazz.getName() + "." + f.getName();
if (excludes.isExcluded(nm)) {
fieldsExcluded.put(nm, nm);
return true;
} else {
fieldsUsed.put(nm, nm);
return false;
}
}
};
// Put the closure of root and all objects reachable from root into
// bag (depth first), but don't include root:
visitor.visit(root);
bag.remove(root);
// Now grab the elements into a vector, and sort it in decreasing size
JavaThing[] things = new JavaThing[bag.size()];
int i = 0;
for (Enumeration e = bag.elements(); e.hasMoreElements(); ) {
things[i++] = (JavaThing) e.nextElement();
}
ArraySorter.sort(things, new Comparer() {
public int compare(Object lhs, Object rhs) {
JavaThing left = (JavaThing) lhs;
JavaThing right = (JavaThing) rhs;
int diff = right.getSize() - left.getSize();
if (diff != 0) {
return diff;
}
return left.compareTo(right);
}
});
this.reachables = things;
this.totalSize = root.getSize();
for (i = 0; i < things.length; i++) {
this.totalSize += things[i].getSize();
}
excludedFields = getElements(fieldsExcluded);
usedFields = getElements(fieldsUsed);
}
private final void updateUser(String channel, int userMode, String nick) {
channel = channel.toLowerCase();
synchronized (_channels) {
Hashtable<User, User> users = _channels.get(channel);
User newUser = null;
if( users != null ) {
Enumeration<User> enumeration = users.elements();
while ( enumeration.hasMoreElements() ) {
User userObj = enumeration.nextElement();
if( userObj.getNick().equalsIgnoreCase(nick) ) {
if( userMode == OP_ADD ) {
if( userObj.hasVoice() ) {
newUser = new User("@+", nick);
} else {
newUser = new User("@", nick);
}
} else if( userMode == OP_REMOVE ) {
if( userObj.hasVoice() ) {
newUser = new User("+", nick);
} else {
newUser = new User("", nick);
}
} else if( userMode == VOICE_ADD ) {
if( userObj.isOp() ) {
newUser = new User("@+", nick);
} else {
newUser = new User("+", nick);
}
} else if( userMode == VOICE_REMOVE ) {
if( userObj.isOp() ) {
newUser = new User("@", nick);
} else {
newUser = new User("", nick);
}
} else if( userMode == HALFOP_ADD ) {
if( userObj.hasVoice() ) {
newUser = new User("%+", nick);
} else {
newUser = new User("%", nick);
}
} else if( userMode == HALFOP_REMOVE ) {
if( userObj.hasVoice() ) {
newUser = new User("+", nick);
} else {
newUser = new User("", nick);
}
}
}
}
}
if( newUser != null ) {
users.put(newUser, newUser);
} else {
// just in case ...
newUser = new User("", nick);
users.put(newUser, newUser);
}
}
}
public static Enumeration listCacheObjects()
{
Hashtable h = CacheAccess.getDefault().getCombinedView();
return h.elements();
}
public ReachableObjects(JavaHeapObject root,
final ReachableExcludes excludes) {
this.root = root;
final Hashtable<JavaHeapObject, JavaHeapObject> bag = new Hashtable<JavaHeapObject, JavaHeapObject>();
final Hashtable<String, String> fieldsExcluded = new Hashtable<String, String>(); //Bag<String>
final Hashtable<String, String> fieldsUsed = new Hashtable<String, String>(); // Bag<String>
JavaHeapObjectVisitor visitor = new AbstractJavaHeapObjectVisitor() {
public void visit(JavaHeapObject t) {
// Size is zero for things like integer fields
if (t != null && t.getSize() > 0 && bag.get(t) == null) {
bag.put(t, t);
t.visitReferencedObjects(this);
}
}
public boolean mightExclude() {
return excludes != null;
}
public boolean exclude(JavaClass clazz, JavaField f) {
if (excludes == null) {
return false;
}
String nm = clazz.getName() + "." + f.getName();
if (excludes.isExcluded(nm)) {
fieldsExcluded.put(nm, nm);
return true;
} else {
fieldsUsed.put(nm, nm);
return false;
}
}
};
// Put the closure of root and all objects reachable from root into
// bag (depth first), but don't include root:
visitor.visit(root);
bag.remove(root);
// Now grab the elements into a vector, and sort it in decreasing size
JavaThing[] things = new JavaThing[bag.size()];
int i = 0;
for (Enumeration<JavaHeapObject> e = bag.elements(); e.hasMoreElements(); ) {
things[i++] = (JavaThing) e.nextElement();
}
ArraySorter.sort(things, new Comparer() {
public int compare(Object lhs, Object rhs) {
JavaThing left = (JavaThing) lhs;
JavaThing right = (JavaThing) rhs;
long diff = right.getSize() - left.getSize();
if (diff != 0) {
return Long.signum(diff);
}
return left.compareTo(right);
}
});
this.reachables = things;
this.totalSize = root.getSize();
for (i = 0; i < things.length; i++) {
this.totalSize += things[i].getSize();
}
excludedFields = getElements(fieldsExcluded);
usedFields = getElements(fieldsUsed);
}