Java 重复数据消除集合中的对象存储
考虑一个具有以下接口的简单对象存储:Java 重复数据消除集合中的对象存储,java,coding-style,Java,Coding Style,考虑一个具有以下接口的简单对象存储: // add an object with ‘blob’ content into the system and return an id int put(string blob); // retrieve the contents of an object identified by ‘id’. NULL if doesn’t exist string get(int); // delete an object identified by ‘id’
// add an object with ‘blob’ content into the system and return an id
int put(string blob);
// retrieve the contents of an object identified by ‘id’. NULL if doesn’t exist
string get(int);
// delete an object identified by ‘id’
void delete(int id);
//number of (non duplicate) objects stored in the object store
int size();
要求对象存储必须消除重复对象。Iif如果相同的字节序列存储两次–则存储区不能存储数据两次。物体 可以相当大,比如说,大小从1K到5MB不等。水滴是不可变的。。 我们正在从这些API调用中寻找标准的顺序一致性语义。如果一个对象是“put”,那么下一个立即的“get”调用 应返回先前的put值。例如,如果客户机执行以下操作:
Id = objectstore.put(data);
data1 = objectstore.get(id);
第二个操作必须返回与“data”指向的字节序列相同的字节序列。任何其他客户端/进程/线程都不能
干涉那件事
到目前为止,我的代码是:
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class ObjectStore {
String blobString;
Object objectId;
public ObjectStore(String blobString, Object objectId) {
this.blobString = blobString;
this.objectId = objectId;
}
@Override
public boolean equals(Object o){
if(!(o instanceof ObjectStore)){
return false;
}
if((o == null) || (o.getClass() != this.getClass()))
return false;
// object must be Test at this point
ObjectStore store = (ObjectStore)o;
return blobString.toString() == store.blobString &&
(objectId != null && objectId.equals(store.objectId));
}
@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + blobString.hashCode();
hash = 31 * hash + ((objectId == null) ? 0 : objectId.hashCode());
return hash;
}
Set<String> set = new HashSet<String>();
/**
* Put object into store and return id.
* @param blobString
* @return
*/
public int put(String blobString) {
set.add(blobString);
return 0;
}
/**
* Get object corresponding to id. Return null if no such object exists.
* @param objectId
* @return
*/
public String get(int objectId) {
return null;
}
/**
* Release object - don't need it anymore.
* @param objectId
*/
public void delete(int objectId) {
// stub
}
/**
* Number of distinct blobs stored in the objectStore
* @return
*/
public int size() {
// stub
return 0;
}
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
}
}
import java.util.HashMap;
导入java.util.HashSet;
导入java.util.Map;
导入java.util.Set;
公共类对象存储{
线团;
Object objectId;
公共对象存储(字符串blobString,对象objectId){
this.blobString=blobString;
this.objectId=objectId;
}
@凌驾
公共布尔等于(对象o){
如果(!(对象存储的实例)){
返回false;
}
if((o==null)| |(o.getClass()!=this.getClass())
返回false;
//此时必须测试对象
ObjectStore=(ObjectStore)o;
返回blobString.toString()==store.blobString&&
(objectId!=null&&objectId.equals(store.objectId));
}
@凌驾
公共int hashCode(){
int hash=7;
hash=31*hash+blobString.hashCode();
hash=31*hash+((objectId==null)?0:objectId.hashCode();
返回散列;
}
Set=newhashset();
/**
*将对象放入存储并返回id。
*@param blobString
*@返回
*/
公共int put(字符串blobString){
set.add(blobString);
返回0;
}
/**
*获取id对应的对象。如果不存在此类对象,则返回null。
*@param objectId
*@返回
*/
公共字符串get(int objectId){
返回null;
}
/**
*释放对象-不再需要它。
*@param objectId
*/
公共无效删除(int objectId){
//存根
}
/**
*存储在objectStore中的不同Blob数
*@返回
*/
公共整数大小(){
//存根
返回0;
}
公共静态void main(字符串[]args){
Map Map=newhashmap();
}
}
我无法确定使用Map或Set接口需要什么,因为put()方法返回对象id。在将对象映射到整数id时使用
映射。集合不一定映射数据,它们只是没有重复元素的集合
然而,由于您似乎正在编写一个接口,所以实现的内部应该不重要。使用最有效的方法,并符合接口要求
编辑2:
如果我实现这个,我会使用一个整数在put
操作上生成唯一的ID。这将提供高达约40亿次put
操作的唯一ID
此外,还需要第二个映射来检查之前是否添加了一些blob序列。这样,您就可以使用与第一次相同的键替换旧斑点
int idProvider=0;
HashMap<Integer, String> map1 = new HashMap<Integer, String>();
HashMap<String, Integer> map2 = new HashMap<String, Integer>();
public int put(String blob){
int id = -1;
if(map2.containsKey(blob)){
id = map2.get(blob);
}else{
id = idProvider;
idProvider+=1;
map2.put(blob, id);
}
map1.put(id, blob);
return id;
}
public void delete(int id){
String blob = map1.remove(id);
if(blob!=null){
map2.remove(blob);
}
}
String get(int id){
String blob = map1.get(id);
return blob;
}
int size(){
return map1.size();
}
你能帮助我如何实现put()方法吗?当然,我已经编辑了这个问题,包含了完整的解决方案,并给出了当前问题的描述。如果这就足够了,请接受我的回答。如果没有,请告诉我。在获取blob中的值时,我们可以使用hashCode()方法的哈希值忽略重复值吗。如何使用like return blob.hashCode()??我已经使用过,是否正确??公共字符串get(Integer objectId){int hash=objectId.hashCode();字符串blobString=map.get(hash);return blobString;}Hashcodes可以在这里工作,但Hashcodes可能有冲突。我的代码也可能有冲突,但前提是您一次放置超过40亿个字符串。hashcode可能工作得很好,因为与hashcode发生冲突的可能性也很小,但这也很有效。
ObjectStore store = new ObjectStore();
int id1 = store.put("hello");
int id2 = store.put("world");
int id3 = store.put("world");
if(store.size()!=2)
throw new RuntimeException("Wrong size");
if (id2 != id3)
throw new RuntimeException("Ids not equal?");
String strHello = store.get(id1);
String strWorld = store.get(id3);
if (!strHello.equals("hello") || !strWorld.equals("world"))
throw new RuntimeException();
store.delete(id3);
String strShouldBeNull = store.get(id3);
if(strShouldBeNull!=null)
throw new RuntimeException();