Java中是否存在弱/软双向映射?(Is there a weak/soft bidirectional Map in Java?)
我想要一个“符号字母表”来获得带有弱/软引用的双向映射。 类似于Apache的Common
BidiMap
(或Guava的BiMap
),具有WeakMap
的界面。一个解决方案是使用
BiMap<WeakReference<K>,V>
和偶尔清理条目,但它不是最佳的。 有什么建议么?I would like a "symbol alphabet" to have a bi-directional map with weak/soft references. Something like an Apache's Common
BidiMap
(or Guava'sBiMap
) with aWeakMap
-like interface.A solution would be to use
BiMap<WeakReference<K>,V>
and once in a while clean-up entries, but it's not optimal. Any suggestions?
原文:https://stackoverflow.com/questions/14011082
最满意答案
这可能不是最好的办法,但它对我有效。
您可能会发现这有帮助: http : //www.vogella.com/tutorials/JavaSerialization/article.html
我不得不在一些项目中也这样做。 这就是我做的:
public final class cacheThis { private cacheThis() {} public static void writeObject(Context context, String fileName, Object object) throws IOException { FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(object); oos.flush(); oos.close(); fos.close(); } public static Object readObject(Context context, String fileName) throws IOException, ClassNotFoundException { FileInputStream fis = context.openFileInput(fileName); ObjectInputStream ois = new ObjectInputStream(fis); Object object = ois.readObject(); fis.close(); return object; } }
要写入文件:
cacheThis.writeObject(YourActivity.this, fileName, movieList);
从文件读取:
movieList.addAll((List<Movie>) cacheThis.readObject( VideoActivity.this, fileName));
你必须让你的Movie类
implements Serializable
This probably isn't the best way to do it, but it worked for me.
You might find this helpful: http://www.vogella.com/tutorials/JavaSerialization/article.html
I had to do the same in some project. This is what I did:
public final class cacheThis { private cacheThis() {} public static void writeObject(Context context, String fileName, Object object) throws IOException { FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(object); oos.flush(); oos.close(); fos.close(); } public static Object readObject(Context context, String fileName) throws IOException, ClassNotFoundException { FileInputStream fis = context.openFileInput(fileName); ObjectInputStream ois = new ObjectInputStream(fis); Object object = ois.readObject(); fis.close(); return object; } }
To write to file:
cacheThis.writeObject(YourActivity.this, fileName, movieList);
To read from file:
movieList.addAll((List<Movie>) cacheThis.readObject( VideoActivity.this, fileName));
You have to have your Movie class
implements Serializable
相关问答
更多-
使用离线同步设计Android应用程序。(Desiging an Android App with Offline Sync. Should I use SyncAdapter?)[2022-03-30]
我认为最适合您的问题的是使用GCM Push将未同步的服务器数据推送到您的Android客户端。 为了同步你的客户端的SQL数据,你会知道什么还没有更新到服务器,并且只要设备是网络连接的,就将这些数据推送到服务器。 客户端和服务器之间的高层通信可以实现什么是不同步的决定。 上面假设你有你自己的服务器,你的客户端将与之通信。 考虑到你不拥有服务器 - 你写一个同步适配器的策略看起来不错。 此链接将帮助您了解同步适配器的复杂性 I think what would be optimal for your pro ... -
也许你可以使用SharedPreferences保存它们。 您可以通过执行以下操作来存储json对象/数组: 将json保存到SharedPreferences: PreferenceManager.getDefaultSharedPreferences(context).edit() .putString("theJson",jsonObject.toString()).apply(); 获取存储的json: JsonObject jsonObject = PreferenceManager. ...
-
脱机平铺缓存支持现在在版本0.5.1的Mapbox Android SDK中可用。 它于2014年12月20日发布。 以下是如何开始的一个基本示例: OfflineMapDownloader offlineMapDownloader = OfflineMapDownloader.getOfflineMapDownloader(getActivity()); BoundingBox boundingBox = mapView.getBoundingBox(); CoordinateSpan span = ne ...
-
Android JSON停止缓存(Android JSON stop caching)[2023-04-11]
我已经弄明白问题是什么了。 当我更改密码时,会话未关闭,并且它与旧数据连接。 所以我不得不使用这段代码“注销”: client.getConnectionManager().closeExpiredConnections(); client = null; 然后我创建了一个新的Http客户端。 I have figured it out what the problem was. When I changed the password the session was not closed and ... -
我的第一个建议是不发布你的API密钥。 Pixabay可能是免费的,但在互联网上共享API密钥会使您的基础设施处于危险之中。 在查询请求时,Pixabay似乎正在设置一些可能会影响缓存的标头。 Access-Control-Max-Age → 86400 Cache-Control → max-age=86400 Content-Type → application/json 查看HttpResponseCache上的Android文档它概述了在设置HttpResponseCache之后如何强制高速缓存响 ...
-
这可能不是最好的办法,但它对我有效。 您可能会发现这有帮助: http : //www.vogella.com/tutorials/JavaSerialization/article.html 我不得不在一些项目中也这样做。 这就是我做的: public final class cacheThis { private cacheThis() {} public static void writeObject(Context context, String fileName, Object object) ...
-
出于某种原因,唯一将播放缓存视频的移动浏览器是Android上的Firefox。 但是 ,有一个解决方案(对于Safari iOS8 +,Chrome,Firefox - 我还没有测试过IE) - 他们甚至会在离线状态下在objectURL中播放视频blob! 你做的是这样的: 创建一个indexedDB 在db request = transaction.objectStore( "myobjectstorename" ).get( savedId )查找存储的视频 检查返回,如果有,请转到步骤5,否则执 ...
-
将pouchdb客户端用作甚至数千条记录的客户端数据库也是如此。 It's okey to use pouchdb client as client database for even thousands records.
-
您应该尝试使用JSON解析库,这样更容易。 杰克逊是一个非常好的JSON库,看看: jackson.codehaus.org 第一步: JacksonInFiveMinutes You should try to use a JSON parsing lib, it's much easier. Jackson is a really good JSON lib, take a look: jackson.codehaus.org First steps: JacksonInFiveMinutes
-
如何从服务器获取或检索数据并在Android应用程序上发布更新 使用HTTPClient 。 如何管理离线存储意味着无论何时没有互联网连接如何在Android应用程序上更新数据 我想你不明白什么是离线存储意味着什么。 如果没有互联网连接,就是这样。 您无法更新您的内容。 如果您有Internet连接,则脱机存储很有用,但不是每次都有。 当您没有互联网连接时,您在连接互联网时下载的内容可以保存到SQLLite以便加载。 我实际上使用php或json作为中间文件从服务器获取数据并将更新发送到Android应用程 ...