title: How to replace Integer objects with primitives
body: <p>I am making  a datastucture where I can:</p>
  
  <ul>
  <li>add pair with start and end positions</li>
  <li>can get list of pairs test</li>
  <li>interval for having gaps</li>
  </ul>
  
  <p>E.g. add (0,2),(0,1),(3,4),(6,8)
  it will return (0,4),(6,8)
  check(2,4) - true
  check(3,8) - false, as there is a gap between 4 and 6.  </p>
  
  <p>How can I modify the code below, such as set <code>list</code> and list <code>data</code> will contain primitive <code>int</code> instead of object <code>Integer</code>?  </p>
  
  <p><strong>EDIT:</strong> I am aware that java generics cannot be an primitives, so List is impossible. My motivation comes from implementation of other data stuctures.<br>
  e.g HashMap&lt; Integer, Object> is basicly same thing as android SparseArray with diffenrece that SparseArray ussed ints instead of Integers for keys.</p>
  
  <pre><code>public class GapAwareList {
    Set&lt;Integer&gt; list = new HashSet&lt;&gt;();
  
    public void put(int start, int end) {
      for (int i = start; i &lt;= end; i++) {
        list.add(i);
      }
    }
  
    public void remove(int start, int end) {
      for (int i = start; i &lt;= end; i++) {
        list.remove(i);
      }
    }
  
    public List&lt;Pair&lt;Integer, Integer&gt;&gt; getPairs() {
      List&lt;Integer&gt; data = new ArrayList&lt;&gt;();
      data.addAll(list);
      Collections.sort(data);
      List&lt;Pair&lt;Integer, Integer&gt;&gt; pairs = new ArrayList&lt;&gt;();
      int last = data.get(data.size() - 1);
      int s = -1;
      int e;
      for (int i = 0; i &lt;= last; i++) {
        if (list.contains(i)) {
          if (s == -1) {
            s = i;
          }
          e = i;
          if (!list.contains(i + 1)) {
            Pair&lt;Integer, Integer&gt; p = new Pair&lt;&gt;(s, e);
            pairs.add(p);
            s = -1;
          }
        }
      }
      return pairs;
    }
  
    public boolean haveGap(int start, int end) {
      boolean b = false;
      for (int i = start; i &lt; end; i++) {
        if (!list.contains(i)) {
          b = true;
          break;
        }
      }
      return b;
    }
  }
  public class Pair {
    public final int first;
    public final int second;
  
    public Pair(int first, int second) {
      this.first = first;
      this.second = second;
    }
  
  }
  public class ExampleUnitTest {
  
    @Test public void gapSize() throws Exception {
      GapAwareList gaps = fillGaps();
  
      gaps.put(0, 2);
      gaps.put(0, 1);
      gaps.put(3, 4);
      gaps.put(6, 8);
      List&lt;Pair&lt;Integer, Integer&gt;&gt; pairs = gaps.getPairs();
  
      assertEquals(3, pairs.size());
    }
  
    @Test public void gapFirst() throws Exception {
      GapAwareList gaps = fillGaps();
  
      gaps.put(0, 2);
      gaps.put(0, 1);
      gaps.put(3, 4);
      gaps.put(6, 8);
      List&lt;Pair&lt;Integer, Integer&gt;&gt; pairs = gaps.getPairs();
  
      assertTrue(pairs.get(0).first == 0);
      assertTrue(pairs.get(0).second == 4);
    }
  
    @Test public void gapSecond() throws Exception {
      GapAwareList gaps = fillGaps();
      List&lt;Pair&lt;Integer, Integer&gt;&gt; pairs = gaps.getPairs();
  
      assertTrue(pairs.get(1).first == 6);
      assertTrue(pairs.get(1).second == 8);
    }
  
    private GapAwareList fillGaps() {
      GapAwareList gaps = new GapAwareList();
      //add (0,2),(0,1),(3,4),(6,8)
      gaps.put(0, 2);
      gaps.put(3, 4);
      gaps.put(0, 1);
      gaps.put(6, 8);
      gaps.put(13,11);
      gaps.put(13, 13);
      gaps.put(13, 14);
  
      return gaps;
    }
    @Test public void checkGaps(){
      GapAwareList gaps = fillGaps();
      assertFalse(gaps.haveGap(0,4));
      assertFalse(gaps.haveGap(6, 8));
      assertFalse(gaps.haveGap(6, 7));
      assertTrue(gaps.haveGap(4, 6));
    }
  }
  </code></pre>