MAP
java为数据结构中的映射定义了一个接口java.util.Map,他实现了四个类,分别是:HashMap,HashTable,LinkedHashMap,TreeMap 。Map 提供了一个更通用的元素存储方法。Map 集合类用于存储元素对(称作“键”和“值”),其中每个键映射到一个值。Map不允许键重复,但允许值重复
1.HashMap:
最常用的Map,根据键的hashcode值来存储数据,根据键可以直接获得他的值(因为相同的键hashcode值相同,在地址为hashcode值的地方存储的就是值,所以根据键可以直接获得值),具有很快的访问速度,遍历时,取得数据的顺序完全是随机的,HashMap最多只允许一条记录的键为null,允许多条记录的值为null,HashMap不支持线程同步,即任意时刻可以有多个线程同时写HashMap,这样对导致数据不一致,如果需要同步,可以使用synchronziedMap的方法使得HashMap具有同步的能力或者使用concurrentHashMap。HashMap是输出数据时无序的,即不会记录插入的顺序。
2.HashTable:
与HashMap类似,不同的是,它不允许记录的键或值为空,支持线程同步,即任意时刻只能有一个线程写HashTable,因此也导致HashTable在写入时比较慢!
3.LinkedHashMap:
是HashMap的一个子类,但它保持了记录的插入顺序,遍历时先得到的肯定是先插入的,也可以在构造时带参数,按照应用次数排序,在遍历时会比HahsMap慢,不过有个例外,当HashMap的容量很大,实际数据少时,遍历起来会比LinkedHashMap慢(因为它是链啊),因为HashMap的遍历速度和它容量有关,LinkedHashMap遍历速度只与数据多少有关
4.TreeMap:
实现了sortMap接口,能够把保存的记录按照键排序(默认升序)如果需要降序使用java.util包中DescendingMap(),也可以指定排序比较器,遍历时得到的数据是排过序的
5.MultiValueMap
即一个键对应多个值,Spring的内部实现是LinkedMultiValueMap。使用场景:批量功能
6.IdentityHashMap
有一种key值可以重复的map.IdentityHashMap在比较键(和值)时使用引用相等性(==)而不是Map的equals()方法。IdentityHashMap使用引用相等性,因此两个键分别存储在内存中,因此它们的引用是不相等的。
//内存中有两个相似但不同的实例
Integer key1 = new Integer(10);
Integer key2 = new Integer(10);
//IdentityHashMap中的相同key
IdentityHashMap<Integer, String> identityHashMap = new IdentityHashMap<>();
identityHashMap.put(key1, "India");
identityHashMap.put(key2, "USA");
//Identity HashMap : {10=USA, 10=India}
System.out.println("Identity HashMap : " + identityHashMap);
keySet排序
- Hashtable.keySet() 降序
- TreeMap.keySet() 升序
- HashMap.keySet() 乱序
- LinkedHashMap.keySet() 原序
TreeMap<String,String> map = new TreeMap<String, String>();
map.put("name", "zychen");
map.put("password", "123456");
map.put("project", "base");
map.put("tenantId", "192319387131");
map.descendingMap();
//根据指定的key获取对应的value
Object name = map.get("name");
if (name instanceof String) { //判断键值是否为String类型
String value = (String) name; //获取指定的value值
}
//通过key遍历
for (String key: map.keySet()) {
System.out.println("value:" + map.get(key));
}
MultiValueMap<String, String> valueMap = new LinkedMultiValueMap<>();
valueMap.add("1", "1");
valueMap.add("1", "2");
valueMap.add("1", "3");
valueMap.add("1", "4");
valueMap.add("1", "5");
valueMap.add("2", "1");
valueMap.add("2", "2");
valueMap.add("3", "1");
for (Map.Entry<String, List<String>> stringListEntry : valueMap.entrySet()) {
System.out.println("key:" + stringListEntry.getKey());
List<String> value = stringListEntry.getValue();
System.out.println("value:" + value);
}
什么情况用什么类型的Map:
- 在Map中插入,删除,定位元素:HashMap
- 要按照自定义顺序或自然顺序遍历:TreeMap
- 要求输入顺序和输出顺序相同:LinkedHashMap
Map 缓存
如果 Map 中不存在这个键,可以设置。存在后就不能覆盖设置
Map<String, Integer> map = new HashMap<>();
map.put("key", 10);
int value = map.computeIfAbsent("key", k -> 20);
System.out.println(value); // 输出: 10
System.out.println(map.get("key")); // 输出: 10
Map遍历
public static void main(String[] args) {
//创建map集合对象
Map<String, Integer> map = new HashMap<>();
map.put("诸葛亮", 1);
map.put("刘伯温", 2);
map.put("张良", 3);
map.put("东方朔", 4);
map.put("东方朔", 5); //5 key值相同会被覆盖的特性
map.containsKey("东方朔");
//JDK8以下推荐写法
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
//JDK8推荐写法,简捷
map.forEach((key, value) -> {
System.out.println(key);
System.out.println(value);
});
}
List
- Vector:线程安全的。
- ArrayList:适合查找与顺序添加。但增删较慢。线程不同步
- LinkedList:适合随机插入与删除。查询较慢
List转String[]
List<String> list = new ArrayList<>();
list.add("mm");
list.add("nn");
list.add("hh");
String[] strings = list.toArray(new String[list.size()]);
List转String
LinkedList<String> lList = new LinkedList<String>();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println(lList);
lList.addFirst("0");
System.out.println(lList);
lList.addLast("6");
System.out.println(lList);
boolean res = llist.contains(4);
if (res == true) {
System.out.println("包含");
}
ArrayList<String> list = new ArrayList<String>() ;
list.add("Tom");
System.out.println(list.contains("Tom"));
String allIndex = StringUtils.join(list,",");
Set<String> set = new HashSet<>();
set.add("hello1");
set.add("hello2");
set.add("hello3");
Object[] arr = set.toArray();
System.out.println(StringUtils.join(arr, ","));
//复制,2个内存空间
List<String> copy = new ArrayList<>(list);
List<ApiVO> result = new LinkedList<>();
List<Packages> agPkList = agMapper.getList(ordersNo);
for (Packages info : agPkList) {
ApiVO vo = new ApiVO();
BeanUtils.copyProperties(info, vo);
result.add(vo);
}
for (Map.Entry<String, List<People>> entry : collects.entrySet()) {
for (People people : entry.getValue()) {
if (people.getOfflineUnfreeze().equals("1")) {
System.out.println("对象:" + people.getPhone());
}
}
}
public void splitDemo4(){
String str= "aaa,bbb#ccc";
//使用,和#分割字符串
String[] split = str.split(",|#");
for (int i = 0; i < split.length; i++) {
System.out.println(split[i]);
}
}
List 分割
在工作中经常遇到需要将数组分割成多个子数组,然后进行批量处理的需求。那有没有比较优雅的实现呢
引入依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
代码演示
public static void main(String[] args) {
//初始化数组
List<Integer> parList = new ArrayList<>();
IntStream.range(0, 30).forEach(parList::add);
//分割成子数组
List<List<Integer>> subList = ListUtils.partition(parList, 10);
//遍历子数组
subList.forEach(list -> {
System.out.println(String.format("subList size:%s", list.size()));
System.out.println(String.format("list:%s", list.toString()));
});
}
Stream
Map
转换为List
List<String> list = Lists.newArrayList("a", "b", "c", "a", "e", "f");
String result = list.stream().collect(Collectors.joining()); //输出为 abcdef
String result = list.stream().collect(Collectors.joining(",")); //输出为 a,b,c,d,e,f
List<String> valueList = linkedMap.values().stream().collect(Collectors.toList());
过滤筛选
List<Product> math = prodList.stream().filter(
product -> product.getCategory().contains("啤酒") && "百威啤酒".equals(product.getName()))
.collect(Collectors.toList());
提取 List 中的某个字段
List<Integer> ids = list.stream().map(User::getId).collect(Collectors.toList());
根据单一字段分组
Product prod1 = new Product(1L, 1, new BigDecimal("15.5"), "面包", "零食");
Product prod2 = new Product(2L, 2, new BigDecimal("20"), "饼干", "零食");
Product prod3 = new Product(3L, 3, new BigDecimal("30"), "月饼", "零食");
Product prod4 = new Product(4L, 3, new BigDecimal("10"), "青岛啤酒", "啤酒");
Product prod5 = new Product(5L, 10, new BigDecimal("15"), "百威啤酒", "啤酒");
Product prod6 = new Product(5L, 7, new BigDecimal("25"), "百威啤酒", "啤酒");
List<Product> prodList = List.of(prod1, prod2, prod3, prod4, prod5, prod6);
Map<String, List<Product>> map1 = prodList.stream().collect(Collectors.groupingBy(Product::getCategory));
输出结果:
啤酒=[Product{id=4, num=3, price=10, name='青岛啤酒', category='啤酒'}, Product{id=5, num=10, price=15, name='百威啤酒', category='啤酒'}, Product{id=5, num=7, price=25, name='百威啤酒', category='啤酒'}]
零食=[Product{id=1, num=1, price=15.5, name='面包', category='零食'}, Product{id=2, num=2, price=20, name='饼干', category='零食'}, Product{id=3, num=3, price=30, name='月饼', category='零食'}]
根据不同条件分组
Map<String, List<Product>> map3 = prodList.stream().collect(Collectors.groupingBy(item -> {
if (item.getNum() > 3) {
return "num大于3";
} else if (item.getNum() < 3) {
return "num小于3";
} else {
return "num等于3";
}
}));
Set<Map.Entry<String, List<Product>>> entries3 = map3.entrySet();
for (Map.Entry<String, List<Product>> entry : entries3) {
System.out.println(entry);
}
输出结果
num小于3=[Product{id=1, num=1, price=15.5, name='面包', category='零食'}, Product{id=2, num=2, price=20, name='饼干', category='零食'}]
num等于3=[Product{id=3, num=3, price=30, name='月饼', category='零食'}, Product{id=4, num=3, price=10, name='青岛啤酒', category='啤酒'}]
num大于3=[Product{id=5, num=10, price=15, name='百威啤酒', category='啤酒'}, Product{id=5, num=7, price=25, name='百威啤酒', category='啤酒'}]
使用分组减少循环层数
for (AccountRelationVo relationVo : relationList) {
for (GatewayRoutingPageVo gatewayRoutingVo : gatewayRoutingList) {
if (relationVo.getGatewayName().equals(gatewayRoutingVo.getName())) {
BeanUtils.copyProperties(gatewayRoutingVo, relationVo);
break;
}
}
}
//使用分组后
for (AccountRelationVo relationVo : relationList) {
BeanUtils.copyProperties(gatewayRoutingMapList.get(relationVo.getGatewayName()), relationVo);
}
去重
list = list.stream().distinct().collect(Collectors.toList());
toMap键值对
toMap(Function, Function) 返回一个 Collector,它将元素累积到一个 Map中,其键和值是将提供的映射函数应用于输入元素的结果。
// 获取正确的<id, phoneNum> map 报空指针,解决方案
Map<Integer, String> stringMap = new HashMap();
if (Phones.size() > 0) {
stringMap = Phones.stream().collect(Collectors.toMap(Phones::getId,e -> e.getPhoneNum() == null ? "" : e.getPhoneNum()));
}
/**
* id作为Map的key,name作为value的集合
*/
Map<Integer, String> collect1 = employeeList.stream().collect(Collectors.toMap(Employee::getId, Employee::getName));
System.out.println(collect1);//{101=张三, 102=李四, 103=王五, 104=赵六}
forEach
对于java8中的特殊写法lamada表达式中,不能使用break,会提示错误;java8中使用return,会跳出当前循环,继续下一次循环,作用类似continue;
foreach循环可以遍历输出数组里面的元素,但是不能通过foreach来修改元素.
做forEach,需要把外面的变量赋值如int,long,boolean,需要外面定义原子类型AtomicReference
//循环遍历赋值
list.forEach(f -> f.setCallId("'" + f.getCallId()));
list.forEach(objTrunk -> {
user.setAreaCode(objTrunk.getAreaCode());
});
AtomicReference<Boolean> isAiExecSuccess = new AtomicReference<>(false);
operationRecords.forEach(rec -> {
if (StringUtils.isNotEmpty(rec.getRemark())) {
if (rec.getRemark().equals("数据执行成功")) {
isAiExecSuccess.set(true);
return;
}
}
});