1

I have two lists with items (List 1 and List 2). I'll create a third one, here called list_mix, with 10 items, and it will receive:

  • The 6 first positions or 60% of list1 and
  • The 4 first positions or 40% of list2.

That new list (list_mix) should receive the items from the lists 1 and 2 randomly.

Here is an example: public class JoinLists {

    public static void main(String[] args) {

        List<String> list1 = new ArrayList<String>();
        list1.add("dog","cat","rabbit","bat","zebra","bear","turtle","mouse","fly","bird");

        List<String> list2 = new ArrayList<String>();
        list2.add("bee","horse","mule","cow","fish","shark","lion","tiger","elephant","panther");

    List<String> list_mix = new ArrayList<String>();
        list_mix.addAll(list1);
        list_mix.addAll(list2);

    System.out.println("list1 : " + list1);
    System.out.println("list2 : " + list2);
    System.out.println("list_mix : " + list_mix);

}

What I want is the following result in list_mix: Output

list1 : [dog,cat,rabbit,bat,zebra,bear,turtle,mouse,fly,bird]
list2 : [bee,horse,mule,cow,fish,shark,lion,tiger,elephant,panther]
list_mix : [dog,bee,cat,horse,rabbit,mule,bat,cow,zebra,bear] //A list with 10 items, using 60% of list 1 and 40% of list 2

If I use ".addAll" I'll get all the items, but I want only the first ones of the two lists. How can I get this?

3 Answers 3

1

You could use the subList method :

list_mix.addAll(list1.subList(0, 6));
list_mix.addAll(list2.subList(0, 4));

Then to randomize the order of the list use Collections.shuffle(list_mix);

Sign up to request clarification or add additional context in comments.

Comments

0

You can create a method that will helps to extract part of a list for a given percentage.

See Java sublist docs

Once you have your sublist you should use Collections.shuffle to randomize results.

Also I updated the way you initialized array strings in your code, here is updated code.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class RandomList {

private static List<String> getSubList(List<String> list, int percentage) {
    return list.subList(0, (percentage / 10));
}

public static void main(String[] args) {

    String[] array1 = { "dog", "cat", "rabbit", "bat", "zebra", "bear",
            "turtle", "mouse", "fly", "bird" };

    List<String> list1 = new ArrayList<String>();
    list1.addAll(Arrays.asList(array1));

    String[] array2 = { "bee", "horse", "mule", "cow", "fish", "shark",
            "lion", "tiger", "elephant", "panther" };

    List<String> list2 = new ArrayList<String>();
    list2.addAll(Arrays.asList(array2));

    List<String> list_mix = new ArrayList<String>();
    list_mix.addAll(getSubList(list1, 60));
    list_mix.addAll(getSubList(list2, 40));

    Collections.shuffle(list_mix);

    System.out.println("list1 : " + list1);


    System.out.println("list2 : " + list2);
        System.out.println("list_mix : " + list_mix);

    }
}

Output

list1 : [dog, cat, rabbit, bat, zebra, bear, turtle, mouse, fly, bird] 
list2 : [bee, horse, mule, cow, fish, shark, lion, tiger, elephant, panther] 
list_mix : [cat, cow, zebra, rabbit, mule, dog, bear, bat, horse, bee]

Hope this helps.

Cheers !!

1 Comment

Thanks Sacnhin, of course it helped :)I really appreciate your care with the code, it was the best answer
0

Here's a method that could clear things up for you:

public List<String> getSubList(List<String> in, int magnitude) {
    List<String> back;
    if (in.size() > 10) { // if > 10, (magnitude*10)% can be more than magnitude
        //use percentage
        back = new ArrayList<>(in); //prevent modifying outside contents
        Collections.shuffle(back); //randomize, use ThreadLocalRandom instead if working with big collections
        magnitude = (in.size() * (magnitude * 10)) / 100; //set our new magnitude
    } else {
        back = in; //just use inserted elements if not using percentage
    }
    return back.subList(0, magnitude); //return our sublist
}

Then it's as simple as:

List<String> mixed = new ArrayList<>();
mixed.addAll(getSubList(list1, 6));
mixed.addAll(getSubList(list2, 4));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.