public class test6 { public static void main (String [] args) { Mapmap = new HashMap (); map.put ("a", null); map.put ("b", "Ma"); map.put ("c", "Ma"); map.put ("1", "Ma"); map.put ("2", "Ma"); map.put ("3", "Ma"); map.put ("4", "Ma");
Set i = map.keySet (); / / Set the collection should be [3, 2, 1, b, c, 4, a]
System.out.println (i);
int len = i.size (); / / i is 7
/ / Get the length of the random collection of set the length of the table values
System.out.println (len); Random random = new Random (); int key = random.nextInt (len); / / key is 0-7 random number between arbitrary
? ? ? ? ? ? ? ? ?
How to get a random collection set out under the subject of values that
Obtained by the index value to get the random "a" or "b" ? ? ? ? ? ? ? ? ? ?
}
------ Solution ------------------------------------ --------
Map map = new HashMap (); map.put ("a", null); map.put ("b", "Ma"); map.put ("c", "Zhang"); map.put ("1", "Mr. Lee"); map.put ("2", "Yu"); map.put ("3", "P sir"); map.put ("4", "SB sir");
Setset = map.entrySet (); Object [] entryArray = set.toArray () ;/ / Can anyone tell me this step directly into Map.Entry [] array, forced (Map.Entry []) set. toArray () error
for (int i = 0; i
Random random = new Random (); int key = random.nextInt (set.size ()); Map.Entry entry = (Map.Entry) entryArray [key]; System.out.println (entry.getKey () + ":" + Entry.getValue ());
Even if the random number key replaced by a constant such as 3, if you follow it into the case, lead to changes in outcomes.
So you can be replaced LinkedHashMap, so we can guarantee the future of plug-in inserted at the end. (LinkedHashMap elements when you walk into the sequence returns just right)
------ For reference only ------------------------ ---------------
convenient set can use the following method
Iterator
while (ite.hasNext ()) {
System.out.println (ite.next ());
}
set are not ordered so no way to use the subscript Get
------ For reference only ------------------ ---------------------
Set Since there is no order, you want to have the order, then you just go to the List which was good.
List list = new ArrayList (i);
------ For reference only ---------------------------------- -----
But I was to get inside the Map map key collection
can only set
But I want to get a random map key value
how can I do
------ For reference only ------------------------------ ---------
How do I get keySet random value inside it?
------ For reference only -------------------------------------- -
keySet get to set the value to a List inside lining, and then you randomly to the index value to obtain the list in the value of
------ For reference only ---------------------------------------
Set inside the elements are unordered collections , this is no way to get.
landlord yes yes LinkedHashMap, this key-value pairs which will put the order in accordance with sorting,
However, you want to get the sorted set of keys, had to put that into Set List can.
List elements inside the collection is ordered.
------ For reference only -------------------------------------- -
Forced (Map.Entry []) set.toArray () error Map.Entry []) implementation is
------ For reference only -------------------- -------------------
if you do not consider the application scenario, there can be linkedhashmap
没有评论:
发表评论