This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Collections which extend iterators

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am trying to pass data to this constructor, however i am not sure how to create a collection to send to this constructor, can anyone help?


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collection<? extends Iterator<T>> iterators

This is not a collection that extends an iterator - it is a collection of iterators. Or, more precisely, a collection of objects of some type that extends Iterator<T>.
 
claire bird
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply,

when i do this it tells me that i cannot infer type arguements but i am not sure what this means?

 
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, for example, you could use



for the first parameter of the constructor.
 
claire bird
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, I dont understand how i add items to this collection?

 
Marshal
Posts: 69862
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something peculiar about having Lists of Iterators. Are you sure that is what you want?Of course if you try anything with the List (numbers) you will suffer a concurrent modification exception.

Check carefully that you really want a List of Iterators.
 
claire bird
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a bit confused by it too, but my project says i do
 
Paul Clapham
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

helen mac wrote:thanks, I dont understand how i add items to this collection?



Start by creating an object which implements Iterator<Integer>. Then you can add it to that collection.
 
Campbell Ritchie
Marshal
Posts: 69862
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you quite sure the project requires a List of Iterators? It does seem a very weird stipulation.
 
claire bird
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quite sure,

I am confused, I am trying to sort the collections, but its never falling into the instance of method, any ideas why?

 
Paul Clapham
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable "it" from line 3 refers to an Iterator<T> for some type T, this we know because that's how the for-loop works.

Then at line 6 you ask whether the object which "it" refers to an ArrayList<T>. Well, it could possibly refer to an object of some class which extends ArrayList and implements Iterator, but chances are that it doesn't. Unless you specifically created such a class; but you didn't, did you?

I don't understand why you're trying to sort the Iterator anyway. Nothing I've seen so far in this thread suggests that there's anything which can be sorted.
 
claire bird
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im passing in the following to the method, my aim is to pass in more than one collection of possibly varying types and then sort them using the passed in comparator

 
Paul Clapham
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

helen mac wrote:im passing in the following to the method, my aim is to pass in more than one collection of possibly varying types and then sort them using the passed in comparator



But you're only passing in one collection. That collection contains two iterators. You have a comparator which doesn't know how to compare iterators. It knows how to compare Integer objects, so you could use it to sort a Collection<Integer>, but you don't have any of those. So I can't tell what all this is supposed to be doing.
 
claire bird
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the brief.... do you think i have miss understood?

 
Paul Clapham
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, part of those javadocs I find sort of ambiguous, namely that it seems to say that the input iterators must be sorted. Probably that means you should assume that they have already been sorted by the calling program, and since the javadocs mention that the iterators can be infinite it almost certainly means that.

It looks to me like you have an Iterator which is given a bunch of Iterators and has to merge them into one. So MergeIterator might be a better name for it. That means that your code is going to have to extract the data from that bunch of Iterators and return it one item at a time, just as an Iterator normally does. Your idea of sorting collections isn't going to be helpful since you don't have any collections, and you can't extract all the data from an Iterator into a collection because there might be too much of it.
 
claire bird
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply. I have to pass a Comparator<T> comparator into the constructor. So i assumed that I had to sort the collection using the comparator in the constructor
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so we have proved that assumption false.

The provided iterators are assumed to be sorted within themselves.

You need to use the comparator to compare the values from the different source iterators to see which is the next one you need to return.

So assuming we have the following inputs:
input1: {2,4,6,8,10}
input2: {3,6,9,12,15}
input3: {2,3,5,7,11,13,17}

Each input is individually sorted, but this iterator needs to merge them into one result stream. \
The comparator would have to be used to compare the FIRST ITEM in each of those inputs to determine which is the next one to return. (and also to detect duplicates)
In this case that would be 2,3 and 2.
For the above example from successive calls to next() I would expect to get:

2 from input 1
2 from input 3 (discarded if we are getting rid of duplicates)
3 from intput 2
3 from input 3
4 from input 1
5 from input2
...


 
claire bird
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code so far, but i am getting an error do you know what i am doing wrong?



on this line: ArrayList<T> dat = (ArrayList<T>) collectionIterator.next();

i get the error

Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList$Itr cannot be cast to java.util.ArrayList
at MergingIterator.<init>(MergingIterator.java:85)
at Main.main(Main.java:43)
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

helen mac wrote:
Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList$Itr cannot be cast to java.util.ArrayList
at MergingIterator.<init>(MergingIterator.java:85)
at Main.main(Main.java:43)



The iterator that is returned from the ArrayList class is not an instance of ArrayList. You can confirm that easily by checking with the JavaDocs.

Henry
 
claire bird
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this doesnt work either...



what should i use?
 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What method are you writing this code in? Is this in the constructor?
I draw your attention to the comment:



Your algorithm (pulling mostly from your comments) is:
- create a new arraylist "mergedArrayList"
- loop through each iterator received and add every value to mergedArrayList
- sort mergedArrayList
- return an iterator to the merged and sorted list: mergedArrayList

You need to rethink this algorithm based on the requirements.

Particularly this requirement:


So anything that involves looping through all the contents of an iterator is not going to fly.

Given the fact that the individual iterators are already sorted, you can think of these provided iterators like a Queue - or a list of things to process in order.
The next one item to serve MUST be at the front of one of these queues.

Given this assumption, can you write some pseudo code for the getNext() method?




 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting Task! I hope Claire Bird didn't give up?!
I was eager to solve this problem by myself, so here's my solution. I found AggregatingIterator a better name for that class.

Feel free to comment ;-)



Here's a unit test inspired by one of the answers before.

 
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic