当前位置: 首页 > news >正文

房产网站建网站外贸公司如何做公司网站

房产网站建网站,外贸公司如何做公司网站,各种网站底部图标代码,做网站必看的外国书籍编程kata是一种练习#xff0c;可以帮助程序员通过练习和重复练习来磨练自己的技能。 本文是“ 通过Katas进行Java教程 ”系列的一部分。 本文假定读者已经具有Java的经验#xff0c;熟悉单元测试的基础知识#xff0c;并且知道如何从他最喜欢的IDE#xff08;我是Intelli… 编程kata是一种练习可以帮助程序员通过练习和重复练习来磨练自己的技能。 本文是“ 通过Katas进行Java教程 ”系列的一部分。 本文假定读者已经具有Java的经验熟悉单元测试的基础知识并且知道如何从他最喜欢的IDE我是IntelliJ IDEA 运行它们。 下面显示的练习背后的想法是使用测试驱动的开发方法来学习Java 8 Streaming编写第一个测试的实现确认它通过并转到下一个测试。 每个部分都将以测试形式的目标开始以证明实现一旦编写便是正确的。 这些测试中的每一个都在Java 7或更早版本和Java 8中使用Streams进行了一种可能的实现。 这样读者可以将Java 8的某些新功能与早期JDK中的等效功能进行比较。 请尝试解决测试而不查看提供的解决方案。 有关TDD最佳实践的更多信息请阅读“ 测试驱动开发TDD使用Java示例的最佳实践” 。 Java 8地图 将集合的元素转换为大写。 测验 package com.technologyconversations.java8exercises.streams;import org.junit.Test;import java.util.List;import static com.technologyconversations.java8exercises.streams.ToUpperCase.*; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat;/* Convert elements of a collection to upper case.*/ public class ToUpperCaseSpec {Testpublic void transformShouldConvertCollectionElementsToUpperCase() {ListString collection asList(My name is John Doe);ListString expected asList(MY NAME IS JOHN DOE);assertThat(transform(collection)).hasSameElementsAs(expected);}}Java 7transform7和Java8transform实现 package com.technologyconversations.java8exercises.streams;import java.util.ArrayList; import java.util.List;import static java.util.stream.Collectors.toList;public class ToUpperCase {public static ListString transform7(ListString collection) {ListString coll new ArrayList();for (String element : collection) {coll.add(element.toUpperCase());}return coll;}public static ListString transform(ListString collection) {return collection.stream() // Convert collection to Stream.map(String::toUpperCase) // Convert each element to upper case.collect(toList()); // Collect results to a new list}}Java 8过滤器 过滤集合以便仅返回少于4个字符的元素。 测验 package com.technologyconversations.java8exercises.streams;import org.junit.Test;import java.util.List;import static com.technologyconversations.java8exercises.streams.FilterCollection.*; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat;/* Filter collection so that only elements with less then 4 characters are returned.*/ public class FilterCollectionSpec {Testpublic void transformShouldFilterCollection() {ListString collection asList(My, name, is, John, Doe);ListString expected asList(My, is, Doe);assertThat(transform(collection)).hasSameElementsAs(expected);}}Java 7transform7和Java8transform实现 package com.technologyconversations.java8exercises.streams;import java.util.ArrayList; import java.util.List;import static java.util.stream.Collectors.toList;public class FilterCollection {public static ListString transform7(ListString collection) {ListString newCollection new ArrayList();for (String element : collection) {if (element.length() 4) {newCollection.add(element);}}return newCollection;}public static ListString transform(ListString collection) {return collection.stream() // Convert collection to Stream.filter(value - value.length() 4) // Filter elements with length smaller than 4 characters.collect(toList()); // Collect results to a new list}}Java 8 FlatMap 展平多维集合。 测验 package com.technologyconversations.java8exercises.streams;import org.junit.Test;import java.util.List;import static com.technologyconversations.java8exercises.streams.FlatCollection.*; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat;/* Flatten multidimensional collection*/ public class FlatCollectionSpec {Testpublic void transformShouldFlattenCollection() {ListListString collection asList(asList(Viktor, Farcic), asList(John, Doe, Third));ListString expected asList(Viktor, Farcic, John, Doe, Third);assertThat(transform(collection)).hasSameElementsAs(expected);}}Java 7transform7和Java8transform实现 package com.technologyconversations.java8exercises.streams;import java.util.ArrayList; import java.util.List;import static java.util.stream.Collectors.toList;public class FlatCollection {public static ListString transform7(ListListString collection) {ListString newCollection new ArrayList();for (ListString subCollection : collection) {for (String value : subCollection) {newCollection.add(value);}}return newCollection;}public static ListString transform(ListListString collection) {return collection.stream() // Convert collection to Stream.flatMap(value - value.stream()) // Replace list with stream.collect(toList()); // Collect results to a new list}}Java 8 Max和比较器 从集合中获取最老的人。 测验 package com.technologyconversations.java8exercises.streams;import org.junit.Test;import java.util.List;import static com.technologyconversations.java8exercises.streams.OldestPerson.*; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat;/* Get oldest person from the collection*/ public class OldestPersonSpec {Testpublic void getOldestPersonShouldReturnOldestPerson() {Person sara new Person(Sara, 4);Person viktor new Person(Viktor, 40);Person eva new Person(Eva, 42);ListPerson collection asList(sara, eva, viktor);assertThat(getOldestPerson(collection)).isEqualToComparingFieldByField(eva);}}Java 7getOldestPerson7和Java8getOldestPerson实现 package com.technologyconversations.java8exercises.streams;import java.util.Comparator; import java.util.List;public class OldestPerson {public static Person getOldestPerson7(ListPerson people) {Person oldestPerson new Person(, 0);for (Person person : people) {if (person.getAge() oldestPerson.getAge()) {oldestPerson person;}}return oldestPerson;}public static Person getOldestPerson(ListPerson people) {return people.stream() // Convert collection to Stream.max(Comparator.comparing(Person::getAge)) // Compares people ages.get(); // Gets stream result}}Java 8之和 对集合的所有元素求和。 测验 package com.technologyconversations.java8exercises.streams;import org.junit.Test;import java.util.List;import static com.technologyconversations.java8exercises.streams.Sum.*; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat;/* Sum all elements of a collection*/ public class SumSpec {Testpublic void transformShouldConvertCollectionElementsToUpperCase() {ListInteger numbers asList(1, 2, 3, 4, 5);assertThat(calculate(numbers)).isEqualTo(1 2 3 4 5);}}Java 7calculate7和Java8calculate实现 package com.technologyconversations.java8exercises.streams;import java.util.List;public class Sum {public static int calculate7(ListInteger numbers) {int total 0;for (int number : numbers) {total number;}return total;}public static int calculate(ListInteger people) {return people.stream() // Convert collection to Stream.reduce(0, (total, number) - total number); // Sum elements with 0 as starting value}}Java 8过滤器和映射 获取所有孩子18岁以下的名字。 测验 package com.technologyconversations.java8exercises.streams;import org.junit.Test;import java.util.List;import static com.technologyconversations.java8exercises.streams.Kids.*; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat;/* Get names of all kids (under age of 18)*/ public class KidsSpec {Testpublic void getKidNameShouldReturnNamesOfAllKidsFromNorway() {Person sara new Person(Sara, 4);Person viktor new Person(Viktor, 40);Person eva new Person(Eva, 42);Person anna new Person(Anna, 5);ListPerson collection asList(sara, eva, viktor, anna);assertThat(getKidNames(collection)).contains(Sara, Anna).doesNotContain(Viktor, Eva);}}Java 7getKidNames7和Java8getKidNames实现 package com.technologyconversations.java8exercises.streams;import java.util.*;import static java.util.stream.Collectors.toSet;public class Kids {public static SetString getKidNames7(ListPerson people) {SetString kids new HashSet();for (Person person : people) {if (person.getAge() 18) {kids.add(person.getName());}}return kids;}public static SetString getKidNames(ListPerson people) {return people.stream().filter(person - person.getAge() 18) // Filter kids (under age of 18).map(Person::getName) // Map Person elements to names.collect(toSet()); // Collect values to a Set}}Java 8摘要统计 获取人员统计信息平均年龄人数最大年龄最小年龄和所有年龄的总和。 测验 package com.technologyconversations.java8exercises.streams;import org.junit.Test;import java.util.List;import static com.technologyconversations.java8exercises.streams.PeopleStats.*; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat;/* Get people statistics: average age, count, maximum age, minimum age and sum og all ages.*/ public class PeopleStatsSpec {Person sara new Person(Sara, 4);Person viktor new Person(Viktor, 40);Person eva new Person(Eva, 42);ListPerson collection asList(sara, eva, viktor);Testpublic void getStatsShouldReturnAverageAge() {assertThat(getStats(collection).getAverage()).isEqualTo((double)(4 40 42) / 3);}Testpublic void getStatsShouldReturnNumberOfPeople() {assertThat(getStats(collection).getCount()).isEqualTo(3);}Testpublic void getStatsShouldReturnMaximumAge() {assertThat(getStats(collection).getMax()).isEqualTo(42);}Testpublic void getStatsShouldReturnMinimumAge() {assertThat(getStats(collection).getMin()).isEqualTo(4);}Testpublic void getStatsShouldReturnSumOfAllAges() {assertThat(getStats(collection).getSum()).isEqualTo(40 42 4);}}Java 7getStats7和Java8getStats实现 package com.technologyconversations.java8exercises.streams;import java.util.IntSummaryStatistics; import java.util.List;public class PeopleStats {public static Stats getStats7(ListPerson people) {long sum 0;int min people.get(0).getAge();int max 0;for (Person person : people) {int age person.getAge();sum age;min Math.min(min, age);max Math.max(max, age);}return new Stats(people.size(), sum, min, max);}public static IntSummaryStatistics getStats(ListPerson people) {return people.stream().mapToInt(Person::getAge).summaryStatistics();}}Java 8分区 分区成人和孩子。 测验 package com.technologyconversations.java8exercises.streams;import org.junit.Test;import java.util.List; import java.util.Map;import static com.technologyconversations.java8exercises.streams.Partitioning.*; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat;/* Partition adults and kids*/ public class PartitioningSpec {Testpublic void partitionAdultsShouldSeparateKidsFromAdults() {Person sara new Person(Sara, 4);Person viktor new Person(Viktor, 40);Person eva new Person(Eva, 42);ListPerson collection asList(sara, eva, viktor);MapBoolean, ListPerson result partitionAdults(collection);assertThat(result.get(true)).hasSameElementsAs(asList(viktor, eva));assertThat(result.get(false)).hasSameElementsAs(asList(sara));}}Java 7partitionAdults7和Java8partitionAdults实现 package com.technologyconversations.java8exercises.streams;import java.util.*; import static java.util.stream.Collectors.*;public class Partitioning {public static MapBoolean, ListPerson partitionAdults7(ListPerson people) {MapBoolean, ListPerson map new HashMap();map.put(true, new ArrayList());map.put(false, new ArrayList());for (Person person : people) {map.get(person.getAge() 18).add(person);}return map;}public static MapBoolean, ListPerson partitionAdults(ListPerson people) {return people.stream() // Convert collection to Stream.collect(partitioningBy(p - p.getAge() 18)); // Partition stream of people into adults (age 18) and kids}}Java 8分组 按国籍分组人。 测验 package com.technologyconversations.java8exercises.streams;import org.junit.Test;import java.util.List; import java.util.Map;import static com.technologyconversations.java8exercises.streams.Grouping.*; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat;/* Group people by nationality*/ public class GroupingSpec {Testpublic void partitionAdultsShouldSeparateKidsFromAdults() {Person sara new Person(Sara, 4, Norwegian);Person viktor new Person(Viktor, 40, Serbian);Person eva new Person(Eva, 42, Norwegian);ListPerson collection asList(sara, eva, viktor);MapString, ListPerson result groupByNationality(collection);assertThat(result.get(Norwegian)).hasSameElementsAs(asList(sara, eva));assertThat(result.get(Serbian)).hasSameElementsAs(asList(viktor));}}Java 7groupByNationality7和Java8groupByNationality实现 package com.technologyconversations.java8exercises.streams;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import static java.util.stream.Collectors.*;public class Grouping {public static MapString, ListPerson groupByNationality7(ListPerson people) {MapString, ListPerson map new HashMap();for (Person person : people) {if (!map.containsKey(person.getNationality())) {map.put(person.getNationality(), new ArrayList());}map.get(person.getNationality()).add(person);}return map;}public static MapString, ListPerson groupByNationality(ListPerson people) {return people.stream() // Convert collection to Stream.collect(groupingBy(Person::getNationality)); // Group people by nationality}}Java 8加入 返回用逗号分隔的人名。 测验 package com.technologyconversations.java8exercises.streams;import org.junit.Test;import java.util.List;import static com.technologyconversations.java8exercises.streams.Joining.namesToString; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat;/* Return people names separated by comma*/ public class JoiningSpec {Testpublic void toStringShouldReturnPeopleNamesSeparatedByComma() {Person sara new Person(Sara, 4);Person viktor new Person(Viktor, 40);Person eva new Person(Eva, 42);ListPerson collection asList(sara, viktor, eva);assertThat(namesToString(collection)).isEqualTo(Names: Sara, Viktor, Eva.);}}Java 7namesToString7和Java8namesToString实现 package com.technologyconversations.java8exercises.streams;import java.util.List;import static java.util.stream.Collectors.joining;public class Joining {public static String namesToString7(ListPerson people) {String label Names: ;StringBuilder sb new StringBuilder(label);for (Person person : people) {if (sb.length() label.length()) {sb.append(, );}sb.append(person.getName());}sb.append(.);return sb.toString();}public static String namesToString(ListPerson people) {return people.stream() // Convert collection to Stream.map(Person::getName) // Map Person to name.collect(joining(, , Names: , .)); // Join names}}资源 完整源代码位于GitHub存储库https://github.com/vfarcic/java-8-exercises中 。 除了测试和实现之外存储库还包含build.gradle除其他外build.gradle可用于下载AssertJ依赖项并运行测试。 翻译自: https://www.javacodegeeks.com/2014/11/java-8-streams-micro-katas.html
http://www.zqtcl.cn/news/515791/

相关文章:

  • 美辰网站建设个人网站如何做移动端
  • 郑州模板网站建设网页在线代理
  • 学生做网站的工作室网站建设项目表
  • .net网站开发教程百度贴吧微网站设计基本要求
  • 无锡网站建设哪家公司好咨询网站建设
  • 优秀的企业网站设计wordpress登陆后台总是跳转首页
  • 国外html5特效网站宁波江北区建设局网站
  • 购物网站哪个是正品商城网站模板下载
  • 网站名称 规则技术支持 石家庄网站建设
  • 专门做私人定制旅游的网站专做韩餐网站
  • 网站 续费wordpress首页调用指定分类
  • 2008系统怎么做网站免费设计软件下载
  • 做电音的软件的专业下载网站宁波俄语网站建设
  • 北?? 网站建设旅游手机网站开发
  • 乐清做网站的网站备案容易通过吗
  • 网站qq登录 开发一个小型网站开发成本
  • 湖北网络建设公司网站js跳转到别的网站
  • 郑州网站app开发的汽车网站 源码
  • 河南网站建设企业做网站多少钱西宁君博示范
  • 沈阳有做网站的吗青浦手机网站制作
  • 腾讯云免费建站建立一个网站英语
  • 沙漠风网站建设怎么样官方网站建设银行2010年存款利息
  • 360报危险网站微信代码小程序
  • 网站维护报价单国外 做励志视频的网站
  • 用源码做自己的网站公司网站建设哪家公司好
  • 网站运营做seohtml前端网站开发PPT
  • 上海网站定制设计图wordpress网站在线安装
  • 互动网站的核心技术wordpress不用插件
  • 厦门市建设工程交易中心网站怎么自己做游戏软件的app
  • 网站论文参考文献人力资源公司名称大全简单大气