How to Shuffle an Arraylist

Shuffle ArrayList of objects

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

Shuffling an ArrayList

Collections.shuffle(temp); is what you need

http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#shuffle%28java.util.List%29

What you may want to do is after creating the ArrayList, run a for-loop to add 52 values:

for(int i = 0; i < 52; i++){ 
temp.add(i);
}

Then do this:

Collections.shuffle(temp);

Print out the result to confirm

Shuffle ArrayList

First you are passing list object to setAdapterForRecyclerView(collections);

After that you are passing same list object to setAdapterForRecyclerViewBestCollections(shuffleCollection(collections));

And then shuffling the object (in both the methods you are using same object and shuffle will reflects to both RecyclerView1 and RecyclerView2

Create new List object and return that after shuffling, so that you will see two different order in RecyclerView1 and RecyclerView2

public List<CollectionsModel> shuffleCollection(List<CollectionsModel> collectionsModelList) {
List<CollectionsModel> shuff = new ArrayList<>(collectionsModelList);
java.util.Collections.shuffle(shuff);
return shuff;
}

How to use Collections.shuffle() to shuffle an ArrayList in SSJS?

Call the method using the complete package name:

java.util.Collections.shuffle(numbersArrayList); 

How can I shuffle a specific range of an ArrayList?

Use List.subList and Collections.shuffle, like this:

Collections.shuffle(list.subList(start, end));

(Note that the second index to subList exclusive, so use end+1 if you want to include end index in the shuffle.)

Since List.subList returns a view of the list, changes made (by the shuffle method) to the sub list, will also affect the original list.

how to Manually shuffle the array list

Here I found Solution to my question please observer here.
"itemList_dump" is an arraylist which contains data.

if (i < itemList_dump.size()) {

MyBitamp Data = itemList_dump.get(original_position_val);
// setting Values according to our algorithm
Data.setItemCurPos(original_position_val);
Data.setItemPosition(current_positon_val);
Data.setrotation(rotataion_val);

}
}
Collections.sort(itemList_dump, new Comparator<MyBitamp>() {

@Override
public int compare(MyBitamp o1, MyBitamp o2) {
if (o1.getItemPosition() > o2.getItemPosition()) {
return 1;
} else if (o1.getItemPosition() < o2.getItemPosition()) {
return -1;
} else {
return 0;
}
}
});

Shuffling An Array List

Like Faro and duffymo already stated your question is not very clear -- or at least we do not understand it very well. I think the problem here may be that you're using an inappropriate design for your domain. In particular, why do you need to pass anything into the shuffle method on the Deck class? Shouldn't it be able to shuffle its own internal cards?

In my mind a design like the following makes more sense:

class Card {
private int value;
private String suit; // Or an enum, whatever you prefer

... constructor/getters/setters ...
}

class Deck {
private ArrayList<Card> cards;

... constructor to initialize cards ...

public void shuffle() {
Collections.shuffle(cards);
}
}

public class MainGame {
private Deck deckOne;

public MainGame() {
deckOne = new Deck();
deckOne.shuffle();
}
}


Related Topics



Leave a reply



Submit