Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Java集合中获取项目_Java_Collections - Fatal编程技术网

从Java集合中获取项目

从Java集合中获取项目,java,collections,Java,Collections,我有一套昂贵的物品 这些对象具有ID,equals使用这些ID表示相等 这些对象的类型有两个构造函数;一个用于昂贵的对象,另一个仅用于设置ID 因此,我可以使用Set.contains(新对象(ID))检查特定ID是否在集中 但是,在确定对象在集合中之后,我无法获取集合中的对象实例 如何获取集合包含的确切对象?如果以id作为键的HashMap不起作用,那么我将使用HashMap将对象同时作为键和值。如果要从集合中获取,应该使用映射 (注意,大多数集合实现都是映射的包装器) Map=new。。。。

我有一套昂贵的物品

这些对象具有ID,
equals
使用这些ID表示相等

这些对象的类型有两个构造函数;一个用于昂贵的对象,另一个仅用于设置ID

因此,我可以使用
Set.contains(新对象(ID))
检查特定ID是否在
集中

但是,在确定对象在集合中之后,我无法获取集合中的对象实例


如何获取集合包含的确切对象?

如果以id作为键的
HashMap
不起作用,那么我将使用
HashMap
将对象同时作为键和值。

如果要从集合中
获取
,应该使用映射

(注意,大多数集合实现都是映射的包装器)

Map=new。。。。
Value=map.get(新键(ID));
在您的情况下,键和值可以是相同的类型,但这通常不是一个好主意,因为键(如集合的元素)应该是不可变的。

您可以使用来自Apache Commons集合的:

Predicate eq = new EqualPredicate(new Object(ID));
FilterIterator filter = new FilterIterator(set.iterator(), eq);
Object o = (Object) filter.next();
显然,这将是一种昂贵的访问方式,但是如果您固定使用
集,您将不得不在某个时候对其进行迭代。

考虑在中使用该类。除了
Set
之外,它还实现了该接口<代码>池
添加了与put和get类似的API<代码>池
比映射内存效率更高,因为它不为值保留内存,只为键保留内存

UnifiedSet<Integer> pool = UnifiedSet.newSet();

Integer integer = 1;
pool.add(integer);

Assert.assertSame(integer, pool.get(new Integer(integer)));
UnifiedSet-pool=UnifiedSet.newSet();
整数=1;
pool.add(整数);
Assert.assertSame(整数,pool.get(新整数(整数));

注意:我是Eclipse集合的提交者。

这里有一个技巧,你可以通过它来获得你想要的。基本上,当您使用contains在哈希集中搜索时,当哈希代码匹配时,将调用您要查找的对象的equals方法。假设您正在处理自己的对象,或者您可以扩展的对象,那么设置一个“信号灯”就很简单了;)类的静态字段和刚刚相等的引用。在这里,您可以调用contains,如果返回true,则检索对象;)

还有,别评判我

import java.util.HashSet;
import java.util.Set;

public class BecauseWhyNot {

    public static void main(String[] args) {

        Set<Poop> sewage = new HashSet<Poop>();

        set.add(new Poop("Morning Delight"));
        set.add(new Poop("Hangover Doodle"));

        System.out.println("Contains Fire Drill?: "
            + set.contains(new Poop("Fire Drill")));
        System.out.println("Contains Morning Delight?: "
            + set.contains(new Poop("Morning Delight")));

        if (Poop.lastlySmelled != null)
            System.out.println("It's you lucky day: " + Poop.lastlySmelled);
        else
            System.out.println("Sorry, try again ;)");
    }

    public static class Poop {
        static Poop lastlySmelled = null;

        String description;

        Poop(String desc) {
            description = desc;
        }

        @Override
        public int hashCode() {
            return 900913 * description.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            lastlySmelled = (Poop) this;
            if (this == obj)            return true;
            if (obj == null)            return false;
            if (getClass() != obj.getClass())   return false;
            Poop other = (Poop) obj;
            if (description == null) {
                if (other.description != null)
                    return false;
            } else if (!description.equals(other.description))
                return false;
            return true;
        }

        public String toString() {
            return "Poop: " + description + "!";
        }
    }
import java.util.HashSet;
导入java.util.Set;
公共课,因为没有{
公共静态void main(字符串[]args){
Set=newhashset();
添加(新便便(“晨曦”);
添加(新便便(“宿醉涂鸦”);
System.out.println(“包含消防演习:”
+套装(新船尾(“消防演习”);
System.out.println(“包含晨曦?”
+包含(新粪便(“晨曦”);
如果(Poop.lastlySmelled!=null)
System.out.println(“今天是你的幸运日:+Poop.lastlySmelled”);
其他的
System.out.println(“对不起,再试一次;)”;
}
公共静态类船尾{
静态Poop lastlySmelled=null;
字符串描述;
船尾(管柱描述){
描述=描述;
}
@凌驾
公共int hashCode(){
返回900913*description.hashCode();
}
@凌驾
公共布尔等于(对象obj){
lastlySmelled=(噗)这个;
如果(this==obj)返回true;
if(obj==null)返回false;
如果(getClass()!=obj.getClass())返回false;
Poop other=(Poop)obj;
if(description==null){
if(other.description!=null)
返回false;
}else如果(!description.equals(other.description))
返回false;
返回true;
}
公共字符串toString(){
返回“Poop:”+说明+“!”;
}
}

我假设键是值字段的子集。如果键是以这种方式从值派生的,并且不能有重复的键,那么也不能有重复的值。这太可怕了,但它让我笑了:)
import java.util.HashSet;
import java.util.Set;

public class BecauseWhyNot {

    public static void main(String[] args) {

        Set<Poop> sewage = new HashSet<Poop>();

        set.add(new Poop("Morning Delight"));
        set.add(new Poop("Hangover Doodle"));

        System.out.println("Contains Fire Drill?: "
            + set.contains(new Poop("Fire Drill")));
        System.out.println("Contains Morning Delight?: "
            + set.contains(new Poop("Morning Delight")));

        if (Poop.lastlySmelled != null)
            System.out.println("It's you lucky day: " + Poop.lastlySmelled);
        else
            System.out.println("Sorry, try again ;)");
    }

    public static class Poop {
        static Poop lastlySmelled = null;

        String description;

        Poop(String desc) {
            description = desc;
        }

        @Override
        public int hashCode() {
            return 900913 * description.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            lastlySmelled = (Poop) this;
            if (this == obj)            return true;
            if (obj == null)            return false;
            if (getClass() != obj.getClass())   return false;
            Poop other = (Poop) obj;
            if (description == null) {
                if (other.description != null)
                    return false;
            } else if (!description.equals(other.description))
                return false;
            return true;
        }

        public String toString() {
            return "Poop: " + description + "!";
        }
    }