1 集合List
1.1 遍历
- 遍历 List链表
for (int i = 0; i < list.size(); i++) { News s = (News)list.get(i); System.out.println(s.getId()+" "+s.getTitle()+" "+s.getAuthor()); }
for (News s : list) { System.out.println(s.getId()+" "+s.getTitle()+" "+s.getAuthor()); }
public static void main(String[] args) { List<Person> personList = Arrays.asList(new Person("17","小美"),new Person("18","小明")); Iterator<Person> it = personList.listIterator(); while(it.hasNext()){ Person person = (Person) it.next(); System.out.println(person); } }
- 遍历 Map映射
for(int key: hashMap3.keySet()){ priorityQueue.enqueue(new MapInnerClass(key,hashMap3.get(key))); }
1.2 三种调用形式的使用场景
- 如果需要操作索引,使用普通for循环。
- 如果在遍历的过程中需要删除元素,请使用迭代器。
- 如果仅仅想遍历,那么使用增强for。
1.3 jdk8新特性
- map的使用(参见5.1)
- foreach的使用(参见5.1)
- map与foreach的区别
- groupingBy方法(参见6.4)
- reducing应用 把流组成新的集合(参见6.5)
- collectingAndThen (参见6.5)
- list和map互转(参见6.5)
(1)map()会分配内存空间存储新数组并返回,forEach()不会返回数据。
map遍历的后的数组通常都是生成一个新的数组,新的数组的值发生变化,当前遍历的数组值不会变化。
(2)forEach()允许callback更改原始数组的元素。map()返回新的数组。
forEach遍历通常都是直接引入当前遍历数组的内存地址,生成的数组的值发生变化,当前遍历的数组对应的值也会发生变化。
1.4 其他方法
- Array.asList demo,参见5.1(不能和list.addAll一起使用)
- listA.addAll(listB) 两个list合并成1个list
- List<String> newList = Optional.ofNullable(list).map(e->{return e.stream().map(...).collect(Collectors.toList())}).orElse(Lists.newArrayList()); 判断非空
- List取前n条数据
list.subList(0, n);(前包后不包)
1.5 结束for循环,continu,break,return的区别
https://www.cnblogs.com/1446358788-qq/p/16736785.html
2 集合Map
2.1 map之put方法1
package itcast.java16;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class MapDemo1 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
// 添加元素,如果出现添加时,相同的键。那么后添加的值会覆盖原有键对应值。并put方法会返回被覆盖的值。
map.put("01", "zhangsan1");
map.put("02", "zhangsan2");
map.put("03", "zhangsan3");
System.out.println("get:" + map.get("03"));
// 可以通过get方法的返回值来判断一个键是否存在。通过返回null来判断
// 获取map集合中的所有的值
Collection<String> coll = map.values();
System.out.println(coll);
System.out.println(map);
}
}
2.2 map之put方法2
package com.axjy.aa;
import java.util.*;
public class Test {
public static void main(String[] args) {
Map map=new HashMap();
String s="key";
map.put(s, "hello");
map.put(s, "Wrold");
map.put(s, "value");
System.out.println(map);
}
}
打印的结果是:
{key=value}
"Administrator" Sid: S-1-5-21-1754733684-1616197072-4204698062-500
请格外注意:调试的时候发现变量s的值一直未有改变,而最后的显示结果证明:s已经发生了改变
2.3 map中的entry
是java中的一个对象,一般可以通过map.entrySet()得到。
1,entrySet实现了Set接口,里面存放的是键值对。一个K对应一个V。
2,用来遍历map的一种方法。
Set<Map.Entry<String, String>> entryseSet=map.entrySet();
for (Map.Entry<String, String> entry:entryseSet) {
System.out.println(entry.getKey()+","+entry.getValue());
}
通过getKey()得到K,getValue得到V。
3 集合Set
4 数组
5 递归
5.1 递归List
背景:
需要从外围系统拿到一个树,转成我们系统的树,但是部分字段不一样(demo中的字段为fatherId到parentId),转的时候发现只转了第一层级,内部层级的没有转化,所以在付哥的帮助下做了递归转换。但是在写博客的时候发现mapStruct是可以进行多层级转换的,经排查也不是版本的问题,反正遇到只转一个层级的情况是可以用递归转换的,这个也作为一个新知识点记录下。
技术栈:
jdk8 lambda表达式:forEach,map使用
mapStruct
递归
json
递归类:
package com.ddwei.test.service; import com.alibaba.fastjson.JSON; import com.ddwei.test.dto.recursion.FamilyMember; import com.ddwei.test.dto.recursion.OutDto; import lombok.extern.slf4j.Slf4j; import org.springframework.util.CollectionUtils; import java.util.*; import java.util.stream.Collectors; @Slf4j public class DiGui2 { private static List<FamilyMember> list; private static void init(){ FamilyMember familyMember211 = new FamilyMember("211","21","堂妹", new ArrayList<>()); FamilyMember familyMember21 = new FamilyMember("21","2","堂叔", Arrays.asList(familyMember211)); FamilyMember familyMember2 = new FamilyMember("2","","二爷",Arrays.asList(familyMember21)); FamilyMember familyMember111 = new FamilyMember("111","11","姐姐", new ArrayList<>()); FamilyMember familyMember112 = new FamilyMember("112","11","哥哥", new ArrayList<>()); FamilyMember familyMember11 = new FamilyMember("11","1","爸爸", Arrays.asList(familyMember111,familyMember112)); FamilyMember familyMember12 = new FamilyMember("12","1","姑姑", new ArrayList<>()); FamilyMember familyMember1 = new FamilyMember("1","","爷爷",Arrays.asList(familyMember11,familyMember12)); list = Arrays.asList(familyMember1,familyMember2); //return list; } /** * 递归调用 * @author weidoudou * @date 2022/8/2 12:50 * @param args 请添加参数描述 * @return void **/ public static void main(String[] args) { init(); //1 最初的对象list log.info("DiGui->main->list->:{}",JSON.toJSONString(list)); //2 过滤出有最外层对象list,否则 List<FamilyMember> remotList = list.stream().filter(e->"".equals(e.getFatherId())).collect(Collectors.toList()); log.info("DiGui->main->remotList->:{}",JSON.toJSONString(remotList)); //3 最外层的转化 List<OutDto> outDtoList = remotList.stream().map(DdWeiAssembler.INSTANCE::copyRemotDtoFamilyMemberToDto).collect(Collectors.toList()); log.info("DiGui->main->outDtoList1->:{}",JSON.toJSONString(outDtoList)); //4 调用递归方法 makeTree(outDtoList,remotList); log.info("DiGui->main->outDtoList2->:{}",JSON.toJSONString(outDtoList)); } private static void makeTree(List<OutDto> outDtoList,List<FamilyMember> remotList){ //如果递归传过来的remotList为空(通常children为空),直接进行下一个循环 if(CollectionUtils.isEmpty(remotList)){ return; } //给新的list赋值 outDtoList.forEach(outRoot->{ Iterator<FamilyMember> remotIt = remotList.iterator(); while(remotIt.hasNext()){ FamilyMember remotDto = remotIt.next(); if(outRoot.getId().equals(remotDto.getId())){ //如果remotList children返回为null,给空list,否则将children转成新的list outRoot.setChildren(Optional.ofNullable(remotDto.getChildren()).map(e->{ return e.stream().map(DdWeiAssembler.INSTANCE::copyRemotDtoFamilyMemberToDto).collect(Collectors.toList()); }).orElse(Collections.EMPTY_LIST)); //下一层级的递归调用 makeTree(outRoot.getChildren(),remotDto.getChildren()); } } }); } }
remotDto
package com.ddwei.test.dto.recursion; import lombok.*; import java.util.List; @Getter @Setter @Builder @NoArgsConstructor @AllArgsConstructor public class FamilyMember { private String id; private String fatherId; private String call; private List<FamilyMember> children; }
outDto
package com.ddwei.test.dto.recursion; import lombok.*; import java.util.List; @Getter @Setter @Builder @NoArgsConstructor @AllArgsConstructor public class OutDto { private String id; private String parentId; private String call; private List<OutDto> children; @Override public String toString() { return "OutDto{" + "id='" + id + '\'' + ", fatherId='" + parentId + '\'' + ", call='" + call + '\'' + ", children=" + children + '}'; } }
Assembler类:
package com.ddwei.test.service; import com.ddwei.test.dto.recursion.FamilyMember; import com.ddwei.test.dto.recursion.OutDto; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.Mappings; import org.mapstruct.factory.Mappers; @Mapper(componentModel = "spring") public interface DdWeiAssembler { DdWeiAssembler INSTANCE = Mappers.getMapper(DdWeiAssembler.class); @Mappings({ @Mapping(source = "fatherId",target = "parentId") }) OutDto copyRemotDtoFamilyMemberToDto(FamilyMember query); }
打印日志:
D:\java\envir\jdk1.8\anZh\bin\java.exe "-javaagent:D:\java\develop\idea\anZh\IntelliJ IDEA Community Edition 2021.2.1\lib\idea_rt.jar=61068:D:\java\develop\idea\anZh\IntelliJ IDEA Community Edition 2021.2.1\bin" -Dfile.encoding=UTF-8 -classpath D:\java\envir\jdk1.8\anZh\jre\lib\charsets.jar;D:\java\envir\jdk1.8\anZh\jre\lib\deploy.jar;D:\java\envir\jdk1.8\anZh\jre\lib\ext\access-bridge-64.jar;D:\java\envir\jdk1.8\anZh\jre\lib\ext\cldrdata.jar;D:\java\envir\jdk1.8\anZh\jre\lib\ext\dnsns.jar;D:\java\envir\jdk1.8\anZh\jre\lib\ext\jaccess.jar;D:\java\envir\jdk1.8\anZh\jre\lib\ext\jfxrt.jar;D:\java\envir\jdk1.8\anZh\jre\lib\ext\localedata.jar;D:\java\envir\jdk1.8\anZh\jre\lib\ext\nashorn.jar;D:\java\envir\jdk1.8\anZh\jre\lib\ext\sunec.jar;D:\java\envir\jdk1.8\anZh\jre\lib\ext\sunjce_provider.jar;D:\java\envir\jdk1.8\anZh\jre\lib\ext\sunmscapi.jar;D:\java\envir\jdk1.8\anZh\jre\lib\ext\sunpkcs11.jar;D:\java\envir\jdk1.8\anZh\jre\lib\ext\zipfs.jar;D:\java\envir\jdk1.8\anZh\jre\lib\javaws.jar;D:\java\envir\jdk1.8\anZh\jre\lib\jce.jar;D:\java\envir\jdk1.8\anZh\jre\lib\jfr.jar;D:\java\envir\jdk1.8\anZh\jre\lib\jfxswt.jar;D:\java\envir\jdk1.8\anZh\jre\lib\jsse.jar;D:\java\envir\jdk1.8\anZh\jre\lib\management-agent.jar;D:\java\envir\jdk1.8\anZh\jre\lib\plugin.jar;D:\java\envir\jdk1.8\anZh\jre\lib\resources.jar;D:\java\envir\jdk1.8\anZh\jre\lib\rt.jar;C:\Users\18612\IdeaProjects\text\target\classes;D:\java\maven\apache-maven-3.8.2\repository\org\projectlombok\lombok\1.18.2\lombok-1.18.2.jar;D:\java\maven\apache-maven-3.8.2\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\java\maven\apache-maven-3.8.2\repository\org\slf4j\slf4j-log4j12\1.7.30\slf4j-log4j12-1.7.30.jar;D:\java\maven\apache-maven-3.8.2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\java\maven\apache-maven-3.8.2\repository\org\mapstruct\mapstruct\1.4.2.Final\mapstruct-1.4.2.Final.jar;D:\java\maven\apache-maven-3.8.2\repository\org\mapstruct\mapstruct-jdk8\1.4.2.Final\mapstruct-jdk8-1.4.2.Final.jar;D:\java\maven\apache-maven-3.8.2\repository\org\mapstruct\mapstruct-processor\1.4.2.Final\mapstruct-processor-1.4.2.Final.jar;D:\java\maven\apache-maven-3.8.2\repository\org\mockito\mockito-core\2.23.4\mockito-core-2.23.4.jar;D:\java\maven\apache-maven-3.8.2\repository\net\bytebuddy\byte-buddy\1.9.3\byte-buddy-1.9.3.jar;D:\java\maven\apache-maven-3.8.2\repository\net\bytebuddy\byte-buddy-agent\1.9.3\byte-buddy-agent-1.9.3.jar;D:\java\maven\apache-maven-3.8.2\repository\org\objenesis\objenesis\2.6\objenesis-2.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;D:\java\maven\apache-maven-3.8.2\repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;D:\java\maven\apache-maven-3.8.2\repository\org\junit\platform\junit-platform-commons\1.5.0\junit-platform-commons-1.5.0.jar;D:\java\maven\apache-maven-3.8.2\repository\org\jmockit\jmockit\1.36\jmockit-1.36.jar;D:\java\maven\apache-maven-3.8.2\repository\junit\junit\4.12\junit-4.12.jar;D:\java\maven\apache-maven-3.8.2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\java\maven\apache-maven-3.8.2\repository\org\junit\platform\junit-platform-runner\1.6.2\junit-platform-runner-1.6.2.jar;D:\java\maven\apache-maven-3.8.2\repository\org\junit\platform\junit-platform-launcher\1.6.2\junit-platform-launcher-1.6.2.jar;D:\java\maven\apache-maven-3.8.2\repository\org\junit\platform\junit-platform-engine\1.6.2\junit-platform-engine-1.6.2.jar;D:\java\maven\apache-maven-3.8.2\repository\org\junit\platform\junit-platform-suite-api\1.6.2\junit-platform-suite-api-1.6.2.jar;D:\java\maven\apache-maven-3.8.2\repository\com\github\houbb\bean-mapping-core\0.2.5\bean-mapping-core-0.2.5.jar;D:\java\maven\apache-maven-3.8.2\repository\com\github\houbb\bean-mapping-api\0.2.5\bean-mapping-api-0.2.5.jar;D:\java\maven\apache-maven-3.8.2\repository\com\github\houbb\heaven\0.1.62\heaven-0.1.62.jar;D:\java\maven\apache-maven-3.8.2\repository\com\alibaba\fastjson\1.2.47\fastjson-1.2.47.jar;D:\java\maven\apache-maven-3.8.2\repository\org\springframework\spring-context\5.3.18\spring-context-5.3.18.jar;D:\java\maven\apache-maven-3.8.2\repository\org\springframework\spring-aop\5.3.18\spring-aop-5.3.18.jar;D:\java\maven\apache-maven-3.8.2\repository\org\springframework\spring-beans\5.3.18\spring-beans-5.3.18.jar;D:\java\maven\apache-maven-3.8.2\repository\org\springframework\spring-core\5.3.18\spring-core-5.3.18.jar;D:\java\maven\apache-maven-3.8.2\repository\org\springframework\spring-jcl\5.3.18\spring-jcl-5.3.18.jar;D:\java\maven\apache-maven-3.8.2\repository\org\springframework\spring-expression\5.3.18\spring-expression-5.3.18.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\plugins\maven-resources-plugin\2.6\maven-resources-plugin-2.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\maven-plugin-api\2.0.6\maven-plugin-api-2.0.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\maven-project\2.0.6\maven-project-2.0.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\maven-profile\2.0.6\maven-profile-2.0.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\maven-artifact-manager\2.0.6\maven-artifact-manager-2.0.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\maven-plugin-registry\2.0.6\maven-plugin-registry-2.0.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\maven-core\2.0.6\maven-core-2.0.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\wagon\wagon-file\1.0-beta-2\wagon-file-1.0-beta-2.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\maven-plugin-parameter-documenter\2.0.6\maven-plugin-parameter-documenter-2.0.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\wagon\wagon-http-lightweight\1.0-beta-2\wagon-http-lightweight-1.0-beta-2.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\wagon\wagon-http-shared\1.0-beta-2\wagon-http-shared-1.0-beta-2.jar;D:\java\maven\apache-maven-3.8.2\repository\jtidy\jtidy\4aug2000r7-dev\jtidy-4aug2000r7-dev.jar;D:\java\maven\apache-maven-3.8.2\repository\xml-apis\xml-apis\1.0.b2\xml-apis-1.0.b2.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\reporting\maven-reporting-api\2.0.6\maven-reporting-api-2.0.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\doxia\doxia-sink-api\1.0-alpha-7\doxia-sink-api-1.0-alpha-7.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\wagon\wagon-provider-api\1.0-beta-2\wagon-provider-api-1.0-beta-2.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\maven-repository-metadata\2.0.6\maven-repository-metadata-2.0.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\maven-error-diagnostics\2.0.6\maven-error-diagnostics-2.0.6.jar;D:\java\maven\apache-maven-3.8.2\repository\commons-cli\commons-cli\1.0\commons-cli-1.0.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\wagon\wagon-ssh-external\1.0-beta-2\wagon-ssh-external-1.0-beta-2.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\wagon\wagon-ssh-common\1.0-beta-2\wagon-ssh-common-1.0-beta-2.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\maven-plugin-descriptor\2.0.6\maven-plugin-descriptor-2.0.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\codehaus\plexus\plexus-interactivity-api\1.0-alpha-4\plexus-interactivity-api-1.0-alpha-4.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\wagon\wagon-ssh\1.0-beta-2\wagon-ssh-1.0-beta-2.jar;D:\java\maven\apache-maven-3.8.2\repository\com\jcraft\jsch\0.1.27\jsch-0.1.27.jar;D:\java\maven\apache-maven-3.8.2\repository\classworlds\classworlds\1.1\classworlds-1.1.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\maven-artifact\2.0.6\maven-artifact-2.0.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\maven-settings\2.0.6\maven-settings-2.0.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\maven-model\2.0.6\maven-model-2.0.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\maven-monitor\2.0.6\maven-monitor-2.0.6.jar;D:\java\maven\apache-maven-3.8.2\repository\org\codehaus\plexus\plexus-container-default\1.0-alpha-9-stable-1\plexus-container-default-1.0-alpha-9-stable-1.jar;D:\java\maven\apache-maven-3.8.2\repository\org\codehaus\plexus\plexus-utils\2.0.5\plexus-utils-2.0.5.jar;D:\java\maven\apache-maven-3.8.2\repository\org\apache\maven\shared\maven-filtering\1.1\maven-filtering-1.1.jar;D:\java\maven\apache-maven-3.8.2\repository\org\sonatype\plexus\plexus-build-api\0.0.4\plexus-build-api-0.0.4.jar;D:\java\maven\apache-maven-3.8.2\repository\org\codehaus\plexus\plexus-interpolation\1.13\plexus-interpolation-1.13.jar com.ddwei.test.service.DiGui2 0 [main] INFO com.ddwei.test.service.DiGui2 - DiGui->main->list->:[{"call":"爷爷","children":[{"call":"爸爸","children":[{"call":"姐姐","children":[],"fatherId":"11","id":"111"},{"call":"哥哥","children":[],"fatherId":"11","id":"112"}],"fatherId":"1","id":"11"},{"call":"姑姑","children":[],"fatherId":"1","id":"12"}],"fatherId":"","id":"1"},{"call":"二爷","children":[{"call":"堂叔","children":[{"call":"堂妹","children":[],"fatherId":"21","id":"211"}],"fatherId":"2","id":"21"}],"fatherId":"","id":"2"}] 79 [main] INFO com.ddwei.test.service.DiGui2 - DiGui->main->remotList->:[{"call":"爷爷","children":[{"call":"爸爸","children":[{"call":"姐姐","children":[],"fatherId":"11","id":"111"},{"call":"哥哥","children":[],"fatherId":"11","id":"112"}],"fatherId":"1","id":"11"},{"call":"姑姑","children":[],"fatherId":"1","id":"12"}],"fatherId":"","id":"1"},{"call":"二爷","children":[{"call":"堂叔","children":[{"call":"堂妹","children":[],"fatherId":"21","id":"211"}],"fatherId":"2","id":"21"}],"fatherId":"","id":"2"}] 95 [main] INFO com.ddwei.test.service.DiGui2 - DiGui->main->outDtoList1->:[{"call":"爷爷","children":[{"call":"爸爸","children":[{"call":"姐姐","children":[],"id":"111","parentId":"11"},{"call":"哥哥","children":[],"id":"112","parentId":"11"}],"id":"11","parentId":"1"},{"call":"姑姑","children":[],"id":"12","parentId":"1"}],"id":"1","parentId":""},{"call":"二爷","children":[{"call":"堂叔","children":[{"call":"堂妹","children":[],"id":"211","parentId":"21"}],"id":"21","parentId":"2"}],"id":"2","parentId":""}] 95 [main] INFO com.ddwei.test.service.DiGui2 - DiGui->main->outDtoList2->:[{"call":"爷爷","children":[{"call":"爸爸","children":[{"call":"姐姐","children":[],"id":"111","parentId":"11"},{"call":"哥哥","children":[],"id":"112","parentId":"11"}],"id":"11","parentId":"1"},{"call":"姑姑","children":[],"id":"12","parentId":"1"}],"id":"1","parentId":""},{"call":"二爷","children":[{"call":"堂叔","children":[{"call":"堂妹","children":[],"id":"211","parentId":"21"}],"id":"21","parentId":"2"}],"id":"2","parentId":""}] Process finished with exit code 0
6 应用
6.1 应用 list和map的应用
ArrayList list = new ArrayList(); HashMap sSendMap2 = new HashMap(); sSendMap2.put("connectType", "");//联系人类型 sSendMap2.put("linkmanName", "");//联系人姓名 sSendMap2.put("connectGender", "");//联系人性别 sSendMap2.put("conIdentType", "");//联系人证件种类 sSendMap2.put("conIdentNo", "");//联系人证件号码 sSendMap2.put("contmethType", "");//联系方式 sSendMap2.put("contmethInfo", "");//联系方式内容 sSendMap2.put("validFlag", "");//是否有效 list.add(sSendMap2); sSendMap.put("MCiOrgLinkman", list); ArrayList list = sRecMap.get("list"); StringBuffer sb = new StringBuffer(); HashMap tempMap = null; for(int i=0;i<list.size();i++){ tempMap = (HashMap)list.get(i); sb.append(temp.get("aaa").toString()+","); sb.append(temp.get("bbb").toString()+"@"); } String result = sb.toString();
6.2 应用 数组和集合之间的转变
Arrays.asList(arr).contains(name);
//我可以实现的功能是把String数组转变成一个list,观察这个list中是否包含特定的字符串,这个在Ajax01中有应用
asList(arr)返回一个受指定数组支持的固定大小的列表。
util类包含用来操作数组(比如排序和搜索)的各种方法。此类还包含一个允许将数组作为列表来查看的静态工厂。
6.3 jdk8 常见应用
package com.company; import java.util.*; import java.util.stream.Collectors; public class Test1 { public static void main(String[] args) { List<User> userList = new ArrayList<>(); userList.add(new User("1","李大锤",23,"南京")); userList.add(new User("2","张无忌",18,"西安")); userList.add(new User("3","带头大哥",36,"杭州")); //id相同,其他数据不同 userList.add(new User("3","333",99,"杭州")); //id相同,其他数据不同 userList.add(new User("3","刘德华",26,"苏州")); userList.add(new User("4","郭靖",33,"上海")); userList.add(new User("1","李大锤",23,"南京")); //id相同,其他数据也相同 //1 常规方法 //1.1 去重:根据userid去重,只取第一条 userList = userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getUserid))), ArrayList::new)); System.out.println(userList); //1.2 list内部处理,不需要list结果 userList.foreach(e-{}); //2 互转 //2.1 组装成String System.out.println(userList.stream().map(User::getName).collect(Collectors.joining("-"))); //2.2 分组 //2.2.1 list转map<String,Object>1 Map<String,User> studentDtoMap = Optional.ofNullable(userList).map( e->{ return e.stream().collect( Collectors.groupingBy(User::getUserid,Collectors.collectingAndThen(Collectors.reducing( (e1,e2)-> e1.getThree().compareTo(e2.getThree())>=0?e1:e2),Optional::get ))); } ).orElse(new HashMap<>()); System.out.println(studentDtoMap); //2.2.2 map转list userList = new ArrayList<>(studentDtoMap.values()); System.out.println(userList); //2.2.3 list转map2<String,List> Map<String,List<User>> studentDtoMap2 = userList.stream().collect(Collectors.groupingBy(User::getUserid)); System.out.println(studentDtoMap2); } }
7 理论
7.1 区别 list和set的区别
list 有序,不唯一,
set 无序,唯一 set的应用场合(如vip会员登陆时,页面显示vip用户名)y12yimai项目中