Java 如何从protobuf中的不可修改列表中删除项
我有一个protobuf,其中有一个成员列表 我想替换此列表中的一项 我试图删除一个项目Java 如何从protobuf中的不可修改列表中删除项,java,list,protocol-buffers,proto,Java,List,Protocol Buffers,Proto,我有一个protobuf,其中有一个成员列表 我想替换此列表中的一项 我试图删除一个项目I,并在同一位置添加另一个项目I List<Venues.Category> categoryList = builder.getCategoryList(); categoryList.remove(i); 如何执行替换?解决方案之一是创建一个新的可修改列表,其中包含旧列表-我的意思是将其传递给的构造函数,例如newArrayList(): List modifiable=新建Arr
I
,并在同一位置添加另一个项目I
List<Venues.Category> categoryList = builder.getCategoryList();
categoryList.remove(i);
如何执行替换?解决方案之一是创建一个新的可修改列表,其中包含旧列表-我的意思是将其传递给的构造函数,例如new
ArrayList()
:
List modifiable=新建ArrayList(不可修改);
从现在起,您应该能够删除和添加元素。我最终克隆了列表,修改了克隆的列表,并将其替换为原来的列表
List<Venues.Category> clone = categoryList.stream().collect(Collectors.toList());
clone.remove(i);
clone.add(i, modifyCategory(category, countryAbbr, gasStationConfig));
builder.clearCategory();
builder.addAllCategory(clone);
List clone=categoryList.stream().collect(Collectors.toList());
克隆。删除(i);
添加(i,修改类别(类别,countryAbbr,gasStationConfig));
builder.clearCategory();
builder.addAllCategory(克隆);
如果列表来自数组,它将抛出java.lang.UnsupportedOperationException
/*Example*/
String[] strArray = {"a","b","c","d"};
List<String> strList = Arrays.asList(strArray);
strList.remove(0); // throw exception
/*示例*/
字符串[]strArray={“a”、“b”、“c”、“d”};
List strList=Arrays.asList(strArray);
strList.remove(0);//抛出异常
因为原始数组和列表是链接的。
列表的大小是固定的,更改将对两者都产生影响
add()或remove()无法完成。如果要更新protobuff生成器列表,可以通过以下方法实现:
//Considering builder is your Category list builder.
List<Venues.Category> categoryList = builder.getCategoryList(); // Previous list.
builder.setCategory(1, categoryBuilder.build()); //categoryBuilder is your object builder which you want to replace at first location.
// Hope you will get setCategory function by protobuffer, or something like that. because it's created by protobuffer compilation.
List<Venues.Category> updatedCategoryList = builder.getCategoryList();
//Your updated list with new object replaced at 1.
//考虑生成器是您的类别列表生成器。
List categoryList=builder.getCategoryList();//上一份名单。
setCategory(1,categoryBuilder.build())//categoryBuilder是要在第一个位置替换的对象生成器。
//希望您能通过protobuffer或类似的方式获得setCategory函数。因为它是由protobuffer编译创建的。
List updatedCategoryList=builder.getCategoryList();
//已更新列表,新对象已在1处替换。
您的categoryList是否从数组转换而来?List categoryList=builder.getCategoryList();我想知道生成器内部。getCategoryList()是按数组还是按列表工作?@EladBenda检查我的答案。我希望有帮助!
/*Example*/
String[] strArray = {"a","b","c","d"};
List<String> strList = Arrays.asList(strArray);
strList.remove(0); // throw exception
//Considering builder is your Category list builder.
List<Venues.Category> categoryList = builder.getCategoryList(); // Previous list.
builder.setCategory(1, categoryBuilder.build()); //categoryBuilder is your object builder which you want to replace at first location.
// Hope you will get setCategory function by protobuffer, or something like that. because it's created by protobuffer compilation.
List<Venues.Category> updatedCategoryList = builder.getCategoryList();
//Your updated list with new object replaced at 1.