Open ZBDXShenYifan opened 6 years ago
工厂类提供的是建造单个类的模式,而建造者模式则是将各种产品集中起来进行管理,用来建造复合对象,建造者模式将很多功能集成到一个类里,这个类可以创造出比较复杂的东西。
在本例中,Builder具有插入排序,冒泡排序,快速排序和选择排序的功能。
与工厂模式的区分:
工厂模式关注的是建造单个产品,建造者模式则是关注与创建多功能的复合对象。
import java.util.*;
public class Builder
{
private BaseSort sort;
public void readInput(double[] a)
{
System.out.println("请输入" + a.length + "个数字");
Scanner input = new Scanner(System.in);
for(int i = 0; i < a.length; i++)
{
a[i] = input.nextDouble();
}
}
public void writeOutput(double[] a)
{
for(int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ");
}
System.out.println();
}
public void produceBubbleSort(double[] a)
{
this.sort = new BubbleSort();
this.doSort(a);
}
public void produceInsertionSort(double[] a)
{
this.sort = new InsertionSort();
this.doSort(a);
}
public void produceQuickSort(double[] a)
{
this.sort = new QuickSort();
this.doSort(a);
}
public void produceSelectionSort(double[] a)
{
this.sort = new SelectionSort();
this.doSort(a);
}
public void doSort(double[] a)
{
this.readInput(a);
sort.sort(a);
this.writeOutput(a);
}
}
import java.util.Scanner;
public class TestBuilder
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Builder build = new Builder();
double[] a1 = new double[10];
double[] a2 = new double[10];
double[] a3 = new double[10];
double[] a4 = new double[10];
build.produceBubbleSort(a1);
build.produceInsertionSort(a2);
build.produceQuickSort(a3);
build.produceSelectionSort(a4);
}
}
请输入10个数字 1 2 3 6 5 4 9 8 7 0 排序算法 冒泡算法的结果 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 请输入10个数字 9 8 7 6 5 4 3 2 1 0 排序算法 插入算法的结果 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 请输入10个数字
9 8 7 3 2 1 4 5 6 0 排序算法 快速算法的结果 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 请输入10个数字 7 5 3 4 6 8 3 1 0 2 排序算法 选择算法的结果 0.0 1.0 2.0 3.0 3.0 4.0 5.0 6.0 7.0 8.0
接口的default methods只能是public ,抽象类的abstract method可以是protected.
Java 8的接口上的default method最初的设计目的是让已经存在的接口可以演化——添加新方法 而不需要实现该接口的类做任何改变(甚至不需要重新编译),Java 8的接口暂时还无法完全替代抽象类,它不能拥有状态,只能提供公有方法的默认实现。
1.第一种遍历:利用Map.KeySet + foreach遍历value 2.第二种遍历:通过Map.entrySet使用iterator遍历Key和value 3.第三种遍历: 通过Map.entrySet + foreach循环 4.第四种遍历:通过Map.value() + foreach来遍历所有的value
package homework3;
import java.util.*;
public class TestMapIterator
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Map<String, String> studentInformation = new HashMap<>();
studentInformation.put("1614011101", "小张");
studentInformation.put("1614011102", "小赵");
studentInformation.put("1614011103", "小可");
studentInformation.put("1614011104", "小红");
studentInformation.put("1614011105", "小军");
studentInformation.put("1614011106", "小李");
studentInformation.put("1614011107", "小刘");
studentInformation.put("1614011108", "小王");
System.out.println("第一种遍历:利用Map.KeySet遍历value");
//KeySet返回所有Key的值
for(String learningNumber: studentInformation.keySet())
System.out.println("value = " + studentInformation.get(learningNumber));
System.out.println();
System.out.println("第二种遍历:通过Map.entrySet使用iterator遍历Key和value");
Iterator<Map.Entry<String, String>> iterator = studentInformation.entrySet().iterator();
//.entrySet()返回的是键值对的一个集合
//建议一个迭代器对象,它迭代的元素Map中的键值对
while(iterator.hasNext())
{
Map.Entry<String, String> entry = iterator.next();
System.out.println("key = " + entry.getKey() + " value = " + entry.getValue());
}
System.out.println();
System.out.println("第三种遍历: 通过Map.entrySet + foreach循环");
for(Map.Entry<String, String> entry : studentInformation.entrySet())
System.out.println("key = " + entry.getKey() + " value = " + entry.getValue());
System.out.println();
System.out.println("通过Map.value()来遍历所有的value,但不能遍历key");
for(String aValue : studentInformation.values())
System.out.println("value = " + aValue);
}
}
第一种遍历:利用Map.KeySet遍历value value = 小红 value = 小可 value = 小李 value = 小军 value = 小赵 value = 小张 value = 小王 value = 小刘
第二种遍历:通过Map.entrySet使用iterator遍历Key和value key = 1614011104 value = 小红 key = 1614011103 value = 小可 key = 1614011106 value = 小李 key = 1614011105 value = 小军 key = 1614011102 value = 小赵 key = 1614011101 value = 小张 key = 1614011108 value = 小王 key = 1614011107 value = 小刘
第三种遍历: 通过Map.entrySet + foreach循环 key = 1614011104 value = 小红 key = 1614011103 value = 小可 key = 1614011106 value = 小李 key = 1614011105 value = 小军 key = 1614011102 value = 小赵 key = 1614011101 value = 小张 key = 1614011108 value = 小王 key = 1614011107 value = 小刘
通过Map.value()来遍历所有的value,但不能遍历key value = 小红 value = 小可 value = 小李 value = 小军 value = 小赵 value = 小张 value = 小王 value = 小刘
import java.util.*;
import java.util.stream.Stream;
public class TestStream
{
/**
* 原始版本的Iterator,用户只能一个一个的遍历元素并对他执行某些操作,而Stream,用户只需要
* 给出对其包含的元素执行什么操作
*/
public static void main(String[] args)
{
System.out.println("利用stream的建造集合与遍历");
Stream<Integer> integerStream = Stream.of(1,3,3,4,5,6,6,8,9);
//forEach()
integerStream.forEach(number -> System.out.print(number + " "));
System.out.println();
//filter()
System.out.println("stream中过滤出大于等于5的元素");
Stream<Integer> integerStream2 = Stream.of(1,3,3,4,5,6,6,8,9);
integerStream2.filter(number -> number >= 5)
.forEach(number -> System.out.print(number + " "));
System.out.println();
//distinct()
System.out.println("删除重复的元素");
Stream<Integer> integerStream3 = Stream.of(1,3,3,4,5,6,6,8,9);
integerStream3.distinct()
.forEach(number -> System.out.print(number + " "));
System.out.println();
//sorted()
System.out.println("进行降序排序");
Stream<Integer> integerStream4 = Stream.of(1,3,3,4,5,6,6,8,9);
integerStream4.sorted((num1, num2) -> num2 - num1)
.forEach(number -> System.out.print(number + " "));
System.out.println();
//map()
System.out.println("对所有元素进行+1的操作");
Stream<Integer> integerStream5 = Stream.of(1,3,3,4,5,6,6,8,9);
integerStream5.map(number-> number + 1)
.forEach(number -> System.out.print(number + " "));
System.out.println();
// flatMap()
System.out.println("将两个集合合并");
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
list1.add(3);
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(4);
list2.add(5);
list2.add(6);
Stream<ArrayList<Integer>> stream = Stream.of(list1,list2);
stream.flatMap(list -> list.stream())
.forEach(number -> System.out.print(number + " "));
System.out.println();
}
}
利用stream的建造集合与遍历 1 3 3 4 5 6 6 8 9 stream中过滤出大于等于5的元素 5 6 6 8 9 删除重复的元素 1 3 4 5 6 8 9 进行降序排序 9 8 6 6 5 4 3 3 1 对所有元素进行+1的操作 2 4 4 5 6 7 7 9 10 将两个集合合并 1 2 3 4 5 6