概述
packageunit;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.Collections;importjava.util.List;importcom.google.protobuf.InvalidProtocolBufferException;importcom.proto.TestProto;importcom.proto.TestProto.Person;importcom.proto.TestProto.Person.PhoneNumber;publicclassMain {publicstaticvoidmain(String[] args)throwsInvalidProtocolBufferException {//空创建Person.Builder builder=Person.newBuilder();
builder.setName("liukaiyi");
builder.setId(1);
builder.setEmail("email");
PhoneNumber.Builder pBuilder1=PhoneNumber.newBuilder();
pBuilder1.setNumber("1");
pBuilder1.setType(TestProto.Person.PhoneType.MOBILE);
PhoneNumber.Builder pBuilder2=PhoneNumber.newBuilder();
pBuilder2.setNumber("2");
pBuilder2.setType(TestProto.Person.PhoneType.HOME);
builder.addPhone(pBuilder1);
builder.addPhone(pBuilder2);
//此处为存储到数据库 或 传输!byte[] data=builder.build().toByteArray() ;for(bytetb : data )
System.out.print( (char)tb );//组建Person person=Person.parseFrom(data);
Person.Builder builder2=Person.newBuilder(person);
builder2.setEmail("test");
PhoneNumber.Builder pBuilder3=PhoneNumber.newBuilder();
pBuilder3.setNumber("3");
pBuilder3.setType(TestProto.Person.PhoneType.HOME);
builder2.addPhone(pBuilder3);
//这 我最不明白的地方 , list 这 只能 使用 这种方法才可以删除 list ????
List list = new ArrayList(builder2.getPhoneList());
List ltmp = builder2.getPhoneList() ;
for(int i=0;i
if(ltmp.get(i).getNumber().equals("2"))
list.remove( ltmp.get(i) ) ;
}
builder2.clearPhone().addAllPhone(list);builder2.clearEmail();
System.out.println(list.size());
System.out.println();byte[] data2=builder2.build().toByteArray() ;for(bytetb : data2 )
System.out.print( (char)tb );
}
}
最后
以上就是生动大船为你收集整理的protobuffer java_google proto buffer 使用(java)的全部内容,希望文章能够帮你解决protobuffer java_google proto buffer 使用(java)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复