2

I'm implementing a bag of Integers in java and I'm not sure how to do so. I would like to do so with either a HashMap, LinkedHashMap, TreeMap, TreeSet, or HashSet. Some of the things I'd like to do are

  1. Be able to count the number of occurrences of a certain element (So I cannot use a set)
  2. be able to add without the structure immediately deleting duplicate integers

I've tried implementing a map so far but I run into problems when I try to add to the map because I'm trying to implement a bag of integer objects not key value pairs.

public class Bag<Integer> {
private int count = 0;
private HashMap <T, Integer> map; 

//class constructor 
public Bag(){ 
  this.map = new HashMap <T, Integer>(); 
}

would a linked hash set be best? I'd like to add duplicate Integers.

emmynaki
  • 157
  • 2
  • 10

3 Answers3

1

If I read your question correctly, you simply want

Map<Integer, Integer> integerBag = new HashMap<>();

Key: represents the different Integers you have in your bag.

Value: represents the count how often the corresponding key was added.

When adding a "new" Integer, you put(newValue, 1) into the map. When the same number comes in, you increase that counter; and decrease on removal.

Beyond that:

without the structure immediately deleting duplicate integers doesn't make much sense. Integers are just numbers; why would you want to remember "6 6 6" ... when you could remember "I got 6 three times" instead?!

Given your comments:

  • you don't need to change the signature of your method. The compiler generates code to turn primitive types such as int into their big brothers such as Integer automatically. That is called auto-boxing.
  • but you can also do that manually.

See here:

int intval =5;
Integer asInteger  = Integer.valueOf(intval);
if (Integer bag.contains(asInteger)) { 
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • So I could also write that as Map integerBad = new HashMap<>(); ? how would i then 1. retrieve count and 2. add to my bag if my signature looks like public void add (int Element)? should i replace int with Integer instead? – emmynaki Apr 18 '17 at 19:58
0

Just use a HashMap. You might want to count how many duplicates you have:

Map<Whatever, Long> bag = new HashMap<>();

To add an element, use the merge method:

bag.merge(someElement, 1, (oldValue, value) -> oldValue + 1);

And to remove an element, you might want to use the computeIfPresent method:

bag.computeIfPresent(someElement, (key, value) -> (value == 1 ? null : value - 1));
fps
  • 33,623
  • 8
  • 55
  • 110
0

Because of your requirement #2, I don't think you can use any collection based on hashing. If you need to retain duplicate Integers, you'll need to use a List.

Adding a new items is easy, just call add() on the list. Finding all items requires a short loop; to count them just call size() on the resulting list.

The code below is not tested.

public class Bag<T> {
   private List<T> items = new ArrayList<>(); 

   public void add( T i ) { items.add(i); }
   public List<T> findAll( T target ) {
      ArrayList<T> retVal = new ArrayList<>();
      for( T i : items ) {
         if( i.equals(target) )
            retVal.add( i );
      }
      return retVal;
   }
}
markspace
  • 10,621
  • 3
  • 25
  • 39