开发者

Efficient way to create sub set from an existing one?

I'm searching for a good solution to create a subset from a existing set. For example, i have a set with 1000 Objects (or whatever..) and i need to make a "team" that is made of 100 Objects fr开发者_开发问答om that main set. I'm working with JAVA and i know that if I just create another array with that 100 Objects that will do.

Assuming that i have a class A, a class B and a Class C. Class B contains the set and Class C contains the subset (team..wtv) and the set is made of Class A objects. My problem is that Class A "needs to know" if it makes part of an Object of Class C an what specific instance of Class C he is part of.

I hope i was clearly enough to explain my situation and get some help.

Thank you ;]


I assume that class A has a method to associate an instance of C with A, like in a parent/child relation.

class A {
   void setParent(Object obj);
   Object getParent();
}

In this case, after you filter the original set contained in B, use addA or setMySet to associate your subset with C:

class C {
   private Set<A> mySet;

   public void addA(A a) {
      mySet.add(a);
      a.setParent(this);
   }

   public void setMySet(Set<A> subset) {
      mySet = new HashSet<A>(subSet);
      for(A a in subSet) {
         a.setParent(this);
      }
   }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜