如何在一个巧妙的方法写的java.util.Map成包裹?巧妙、包裹、方法、如何在

2023-09-04 12:21:41 作者:帅哥哥の热头脑

我有一个通用的地图字符串(键,值),该区域是一个Bean的一部分,我必须parcelable。 所以,我可以使用包裹#writeMap方法。该API文档说:

  

请使用writeBundle(包)来代替。平化映射到包裹   在如果需要当前dataPosition(),生长dataCapacity()。该   映射关键字必须是String对象。在地图值用写   writeValue(对象),并必须遵循的规范存在。它是   强烈建议使用writeBundle(捆绑),而不是这个   方法,因为包类提供了一个类型安全的API,允许   你避免神秘类型的错误,在编组的地步。

所以,我可以每个条目在我的地图迭代一个把它放入包,但我仍然在寻找一个更聪明的方式这样做。有没有在Android SDK中我失去了任何方法?

目前,我做的是这样的:

 最后的捆绑包=新包();
最后的迭代器<进入<字符串,字符串>> ITER = links.entrySet()迭代器()。
而(iter.hasNext())
{
    最终进入<字符串,字符串>条目= iter.next();
    bundle.putString(entry.getKey(),entry.getValue());
}
parcel.writeBundle(包);
 

解决方案

最后我做了一点不同的。它遵循你所期望的处理parcelables的模式,所以应该不会陌生。

 公共无效writeToParcel(包裹出来,诠释标志){
  out.writeInt(map.size());
  对于(Map.Entry的<字符串,字符串>输入:map.entrySet()){
    out.writeString(entry.getKey());
    out.writeString(entry.getValue());
  }
}

私人MyParcelable(包裹中){
  //初始化前的地图
  INT大小= in.readInt();
  的for(int i = 0; I<大小;我++){
    字符串键= in.readString();
    字符串值= in.readString();
    map.put(键,值);
  }
}
 
Java多线程并发之同步容器和并发容器 第一篇

在我的应用程序,在地图中键的顺序要紧。我用一个LinekdHashMap至preserve订购及做这种方式保证了键会从包裹被提取之后出现在相同的顺序。

I have a Generic Map of Strings (Key, Value) and this field is part of a Bean which I need to be parcelable. So, I could use the Parcel#writeMap Method. The API Doc says:

Please use writeBundle(Bundle) instead. Flattens a Map into the parcel at the current dataPosition(), growing dataCapacity() if needed. The Map keys must be String objects. The Map values are written using writeValue(Object) and must follow the specification there. It is strongly recommended to use writeBundle(Bundle) instead of this method, since the Bundle class provides a type-safe API that allows you to avoid mysterious type errors at the point of marshalling.

So, I could iterate over each Entry in my Map a put it into the Bundle, but I'm still looking for a smarter way doing so. Is there any Method in the Android SDK I'm missing?

At the moment I do it like this:

final Bundle bundle = new Bundle();
final Iterator<Entry<String, String>> iter = links.entrySet().iterator();
while(iter.hasNext())
{
    final Entry<String, String>  entry =iter.next();
    bundle.putString(entry.getKey(), entry.getValue());
}
parcel.writeBundle(bundle);

解决方案

I ended up doing it a little differently. It follows the pattern you would expect for dealing with parcelables, so it should be familiar.

public void writeToParcel(Parcel out, int flags){
  out.writeInt(map.size());
  for(Map.Entry<String,String> entry : map.entrySet()){
    out.writeString(entry.getKey());
    out.writeString(entry.getValue());
  }
}

private MyParcelable(Parcel in){
  //initialize your map before
  int size = in.readInt();
  for(int i = 0; i < size; i++){
    String key = in.readString();
    String value = in.readString();
    map.put(key,value);
  }
}

In my application, the order of the keys in the map mattered. I was using a LinekdHashMap to preserve the ordering and doing it this way guaranteed that the keys would appear in the same order after being extracted from the parcel.