stack的遍历
增强型for循环的遍历方法
for(int path:stack){//这里的path可以随便写什么
System.out.println(path+" ");
}
栈弹出的while遍历方式
while(!stack.empty()){//栈的api中只有empty(),没有Empty()或者isEmpty();
System.out.printn(stack.pop());
}
队列的遍历
增强型for循环
for (Integer x : queue) {
System.out.println(x);
}
队列方式遍历,元素逐个被移除
while(queue.peek() != null) {
System.out.println(q.poll());
}
hashmap的遍历
for(char c:hashmap.keySet()){//前提是hashmap中存的是char类型的数据。
if(hash.get(c)==1)//get(key)得到key对应的值。
return c;
}
如何通过值得到键?
// 因为可能有多个key对应一个value,所以我们把取出来的结果放到一个list中
List<Integer> keyList = new ArrayList<Integer>();
for (Entry<Integer, String> entry : hashmap.entrySet()) {
if ("a".equals(entry.getValue())) {
keyList.add(e.getKey());
}
}