遍历Map的两种方式:
Map<String,String> map = new HashMap<String,String> ();
map.put("1", "wang");
map.put("2", "hai");
//第一种遍历方式:返回set视图
Set<Map.Entry<String, String>> entryseSet=map.entrySet();
for (Map.Entry<String, String> entry:entryseSet) {
System.out.println(entry.getKey()+","+entry.getValue());
}
//第二种遍历方式:返回键的set视图 通过键key去取值
Iterator iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
String value = map.get(key);
System.out.println(key+"=======>"+value);
}
第二:线程实现的两种方式
1.使用Thread实现线程
class MyThread extends Thread
{
private String name;
public MyThread(String name ){
this.name=name;
}
public void run(){
for(int i=0;i<10;i++){
System.out.println("线程实现"+name+i);
}
}
}
public class ThreadDemo01
{
public static void main(String args[]){
MyThread A = new MyThread("A"); //实例线程对象
MyThread B = new MyThread("B"); //实例线程对象
A.start();
B.start();
}
}
2、通过实现Runnable接口实现:
//使用Runnable实现线程
class MyThread implements Runnable
{
private String name;
public MyThread(String name){
this.name=name;
}
public void run(){
for(int i=1;i<=10;i++){
System.out.println("线程"+name+i);
}
}
}
public class RunnableDemo01
{
public static void main(String args[]){
MyThread wang = new MyThread("wang"); //实例化线程要执行的任务
MyThread hai = new MyThread("hai"); //实例化线程要执行的任务
Thread Ta = new Thread(A); //实例化线程,并传递其要执行的任务
Thread Tb = new Thread(B); //实例化线程,并传递其要执行的任务
Ta.start(); //启动线程
Tb.start();
}
} |
|