-7

Read carefully before marking duplicate or downvoting.

My Requirement is to shuffle Arraylist of object keeping the list data same just changing the position. I have an of type Dummydata :

List<DummyData> dataList = new ArrayList<>();

My DummyData class has three String fields.

So adding some data for clarification:

dataList.add(new DummyData("Item1","Price1","Discount1"));
dataList.add(new DummyData("Item2","Price2","Discount2"));
dataList.add(new DummyData("Item3","Price3","Discount3"));
dataList.add(new DummyData("Item4","Price4","Discount4"));
dataList.add(new DummyData("Item5","Price5","Discount5"));

When I use Collections.shuffle(dataList);

the output is :

Item5 Price3 Discount4
Item2 Price1 Discount2
and some random output like this everytime.

My intended output is :

Item2 Price2 Discount2
Item5 Price5 Discount5
Item1 Price1 Discount1

Just the position should change list data should not jumble. so my question is this possible to achieve using Collections.shuffle or not??

19
  • 5
    What is dataList? It can't be a java.util.ArrayList, since it has no add method that takes 3 arguments. Commented Jun 9, 2017 at 5:59
  • 2
    Encapsulate each of these three items together in a container. Then shuffle the list of these containers. Commented Jun 9, 2017 at 5:59
  • 1
    Eran is exactly right, your code cannot work as you described, thus you did not describe your actual problem, thus nobody can help you without using a crystal ball. Commented Jun 9, 2017 at 6:00
  • 2
    Read carefully before marking duplicate or downvoting. same result Commented Jun 9, 2017 at 6:00
  • 1
    Well to be honest starting your question off with Read carefully before marking duplicate or downvoting. is going to annoy a lot of people if you don't post carefully. The first comment highlighted the problem with your code and you seemed to ignore it and went on the offensive. Nothing more to say. Commented Jun 9, 2017 at 6:48

2 Answers 2

5

Hi Sumit i dont know why there are few downvote it is because you have not informed that you are new to Java Language Please find my answer below

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

final class Product {
    private final String name;
    private final Double price;
    private final Double discount;

    Product(String name, Double price, Double discount) {
        this.name = name;
        this.price = price;
        this.discount = discount;
    }

    @Override
    public String toString() {
        return "{name=" + name + ", price=" + price + ", discount=" + discount + "}";
    }
}

public final class Main {

    public static void main(String[] args) {
        List<Product> products = new ArrayList<>();
        products.add(new Product("Watch", 45.99, 9.99));
        products.add(new Product("Keyboard", 21.99, 3.99));
        products.add(new Product("Mouse", 99.99, 4.99));

        System.out.println("Original items Before shuffle are");
        products.forEach(System.out::println);

        Collections.shuffle(products);
        System.out.println("Original items After shuffle are");
        products.forEach(System.out::println);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot @sector11 problem with my code was I was using ArrayList instead of LinkedList.
Don't know why they have downvoted when I provided all the necessary information that was required to answer the question.
@sumit update for ArrayList and yes they have rights to downvote your question is NOT AT ALL CLEAR!!
they have rights I know but in what Aspect is my question not clear to understand
You were not clear on your approach, anyways lets agree that you have a solution back to work!!!
-1

Try this

Collections.shuffle(dataList);
Log.d(TAG,dataList.toString());

Working use your logic

   Collections.shuffle(dataList);
    ArrayList<DummyData> l1=new ArrayList<>();
    for(DummyData e : dataList){
        l1.add(e);
    }
    Log.d(TAG,l1.toString());

1 Comment

do you know the output of l1.toString()?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.