`
thisjava
  • 浏览: 13419 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

immutable reference & collections

阅读更多

最近遭遇collection reference错误使用的bug,原始代码:

List<CallbackRequestModel> callbackModels = new ArrayList<CallbackRequestModel>(datas.size());

for (TaskData<BatchUserData> taskData : datas) {
CallbackRequestModel request = new CallbackRequestModel();
BatchUserData user = taskData.getData();
request.setData(user.getUserId());

//先解析任务配置时的扩展数据
Map<String, Object> configExtra=dispatch.getSubTask().getTaskConfig().getOutBizMarkMap();
if (configExtra==null){
configExtra=new HashMap<String,Object>();//原始任务数据被替换、当前临时数据写入任务配置中、被其他线程使用
}

//再解析任务数据的扩展信息
Map<String, Object> dataExtra = JSONUtil.parseToMap(taskData.getExtraData());
//任务数据扩展信息不为空时,合并两份扩展
if (!CollectionUtils.isEmpty(dataExtra)) {
configExtra.putAll(dataExtra);
}

request.setExtraData(configExtra);
callbackModels.add(request);
}

修改后:
List<CallbackRequestModel> callbackModels = new ArrayList<CallbackRequestModel>(datas.size());

for (TaskData<BatchUserData> taskData : datas) {
CallbackRequestModel request = new CallbackRequestModel();
BatchUserData user = taskData.getData();
request.setData(user.getUserId());
//单独新增扩展数据map,拷贝元素任务扩展数据
Map<String, Object> configExtra = new HashMap<String, Object>();

//先解析任务配置时的扩展数据
Map<String, Object> outBizMarkMap=dispatch.getSubTask().getTaskConfig().getOutBizMarkMap();
if (!CollectionUtils.isEmpty(outBizMarkMap)){
configExtra.putAll(outBizMarkMap);
}

//再解析任务数据的扩展信息
Map<String, Object> dataExtra = JSONUtil.parseToMap(taskData.getExtraData());
//任务数据扩展信息不为空时,合并两份扩展
if (!CollectionUtils.isEmpty(dataExtra)) {
configExtra.putAll(dataExtra);
}

request.setExtraData(configExtra);
callbackModels.add(request);
}

java collection reference immutable引用不可修改:

List<String> names = new ArrayList<String>();
names.add(“helo”);
names.add(“google”);
final List<String> list = names;
list.add(“baidu”);//集合可修改
list = new ArrayList<String>();//引用不可修改
java collection immutable引用所指对象不可修改:

List<String> names = new ArrayList<String>();
names.add(“helo”);
names.add(“google”);
List<String> copyList = Collections.unmodifiableList(names);
copyList.add(“world”); 集合不可修改

copyList=new ArrayList<String>();//引用可修改

分享到:
评论

相关推荐

    C# - CSharp 12 in a Nutshell The Definitive Reference

    Strings in C# are immutable sequences of characters. They are used extensively in C# programs for storing text-based data. Characters are individual symbols that can be manipulated using various ...

    hibernate_reference中文文档.pdf

    ### Hibernate Reference Documentation 知识点概述 #### 1. 教程 ##### 1.1 第一部分 — 第一个 Hibernate 应用程序 该章节详细介绍了如何创建并运行一个简单的 Hibernate 应用程序。 - **1.1.1 设置**:这部分...

    google common jar包1.0

    例如,`ImmutableList`、`ImmutableMap`和`ImmutableSet`等不可变集合类,可以在保证数据安全的同时提高代码的可读性和可维护性。 在Android开发中,`google-collections-1.0.jar`可能被用在`Setting`或者`SystemUI...

    Rust - Rust Programming By Example - Guillaume Gomez

    Tuples are fixed-length collections of heterogeneous data types: ```rust let p = (1.0, 2.0); let (x, y) = p; ``` #### Conclusion Chapter 1 of "Rust Programming By Example" provides a solid ...

    Professional C# 7 and .NET Core 2.0

    C# 7 focuses on data consumption, code simplification, and performance, with new support for local functions, tuple types, record types, pattern matching, non-nullable reference types, immutable ...

    Effective C#

    - **Issues:** Incorrect implementation can lead to poor performance in hash-based collections. - **Guidelines:** - Ensure consistency between `Equals` and `GetHashCode`. - Combine hash codes using ...

    python3.6.5参考手册 chm

    PEP 372: Adding an Ordered Dictionary to collections PEP 378: Format Specifier for Thousands Separator PEP 389: The argparse Module for Parsing Command Lines PEP 391: Dictionary-Based Configuration...

    net常见面试题

    在.NET框架中,数据类型可以分为两大类:值类型(Value Types)和引用类型(Reference Types)。这两种类型的主要区别在于它们如何在内存中存储数据。 - **值类型**:这些类型的数据直接存储在栈内存中。当声明一个...

    ImTools:快速且高效存储的不可变集合和辅助数据结构

    基准测试将与ImMap V1版本,V2 ImMapSlots版本和各种BCL C#集合进行比较,包括实验性的Microsoft.Collections.Extensions.DictionarySlim&lt;K&gt; 。 注意:请记住,与Dictionary , DictionarySlim甚至...

Global site tag (gtag.js) - Google Analytics