-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??

sumit
  • 1,047
  • 1
  • 10
  • 15
  • 5
    What is `dataList`? It can't be a java.util.ArrayList, since it has no add method that takes 3 arguments. – Eran Jun 09 '17 at 05:59
  • 2
    Encapsulate each of these three items together in a container. Then shuffle the list of these containers. – SHG Jun 09 '17 at 05: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. – Florian Schaetz Jun 09 '17 at 06:00
  • 2
    *Read carefully before marking duplicate or downvoting.* same result – Scary Wombat Jun 09 '17 at 06:00
  • @SHG I didn't get your approach of Encapsulating items in a container and the shuffle. Can you please explain further?? – sumit Jun 09 '17 at 06:05
  • read the above comments – Scary Wombat Jun 09 '17 at 06:07
  • what about above comments @ScaryWombat – sumit Jun 09 '17 at 06:08
  • They explain why your question is getting downvoted - the code you have supplied would not even compile. – Scary Wombat Jun 09 '17 at 06:10
  • Create another class having three fields `ItemNo., Price, Discount` Then create a list of the object of that class. Then use `Collections.shuffle()` method on that list. Retrieve objects from that list. For each object retrieve other data from it. – Sanket Makani Jun 09 '17 at 06:10
  • @ScaryWombat I have provided the necessary code required to answer the question and my code is compiling fine and I also shared the results I was getting – sumit Jun 09 '17 at 06:18
  • Please show a link to the javadocs where `add` with 3 parameters `dataList.add("Item1","Price1","Discount1");` would work – Scary Wombat Jun 09 '17 at 06:28
  • @ScaryWombat have you ever heard of CustomList of type class. Don't just blabber anything. See the answer below for List of type Product class List products = new ArrayList<>(); Read yourself before commenting. – sumit Jun 09 '17 at 06:30
  • Yes, the answer below gives `products.add(new Product("Watch", 45.99, 9.99));` see how it is instantiating a new `Product` before trying to add it as a single parameter. Your code is trying to add 3 `String` paramaters - BTW, you seem to be a very angry young man. – Scary Wombat Jun 09 '17 at 06:40
  • Sorry man @ScaryWombat I was really frustrated why people were downvoting my question. I am not a newbie I was adding the data same way by creating new object everytime of my data class. Here I provided the code which I thought would be necessary for people to provide a solution to my problem. I didn't thought they'll downvote if I don't write each and everyLine of my code. – sumit Jun 09 '17 at 06:44
  • 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. – Scary Wombat Jun 09 '17 at 06:48
  • @sumit This comment is not meant for this question, its just to clarify the downvote you have done for one of my post, First know that `postDelayed` function of handler class is not static, which cannot be reference by `Handler.postDelayed(..)` instead you have to create an object of Handler class and call the method `new Handler().postDelayed(..`, learn some basics, and then downvote. – Sanoop Surendran Jun 21 '17 at 09:41
  • @sumit hope this makes sense to you now. – Sanoop Surendran Jun 21 '17 at 09:43
  • @Sanoop sorry I missed that I was about to correct myself till then he deleted the post. That was just a silly mistake on my side. – sumit Jun 21 '17 at 09:44
  • @sumit No issues :) :) – Sanoop Surendran Jun 21 '17 at 09:46

2 Answers2

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);
    }
}
silentsudo
  • 6,730
  • 6
  • 39
  • 81
  • Thanks a lot @sector11 problem with my code was I was using ArrayList instead of LinkedList. – sumit Jun 09 '17 at 06:15
  • Don't know why they have downvoted when I provided all the necessary information that was required to answer the question. – sumit Jun 09 '17 at 06:16
  • @sumit update for ArrayList and yes they have rights to downvote your question is NOT AT ALL CLEAR!! – silentsudo Jun 09 '17 at 06:17
  • they have rights I know but in what Aspect is my question not clear to understand – sumit Jun 09 '17 at 06:19
  • You were not clear on your approach, anyways lets agree that you have a solution back to work!!! – silentsudo Jun 09 '17 at 06:21
-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());
Raj
  • 477
  • 5
  • 20