Suppose that each object in an n-object list L is colored either red or blue. Give an efficient EREW algorithm to form two lists from the objects in L: one consisting of the blue objects and one consisting of the red objects.
class MyObj { Property a; Property b; Property c; public boolean equals(Object o) { // use == if Property is primitive, like int or something return o instanceof MyObj && a.equals(o.a) && b.equals(o.b) && c.equals(o.c); } // edit - when you override equals, also override hashcode public int hashCode() { return a.hashCode() ^ b.hashCode() ^ c.hashCode(); } public String toString() { return a.toString() + " " + b.toString() + " " + c.toString(); } } // later in your main method Set<MyObj> objSet = new HashSet<MyObj>(); for(MyObj o : list1) objSet.add(o); for(MyObj o : list2) if(objSet.contains(o)) System.out.println(o + " is a match!");
Get Answers For Free
Most questions answered within 1 hours.