Skip to content

Instantly share code, notes, and snippets.

@zEvg
Last active December 10, 2015 21:38
Show Gist options
  • Save zEvg/4496384 to your computer and use it in GitHub Desktop.
Save zEvg/4496384 to your computer and use it in GitHub Desktop.
Example of the Bag implementation behavior
package ;
import java.util.Collection;
public class BagMain {
public static void main(String[] args) {
Collection<Integer> bag = new Bag<Integer>();
bag.add(1);
bag.add(5);
bag.add(7);
bag.add(8);
bag.add(1);
bag.add(12);
bag.add(1);
for (Integer i : bag) {
System.out.print(i + ",");
}
// output: 1,1,1,5,7,8,12,
System.out.println();
for (Integer i : bag) {
System.out.print(i + ",");
}
// output: 1,1,1,5,7,8,12,
}
package ;
import java.util.Collection;
public class LinkedBag {
public static void main(String[] args) {
Collection<Integer> bag = new LinkedBag<Integer>();
bag.add(1);
bag.add(5);
bag.add(7);
bag.add(8);
bag.add(1);
bag.add(12);
bag.add(1);
for (Integer i : bag) {
System.out.print(i + ",");
}
// output: 1,5,7,8,1,12,1,
System.out.println();
for (Integer i : bag) {
System.out.print(i + ",");
}
// output: 1,5,7,8,1,12,1,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment