如何组合subList和Hashset(ArrayList)?

时间:2016-03-14 02:24:22

标签: java arraylist hashset sublist

我正在制作一个包含3种不同类型组的ArrayList。我想要实现的目标是:

  1. 确保随机化(Collections.shuffle)(完成)
  2. 使用子列表,我可以在ArrayList(Done)中选择特定数量的元素
  3. 为了防止在运行程序时出现在其他组中的重复,我实现了一个HashSet(未完成)
  4. 我似乎无法将#2和#3结合起来。也许我还没有理解HashSets是如何完成的。这是我的代码:

    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
    

    我没有在这里实现我的HashSet,因为它看起来很尴尬,但现在是:

    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);
    

    我想要的输出是这样的:

    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选择特定数组并打印它们,以及HashSet以防止重复。

    请记住,这只是 1周,现在我必须找到一种方法让它重复出现,以便每个国家在5周内至少会见一次-_-帮助/建议/示例我很感激,但解决这个问题是我的主要目标。

2 个答案:

答案 0 :(得分:0)

如果列表中成员的类型只是String,那么您的代码运行良好,我会测试它。但是如果你想避免重复的自定义类,你需要覆盖equals()和hashCode()方法。您应该了解如何确保已正确实现equals和hashCode。这是一个很好的起点:What issues should be considered when overriding equals and hashCode in Java?

答案 1 :(得分: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");

}