D
Daniel Pitts
You're right, that was uncalled for by me. I apologize.Twisted said:Daniel said:Damo hasn't argued with the point [implied insult directed towards me deleted]
Oh, no, not this shit again.
You're right, that was uncalled for by me. I apologize.Twisted said:Daniel said:Damo hasn't argued with the point [implied insult directed towards me deleted]
Oh, no, not this shit again.
using lists at all.
2) If you don't mind some temporary storage,
LinkedList merge(List multipleLists) {
LinkedList newList = new LinkedList();
Set newListSet = new HashSet();
for(Iterator li = multipleLists.iterator(); li.hasNext(){
List l = (List)li.next();
for(Iterator i = l.iterator(); i.hasNext(){
Object o = i.next();
if(!newListSet.contains(o)) {
newListSet.add(o);
newList.add(o);
}
}
}
return newList;
}may serve (untested code)
I'm assuming your multiple lists are held in a list...
I think that's O(N).
It also kind of retains the order of the input lists
BugBear
djthomp said:It can be simpler if you take advantage of set's addAll method:
LinkedList merge(List multipleLists) {
Set newListSet = new HashSet();
for(Iterator li = multipleLists.iterator(); li.hasNext()
newListSet.addAll((List)li.next());
return new LinkedList(newListSet);
}
hello all,
I was'nt ignoring you're advice. I was just exploring all the options,
seeing as I need some of the advantages of Linked Lists and Sets. In
particular the no duplicate feature of sets and i needed to keep the
order items were added(not necessarlly sorted)(Linked Lists), and
inserting into a particulaar point in the colection(Linked Lists).
I have been trying to work with sets but they dont seem to suit my
needs. Its too convoluted to retain the ordering of the collection
Anyway you're words of advice are appreciated.
Damo said:i needed to keep the
order items were added(not necessarlly sorted)(Linked Lists)
you might take at LinkedHashSet. It keeps track of item order by using
saving the insertion order with an internal linked list. Iterating
over the LinkedHashSet gives you its contents in their original order.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedHashSet.html
djthomp said:...you might take -a look- at LinkedHashSet. Sigh.
Daniel said:Or, using a more type safe, Java 1.5 approach:
public static <E> List<E> merge(
Iterable<? extends Collection<? extends E>> collections) {
final Set<E> set = new LinkedHashSet<E>();
for (Collection<? extends E> collection: collections) {
set.addAll(collection);
}
return new LinkedList<E>(set);
}
Patricia said:Don't care about order: HashSet
Insertion order: LinkedHashSet
Natural order: TreeSet
Comparator-specified order: TreeSet
Patricia
John said:public class PositionalHolder<T>
implements Comparable<PositionalHolder<T>> {
public double position;
public T object;
public PositionalHolder (T object, double position) {
this.object = object;
this.position = position;
}
@SuppressWarnings("unchecked")
public void equals (Object x) {
return (x == this)
||(x instanceof PositionalHolder
&& ((PositionalHolder)x).position
== position);
}
public int hashCode () {
return Double.valueOf(position).hashCode();
}
public int compareTo (PositionalHolder<T> other) {
return (position < other.position)
?-1(position > other.position)?1:0);
}
public void putBetween (PositionalHolder<T> x,
PositionalHolder<T> y) {
// Convenience method.
position = (x.position + y.position)/2;
}
}
bugbear said:Yeah - sorry, we're still using Java 1.4 here, for reasons
of install base compatibility.
BugBear
Daniel said:I find it a lot easier to upgrade an install base, than to maintain 1.4
code.
John said:Agreed. All those casts! They make pre-1.5 Java the first runner up for
the Too Damn Many Parentheses Award. (We all know who the actual winner
is, I trust.)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.