Home > OS >  How can I set the count of an item in a list<T> to a specific amount
How can I set the count of an item in a list<T> to a specific amount

Time:10-27

I have a class SimpleHistogram<DT> that takes in a generic array DT[] and I'm supposed to set the count, the number of times the element occurs in the array, of a specific item in the array to int count.

Here is what I have so far:

public class SimpleHistogram<DT> implements Histogram<DT>, Iterable<DT> {
    DT[] items;
    int size;

    public SimpleHistogram() {
    }

    public SimpleHistogram(DT[] items) {

        this.items = items;
    }
    @Override
    public void setCount(DT item, int count) {
        int n = 0;
        Iterator<DT> L = this.iterator();
        while (L.hasNext()) {
            DT dt = L.next();
            if (dt == item) {  // if dt equals to item, meaning item IS present, then
                n =count;      // set the count of the item to count

            } else
            {this.add(dt, count)} // if its not equal, meaning its not there, then add the item and the count of the item
        }


    }
    private class Iterate implements Iterator<DT> {
        int index = 0;
        boolean lastRemoved = false;

        @Override
        public boolean hasNext() {
            return (index < items.length-1);
        }

        @Override
        public DT next() {
            if (index < (items.length) -1)
                throw new NoSuchElementException("No element at index");
            DT object = items[index];
            index  ;
            lastRemoved = false;
            return object;
        }
    }

I'm struggling to implement the function setCount( DT item, int count) which is supposed to set the count of item to count.

Aditionally, if item does not exist already in the list, then we are supposed to add the item in and then set the count of the item to count.

I have provided explanations for what I intended to do but due to the fact that I am new to this, I haven't found sources that can properly clear this doubt, so any help would be greatly appreciated

Edit Here is the full code in case you may want to derive something from it. Test cases also presented below.

package histogram;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;

// TODO: Uncomment this and make sure to implement all the methods

    public class SimpleHistogram<DT> implements Histogram<DT>, Iterable<DT> {
        DT[] items;
        int size;
    
        public SimpleHistogram() {
        }
    
    public SimpleHistogram(DT[] items) {

        this.items = items;
    }
    @Override
    public void setCount(DT item, int count) {
        int n = 0;
        Iterator<DT> L = this.iterator();
        while (L.hasNext()) {
            DT dt = L.next();
            if (dt == item) {  // if dt equals to item, meaning item IS present, then
                n =count;      // set the count of the item to count

            } else
            {this.add(dt, count)} // if its not equal, meaning its not there, then add the item and the count of the item
        }


    }
    private class Iterate implements Iterator<DT> {
        int index = 0;
        boolean lastRemoved = false;

        @Override
        public boolean hasNext() {
            return (index < items.length-1);
        }

        @Override
        public DT next() {
            if (index < (items.length) -1)
                throw new NoSuchElementException("No element at index");
            DT object = items[index];
            index  ;
            lastRemoved = false;
            return object;
        }
    }

    public int getCount(DT item) {
        int n = 0;
        Iterator<DT> L = this.iterator();
        while (L.hasNext()) {
            DT dt = L.next();
            if (dt == item) {
                n  ;
            }

        }
        return n;
    }



    @Override
    public Iterator<DT> iterator() {

        return new Iterate();
    }

    @Override
    public int getTotalCount() {
        return items.length;
    }




}

Test cases:

public class SimpleHistogramTest {

    @Test
    public void testHistogram() {
        Character[] target = {'a','b','c','a'};
        Histogram<Character> h = new SimpleHistogram<>(target);
        Iterator<Character> iter = h.iterator();
        int elemCount = 0;
        while(iter.hasNext()) {
            iter.next();
            elemCount  ;
        }

        assertEquals(3, elemCount);
        assertEquals(2, h.getCount('a'));
        assertEquals(1, h.getCount('b'));
        assertEquals(1, h.getCount('c'));
        assertEquals(4, h.getTotalCount());
    }
}

CodePudding user response:

Would something like this satisfy the requirements? It's essentially a wrapper around a map, where the elements are the keys, and each value is a count of that element. Presumably, the Histogram interface has additional operations like getCount(T); here, you'd just get the count from the encapsulated map.

class SimpleHistogram<T> implements Histogram<T>, Iterable<T> {

    private final Map<T, Integer> bins;

    SimpleHistogram() {
        this(List.of());
    }
    
    SimpleHistogram(List<? extends T> items) {
        Collector<T, ?, Integer> intCounting = Collectors.collectingAndThen(Collectors.counting(), Math::toIntExact);
        bins = items.stream().collect(Collectors.groupingBy(Function.identity(), intCounting));
    }

    @Override
    public void setCount(T item, int count) {
        bins.put(item, count);
    }

    @Override
    public Iterator<T> iterator() {
        return bins.keySet().iterator();
    }

}

CodePudding user response:

== compares the reference of an object in Java. equals() is also same.

If you want to compare two objects(dt and item) wit the same value, you need to override hashCode() and equals().

After that, you use equals() rather than ==.

Here is reference link.

Compare two objects with .equals() and == operator

  • Related