1 hashmap 的遍历
Map<String, String> map = new HashMap();
map.put("aa", "@sohu.com");
map.put("bb", "@163.com");
map.put("cc", "@sina.com");
System.out.println("普通的遍历方法,通过Map.keySet遍历key和value");//普通使用,二次取值
for (String key : map.keySet()) {
System.out.println("key= "+key+" and value= "+map.get(key));
}
System.out.println("----------java8遍历------------");
map.keySet().forEach(key -> System.out.println("map.get(" + key + ") = " + map.get(key)));
System.out.println("通过Map.entrySet使用iterator遍历key和value:");
Iterator<Map.Entry<String, String>> it= map.entrySet().iterator();
while(it.hasNext()) {
Map.Entry<String, String> entry=it.next();
System.out.println("key="+entry.getKey()+"and value="+entry.getValue());
}
System.out.println("通过Map.entrySet遍历key和value"); //推荐这种,特别是容量大的时候
for(Map.Entry<String, String> entry:map.entrySet()) {
System.out.println("key= "+entry.getKey()+" and value= "+entry.getValue());
}
System.out.println("-------java8--------");
map.entrySet().forEach(entry -> System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue()));
System.out.println("-------onlyJava8-----------");
map.forEach((k,v)->System.out.println("k:v"+k+":"+v));
2 模板方法
package T.T;
/**
* 汽车模型
* 模型模式
* @author Administrator
* 模板方法模式通过把不变的行为搬移到超类,去除了子类中的重复代码。
子类实现算法的某些细节,有助于算法的扩展。
通过一个父类调用子类实现的操作,通过子类扩展增加新的行为,符合“开放-封闭原则”
*/
public abstract class CarModel {
/**
* 汽车启动
*/
protected abstract void start();
/**
* 停车
*/
protected abstract void stop();
/**
* 用户并不需要关注你的车怎么启动或者停下来的,可以开可以停就可以啦
*/
final public void excet(){
this.start();
this.stop();
}
}
package T.T;
public class Ocar extends CarModel{
@Override
protected void start() {
System.out.println("奥迪 无匙启动 突突突");
}
@Override
protected void stop() {
System.out.println("奥迪 停车 ");
}
}
package T.T;
public class Wcar extends CarModel{
@Override
protected void start() {
System.out.println("大众车启动 。。。。。。。。突突突");
}
@Override
protected void stop() {
System.out.println("大众车停车。。。。。。。。。");
}
}