Kei
Kei

Reputation: 315

Java Counting unique values in a queue

int uniqueCount = 0;
System.out.println("List Cities Visited:");

for(int i = 0; i < num; i++)
{
    System.out.println((i+1)+". "+(String)cityTrack.dequeue());

}

System.out.println("Unique Cities: "+uniqueCount);

Basically, I added a list of cities into a queue. And I am to print it out as shown above, and to count the number of unique cities there are. Any ideas? I'm quite new to this.

EDIT: Solved. Thanks guys!

Upvotes: 1

Views: 5987

Answers (4)

wassgren
wassgren

Reputation: 19201

With Java 8 comes the stream API:s which supports the distinct-method. The following code can be used:

Collection<String> cities = new ArrayDeque<>();
cities.add("Detroit");
cities.add("NYC");
cities.add("Boston");
cities.add("Boston");

// Count the number of unique cities
final long uniqueCities = cities.stream().distinct().count();

// Prints all of the cities (including duplicates) and removes them from the 
// queue (i.e. performs a dequeue) which is the same way as you showed in the example
IntStream.rangeClosed(1, cities.size())
        .forEach(i -> System.out.println(i + ". " + cities.remove()));

// Prints the unique cities counter
System.out.println("Unique: " + uniqueCities); // -> Unique: 3

The distinct-method requires that the hashCode-method is properly implemented (which it already is for the String class).

Resources:

Tip, if you need to create your own hash the Objects.hash method works fine. You simple pass the args that should be part of the hashCode.

public int hashCode() {
    return Objects.hash(attr1, attr2, attr3);
}

Also, note that by using the remove-method (which is the same as dequeue in your example) the elements are removed from the queue. If this is not the intention a simple loop with a counter will do just fine like in the example below:

AtomicInteger counter = new AtomicInteger(0);
cities.stream()
        .map(city -> counter.incrementAndGet() + ". " + city)
        .forEach(System.out::println);

Upvotes: 3

Sarz
Sarz

Reputation: 1976

Like you mention in your code. Create a temp queue named e.g. uniqueCityTrack and check for each you have checked and entered.

int uniqueCount = 0;
System.out.println("List Cities Visited:");
CityTrack uniqueCityTrack = new CityTrack();
for(int i = 0; i < num; i++)
{
    System.out.println((i+1)+". "+(String)cityTrack.dequeue());
    while(uniqueCityTrack.hasNext){
        if(!uniqueCityTrack.contain((String)cityTrack.dequeue()){
            uniqueCount++;
        }
    }
uniqueCityTrack.enqueue((String)cityTrack.dequeue());
}

System.out.println("Unique Cities: "+uniqueCount);

Upvotes: 1

Akash Thakare
Akash Thakare

Reputation: 22972

....and to count the number of unique cities there are.....

You can use Set here because it only contains unique elements.

FOR EXAMPLE

    Queue<Integer> queue = new LinkedList<Integer>();
    queue.add(1);
    queue.add(1);
    queue.add(1);
    Set<Integer> set = new HashSet<Integer>();
    set.addAll(queue);
    System.out.println(set.size());

OUTPUT

1

Upvotes: 2

Nicola Ferraro
Nicola Ferraro

Reputation: 4179

Add them to an HashSet and get the size.

Upvotes: 0

Related Questions