0

I've a priority queue in which I've put several objects. These objects are from a class Prod that has the following fields:

int Id;
String Gtin;
String Msg;

so my queue looks like this:

PriorityQueue<Prod> pq= new PriorityQueue<Prod>();
pq.add(new Prod(1,"454545","Counter"));
pq.add(new Prod(1,"676767","Counter"));
pq.add(new Prod(1,"787878","Counter"));
pq.add(new Prod(2,"232323","FLoor"));
pq.add(new Prod(2,"323232","Floor"));
pq.add(new Prod(2,"989898","Floor"));

I am then using the iterator to fetch the elements

Iterator iterator = pq.iterator();

while (iterator.hasNext()){
Object prod= iterator.next();
System.out.println("Prod:"+ prod);
}

However, I need to fetch all the Gtins belonging to Id 1 and put them into a file i.e. "454545","676767","787878" for Id 1 needs to be extracted in this example and the same should be repeated for all other Ids. I don't know how to do this. Any help would be truly appreciated.

Thanks

user68112
  • 1
  • 1
  • 1. Adding elements to `PriorityQueue` is incorrect. 2. Where you are facing the problem. – SatyaTNV Oct 16 '15 at 04:28
  • `Prod` must implement `Comparable` to be used with a [`PriorityQueue`](http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html). What is the [natural ordering](http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html) of `Prod`? – Andreas Oct 16 '15 at 04:57
  • the items in the queue are sorted based on the Ids which is the natural sorting and the `Prod` has implemented the `Comparable` interface: @Override public int compareTo(Prod prod) { return this.ID.compareTo(prod.ID); } } – user68112 Oct 16 '15 at 05:09
  • @laune There is no evidence anywhere that writing the file (singular) is the problem. – user207421 Oct 16 '15 at 05:30
  • @EJP I did suggest the sort. OP didn't say clearly where the problem lies. Thanks for downvoting. – laune Oct 16 '15 at 05:34
  • @laune You suggested (a) a redundant sort and (b) a non-existent problem writing files. See the duplicated question for the real answer. – user207421 Oct 16 '15 at 05:35
  • @EJP So why is the sort by Id redundant while the priority queue is not sorted? How would you group the collection by id values? Please enlighten me! – laune Oct 16 '15 at 05:39
  • @laune See my answer in the duplicated question. I'm not going to repeat it all here like a parrot. The `Comparator` does the grouping, as the OP himself has already pointed out. – user207421 Oct 16 '15 at 05:41
  • But removing the entries from the queue, one by one, will destroy the queue - and you don't know that this is OK, do you? The javadoc suggests the sort, as has been pointed out elsewhere. At least, sorting is a plausible alternative to iterated removal. – laune Oct 16 '15 at 05:43
  • @laune That is the intended usage of a `PriorityQueue` and indeed of any queue: you put things into it and you take them out again. – user207421 Oct 16 '15 at 05:46
  • @EJP Quite so, but when I answered with the sort, it wasn't even clear what the ordering principle of the queue was in the first place. Note that OP supplied this info only later. I didn't see that until long after... – laune Oct 16 '15 at 05:51

0 Answers0