Skip to main content
edited body
Source Link
Sleiman Jneidi
  • 3.3k
  • 12
  • 20

It looks very good for me, you can use the extend keyword on generics to insure type safety and to avoid unnecessary casts. And this is called a Bounded type

class StackMinimum<T extends Comparable>{
      public void push(T item) {
       stack1.push(item);
       // to need for the cast here
       if (stack2.isEmpty() || item.compareTo(stack2.peek()) < 0) {
           stack2.push(item);
       }
    }
}

Only Comparable type wouldtypes will be welcomed in your stack

It looks very good for me, you can use the extend keyword on generics to insure type safety and to avoid unnecessary casts. And this is called a Bounded type

class StackMinimum<T extends Comparable>{
      public void push(T item) {
       stack1.push(item);
       // to need for the cast here
       if (stack2.isEmpty() || item.compareTo(stack2.peek()) < 0) {
           stack2.push(item);
       }
    }
}

Only Comparable type would be welcomed in your stack

It looks very good for me, you can use the extend keyword on generics to insure type safety and to avoid unnecessary casts. And this is called a Bounded type

class StackMinimum<T extends Comparable>{
      public void push(T item) {
       stack1.push(item);
       // to need for the cast here
       if (stack2.isEmpty() || item.compareTo(stack2.peek()) < 0) {
           stack2.push(item);
       }
    }
}

Only Comparable types will be welcomed in your stack

Source Link
Sleiman Jneidi
  • 3.3k
  • 12
  • 20

It looks very good for me, you can use the extend keyword on generics to insure type safety and to avoid unnecessary casts. And this is called a Bounded type

class StackMinimum<T extends Comparable>{
      public void push(T item) {
       stack1.push(item);
       // to need for the cast here
       if (stack2.isEmpty() || item.compareTo(stack2.peek()) < 0) {
           stack2.push(item);
       }
    }
}

Only Comparable type would be welcomed in your stack