0

I am making an ArrayList with 3 different types of groups. The objective I am trying to achieve is:

  1. It ensures randomization (Collections.shuffle) (Done)
  2. Using a sublist, I can pick a specific number of elements in the ArrayList (Done)
  3. To prevent duplication appearing in the other groups when running the program, I implement a HashSet (Not done)

I can't seem to combine #2 and #3. Maybe I'm just not understanding how HashSets are done yet. Here is my code:

class code {
    public static void main(String[] args) {

    //This is creating the ArrayList with everything added

    List one = new ArrayList();
    List two = new ArrayList();
    List three = new ArrayList();

    one.add("United States");
    one.add("United Kingdom");
    one.add("Italy");
    one.add("France");
    one.add("Russia");
    one.add("Japan");
    one.add("China");
    one.add("Mexico");

    two.add("Philippines");
    two.add("Austria");
    two.add("Canada");
    two.add("Sweden");
    two.add("Iceland");
    two.add("India");
    two.add("Australia");
    two.add("Deutschland");

    three.add("Norway");
    three.add("Kosovo");
    three.add("North Korea");
    three.add("Sudan");
    three.add("United Arab Emirates");
    three.add("Bahrain");
    three.add("Haiti");

    //Formatting purposes

    System.out.println("Country Categories: ");
    System.out.println("#1 Countries = " + one);
    System.out.println("#2 Countries = " + two);
    System.out.println("#3 Countries = " + three);
    System.out.println("\n");

    //Randomization code. I use this method so it's shuffled the same way.

    long seed = System.nanoTime();
    Collections.shuffle(one, new Random(seed));
    Collections.shuffle(two, new Random(seed));
    Collections.shuffle(three, new Random(seed));

    System.out.println("Group #1");
    List largelist = one.subList(1,4);
    System.out.println(largelist);
    List mediumlist = two.subList(1,4);
    System.out.println(mediumlist);
    List smalllist = three.subList(1,3);
    System.out.println(smalllist);

    /*System.out.println("Group #2");
    List largerlist = one.subList(1,4);
    System.out.println(largerlist);
    List mediumrlist = two.subList(1,4);
    System.out.println(mediumrlist);
    List smallrlist = three.subList(1,3);
    System.out.println(smallrlist);

    System.out.println("Group #3");
    List largerrlist = one.subList(1,3);
    System.out.println(largerrlist);
    List mediumrrlist = two.subList(1,3);
    System.out.println(mediumrrlist);
    List smallrrlist = three.subList(1,3);
    System.out.println(smallrrlist);*/
    }
} //I only put one group because of issue I am having

I didn't implement my HashSet in this because it looked embarrassing but here it is:

System.out.println("Group #1");
Set largeset = new HashSet(one);
for (Object wow : one) {
  List largelist = one.subList(1,4);
  System.out.println(largelist);
  System.out.println(wow);

My intended output I wanted is something like this:

Group #1 //Week 1
[United Kingdom, Italy, Russia]
[Philippines, Canada, Iceland]
[Bahrain, Sudan]

Group #2
[France, United States, Japan]
[Sweden, India, Austria]
[Kosovo, North Korea]

Group #3
[Mexico, China]
[Australia, Deutschland]
[United Arab Emirates, Norway]

Randomization to Randomize, SubList to choose specific arrays and print them, and a HashSet to prevent duplication.

Keep in mind this is just for 1 week, now I must find a way for it to recur so that every country meets up at least once in 5 weeks -_- Help/Advice/Examples are appreciated, but figuring this out is my main goal.

2 Answers2

0

If the type of the members in the list is just String, your code works well and I test it. But if you want to avoid duplicate self-defined class, you will need to override the equals() and hashCode() method. You should read up on how to ensure that you've implemented equals and hashCode properly. This is a good starting point: What issues should be considered when overriding equals and hashCode in Java?

Community
  • 1
  • 1
Adam Lyu
  • 431
  • 1
  • 5
  • 17
0
private static Set allCountries = new HashSet();

private static void distinctAdd(String country, List targetList){
    if(!allCountries.contains(country)){
        allCountries.add(country);
        targetList.add(country);
    }else{
        //don't add duplicates!
    }
}

public static void main(String args[]) {

    List one = new ArrayList();
    List two = new ArrayList();
    List three = new ArrayList();

    distinctAdd("United States", one);
    distinctAdd("United Kingdom", one);
    distinctAdd("Italy", one);
    distinctAdd("France", one);
    distinctAdd("Russia", one);
    distinctAdd("Japan", one);
    distinctAdd("China", one);
    distinctAdd("Mexico", one);

    distinctAdd("Philippines", two);
    distinctAdd("Austria", two);
    distinctAdd("Canada", two);
    distinctAdd("Sweden", two);
    distinctAdd("Iceland", two);
    distinctAdd("India", two);
    distinctAdd("Australia", two);
    distinctAdd("Deutschland", two);
    distinctAdd("United States", two);

    distinctAdd("Norway", three);
    distinctAdd("Kosovo", three);
    distinctAdd("North Korea", three);
    distinctAdd("Sudan", three);
    distinctAdd("United Arab Emirates", three);
    distinctAdd("Bahrain", three);
    distinctAdd("Haiti", three);

    System.out.println("Country Categories: ");
    System.out.println("#1 Countries = " + one);
    System.out.println("#2 Countries = " + two);
    System.out.println("#3 Countries = " + three);
    System.out.println("\n");

}
W. Lee
  • 105
  • 5
  • I understand how it works... but it doesn't print out a group like I wanted it to. I've spent a whole hour messing with the method you've provided to me to add a subList but no luck, so back to my original question. – GalaticFragility Mar 14 '16 at 06:51
  • @GalcticFragility Sorry about that! I just edited my code snippet and added "allCountries.add(...)" to line 5. That adds the given country to the HashSet, which represents the universe of all countries that have been added to any of your lists. I also added a line in the middle where I tried to add "United States" to List "two", even though it was already added to List "one." Because there's a check in the helper method to see if that country has already been added to a previous list, it ends up not getting added to List "two" (as observed in the print statements at the end). – W. Lee Mar 14 '16 at 11:56
  • That makes a lot of sense. I've edited the 3 System.out at the bottom & changed them to a subList (Refer to the comment I made above in my main code at the bottom w/ the 3 groups) like I intended it to be. It kinda achieves #2 on my objective list (Now how do I put 2 groups?, maybe adding a for or another method?) but since I've also found it hard to implement randomization as the hash creates a wall that prevents it, #1 gets thrown out the window and #3 sorta achieves what I wanted (Thx for that) but I just want 3 groups that does my intended output. Go ahead and run my main code 2 see whtimn – GalaticFragility Mar 14 '16 at 19:20
  • Just to recap, please implement randomization in there. instead of printing out the whole list, please change to sublist like the one in my main code. And please help give me something to print out the other groups. All I wanted is 3 groups that are constantly randomized so when it runs different outcomes appear rather than in an ordered list. It does not dupe, it's my intended output. Please run my main code w/out the comments to see what I mean – GalaticFragility Mar 14 '16 at 19:53