mercredi 19 avril 2017

Shuffling grouped list with ordering such that element of same group does not appear one after another in java

I have a list of Media objects.

Class structure of Media is :
Integer id;
String url;
(@ManyToOne)EventEntity event,..etc

Class structure of EventEntity is :

Integer id;
String name;....etc

There can be multiple Media objects of same EventEntity. How can we randomly sort Media list such that Media object of same EventEntity does not appear one after another.

For example, My desired result is:

[
{
"abc",event1
},
{
"sdf",event2
},
{
"sdf34",event1
},
{
"sdsdafsdf",event3
},
{
"sdf345f34",event2
}
]

Also I can create another class->Demo.class:

Integer id;
String url;
Integer eventId;

And populate the List<Demo> and randomize. The output should be :

[
    {
    5,"abc",1
    },
    {
    8,"sdf",2
    },
    {
    1,"sdf34",1
    },
    {
    2,"sdsdafsdf",3
    },
    {
    9,"sdf345f34",2
    }
    ]

Please suggest with some description.Java 8 is preferable, as well as older version whichever is more efficient.




Aucun commentaire:

Enregistrer un commentaire