第一种:
Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); Object val = entry.getValue(); }
效率高,以后一定要使用此种方式!
第二种:
Map map = new HashMap(); Iterator iter = map.keySet().iterator(); while (iter.hasNext()) { Object key = iter.next(); Object val = map.get(key); }
效率低,以后尽量少使用!
例:
HashMap的遍历有两种常用的方法,那就是使用keyset及entryset来进行遍历,但两者的遍历速度是有差别的,下面请看实例:
public class HashMapTest { public static void main(String[] args) ...{ HashMap hashmap = new HashMap(); for (int i = 0; i < 1000; i ) ...{ hashmap.put("" i, "thanks"); } long bs = Calendar.getInstance().getTimeInMillis(); Iterator iterator = hashmap.keySet().iterator(); while (iterator.hasNext()) ...{ System.out.print(hashmap.get(iterator.next())); } System.out.println(); System.out.println(Calendar.getInstance().getTimeInMillis() - bs); listHashMap(); } public static void listHashMap() ...{ java.util.HashMap hashmap = new java.util.HashMap(); for (int i = 0; i < 1000; i ) ...{ hashmap.put("" i, "thanks"); } long bs = Calendar.getInstance().getTimeInMillis(); java.util.Iterator it = hashmap.entrySet().iterator(); while (it.hasNext()) ...{ java.util.Map.Entry entry = (java.util.Map.Entry) it.next(); // entry.getKey() 返回与此项对应的键 // entry.getValue() 返回与此项对应的值 System.out.print(entry.getValue()); } System.out.println(); System.out.println(Calendar.getInstance().getTimeInMillis() - bs); } }
对于keySet其实是遍历了2次,一次是转为iterator,一次就从hashmap中取出key所对于的value。而entryset只是遍历了第一次,他把key和value都放到了entry中,所以就快了。
(只能访问音频文件,如music,podcast,audiobook等)
2.MPMusicPlayerController的使用有两种播放器可以选择,一种是application music player,另外一种是iPod music player。
第一种播放器是一种内部播放器,当程序对出后停止播放;而第二种播放器则与iPod播放器内的信息相关,退出之后不会停止播放。获取方式如下:
- + applicationMusicPlayer
- + iPodMusicPlayer
播放之前需要设置播放器的播放队列
- – setQueueWithQuery:
- – setQueueWithItemCollection:
管理播放模式和播放状态的一些属性
- currentPlaybackTime property
- nowPlayingItem property
- playbackState property
- repeatMode property
- shuffleMode property
- volume property
播放状态 MPMusicPlaybackState
播放控制方法
- – play
- – pause
- – stop
- – beginSeekingForward
- – beginSeekingBackward
- – endSeeking
- – skipToNextItem
- – skipToBeginning
- – skipToPreviousItem
播放状态发生变化时可以发送通知
- – beginGeneratingPlaybackNotifications
- – endGeneratingPlaybackNotifications
MPMusicPlayerControllerPlaybackStateDidChangeNotification
可以通过该通知来改变播放按钮的样式
MPMusicPlayerControllerNowPlayingItemDidChangeNotification
MPMusicPlayerControllerVolumeDidChangeNotification
具体步骤
1.注册和开始发送通知
- – setQueueWithQuery:
- – setQueueWithItemCollection:
3.MPMediaPickerController的使用
主要是设置代理和选择多媒体类型,然后通过代理方法来获取选中的歌曲
4.MPMediaItem
NSString *const MPMediaItemPropertyTitle;
NSString *const MPMediaItemPropertyAlbumTitle;
NSString *const MPMediaItemPropertyArtist;
5.MPMediaItemCollection
- + collectionWithItems:
- – initWithItems:
属性
- items property
- representativeItem property
- count property
- mediaTypes property
需要设置两个属性: filter and grouping type
filter描述查询内容,grouping type 描述返回内容的排列方式
查询可以获取items,也可以获取collections
- When you ask for items, the query returns a collection containing all the items that match the filter. The items are in “natural” order, meaning that they are ordered as iTunes shows them on the desktop.
- When you ask for collections, the media query employs not only its filter but also its grouping type.
专辑封面的使用
<View android:layout_height="1dip" android:background="#B7B7B7" />
<TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@drawable/list_bg_border" > <TableRow android:padding="10dp" > <me.mcar.parking.control.AutoAjustSizeTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/parkdetail_rushHours_text" /> <TextView android:id="@+id/parkdetail_rushHours" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableRow> <View android:layout_height="1dip" android:background="#B7B7B7" /> <TableRow android:padding="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/parkdetail_amount_text" /> <TextView android:id="@+id/parkdetail_amount" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableRow> <View android:layout_height="1dip" android:background="#B7B7B7" /> <TableRow android:padding="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/parkdetail_price_text" /> <TextView android:id="@+id/parkdetail_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="10" android:ellipsize="end" android:singleLine="true" android:width="150dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="@string/parkdetail_price_detail" android:src="/blog_article/@drawable/detail_btn/index.html" /> </TableRow> </TableLayout>