Tuesday, 15 February 2011

java - Join two collections and get a random element -



java - Join two collections and get a random element -

i need bring together 2 collection<string>, n random elements , remove them original collection in stored.

to bring together collections thought iterate them , store in custom map construction in way to:

have same key stored n times get original collection.

is there simple method that?

can help me?

how this:

collection<string> collection1 = new arraylist<string>(); collection<string> collection2 = new arraylist<string>(); list<string> allelements = new arraylist<string>(collection1); allelements.addall(collection2); collections.shuffle(allelements); random random = new random(); int n = 10; list<string> randomresults = new arraylist<string>(n); (int = 0; < n && !allelements.isempty(); i++) { string randomelement = allelements.remove(random.nextint(allelements.size())); randomresults.add(randomelement); } collection1.removeall(randomresults); collection2.removeall(randomresults);

java

No comments:

Post a Comment