Java涔婥ollection鎺ュ彛锛堥泦鍚堬級
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java涔婥ollection鎺ュ彛锛堥泦鍚堬級相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/oar' title='oar'>oar
闆嗗悎姒傝堪
- 闆嗗悎鍒板簳鏄粈涔堝憿?闆嗗悎锛氶泦鍚堟槸java涓彁渚涚殑涓€绉嶅鍣紝鍙互鐢ㄦ潵瀛樺偍澶氫釜鏁版嵁
闆嗗悎鍜屾暟缁勬棦鐒堕兘鏄鍣紝瀹冧滑鏈夊暐鍖哄埆鍛紵
鍖哄埆1锛?/strong>
- 鏁扮粍鐨勯暱搴︽槸鍥哄畾鐨勩€?/span>
- 闆嗗悎鐨勯暱搴︽槸鍙彉鐨勩€?/span>
鍖哄埆2锛?nbsp;
- 鏁扮粍涓瓨鍌ㄧ殑鏄悓涓€绫诲瀷鐨勫厓绱狅紝鍙互瀛樺偍鍩烘湰鏁版嵁绫诲瀷鍊硷紝涔熷彲浠ュ瓨鍌ㄥ紩鐢ㄧ被鍨?
- 闆嗗悎瀛樺偍鐨勯兘鏄璞°€傝€屼笖瀵硅薄鐨勭被鍨嬪彲浠ヤ笉涓€鑷淬€傚湪寮€鍙戜腑涓€鑸綋瀵硅薄澶氱殑鏃跺€欙紝浣跨敤闆嗗悎杩涜瀛樺偍銆?nbsp;
瀛︿範闆嗗悎鐨勭洰鏍?/strong>
- 浼氫娇鐢ㄩ泦鍚堝瓨鍌ㄦ暟鎹?/span>
- 浼氶亶鍘嗛泦鍚堬紝鎶婃暟鎹彇鍑烘潵
- 鎺屾彙姣忕闆嗗悎鐨勭壒鎬?/span>
瀛︿範闆嗗悎鐨勬柟寮?/h3>
- 瀛︿範椤跺眰锛?/strong>瀛︿範椤跺眰鎺ュ彛鎴栬€呮娊璞$被涓叡鎬ф柟娉曪紝鎵€鏈夌殑瀛愮被閮藉彲浠ヤ娇鐢?/span>
- 浣跨敤搴曞眰锛氶《灞備笉鏄帴鍙e氨鏄娊璞$被锛屾棤娉曠洿鎺ヤ娇鐢紝鎴戜滑闇€瑕佷娇鐢ㄥ簳灞傜殑瀛愮被鏉ュ垱寤哄璞′娇鐢?/span>
闆嗗悎妗嗘灦
鍦ㄤ娇鐢↗AVASE鎻愪緵鐨凙PI鍓嶏紝浜嗚В鍏剁户鎵夸笌鎺ュ彛鎿嶄綔鏋舵瀯锛屾墠鑳界煡閬撲綍鏃堕噰鐢ㄥ摢涓被锛屼互鍙婄被涔嬮棿濡備綍褰兼鍚堜綔锛屼粠鑰岃揪鍒扮伒娲诲簲鐢ㄣ€傞泦鍚堟寜鐓у叾瀛樺偍缁撴瀯鍙互鍒嗕负涓ゅぇ绫伙紝鍒嗗埆鏄?strong>鍗曞垪闆嗗悎java.util.Collection锛氬畾涔変簡鎵€鏈夊崟鍒楅泦鍚堢殑鍏辨€ф柟娉曘€?strong>鍙屽垪闆嗗悎java.util.Map锛氬畾涔変簡鎵€鏈夊弻鍒楅泦鍚堢殑鍏辨€ф柟娉曘€侸DK涓彁渚涗簡涓板瘜鐨勯泦鍚堢被搴擄紝涓轰簡渚夸簬鍒濆鑰呰繘琛岀郴缁熷湴瀛︿範锛屾帴涓嬫潵閫氳繃涓€寮犲浘鏉ユ弿杩版暣涓泦鍚堢被鐨勭户鎵夸綋绯汇€?/span>
娉ㄦ剰浜嬮」锛?/strong>
- 涓婂浘姗欒壊妗嗛噷濉啓鐨勯兘鏄帴鍙g被鍨嬶紝鑰岃摑鑹叉閲屽~鍐欑殑閮芥槸鍏蜂綋鐨勫疄鐜扮被
- 闆嗗悎鏈韩鏄竴涓伐鍏凤紝瀹冨瓨鏀惧湪java.util鍖呬腑銆傚湪 Collection 鎺ュ彛瀹氫箟鐫€鍗曞垪闆嗗悎妗嗘灦涓渶鏈€鍏辨€х殑鍐呭銆?nbsp;
鍗曞垪闆嗗悎鐨勪綋绯荤粨鏋勫浘
鎴戜滑鍏堢湅涓嬪浘澶ф浜嗚В涓€涓嬪崟鍒楅泦鍚堢殑缁撴瀯浣撶郴锛屼笅闈㈡垜浠潵璇︾粏瀛︿範
Collection 甯哥敤鍔熻兘
- public boolean add(E e) 锛?鎶婄粰瀹氱殑瀵硅薄娣诲姞鍒板綋鍓嶉泦鍚堜腑 銆?/span>
- public void clear() :娓呯┖闆嗗悎涓墍鏈夌殑鍏冪礌銆?/span>
- public boolean remove(E e) : 鎶婄粰瀹氱殑瀵硅薄鍦ㄥ綋鍓嶉泦鍚堜腑鍒犻櫎銆?/span>
- public boolean contains(E e) : 鍒ゆ柇褰撳墠闆嗗悎涓槸鍚﹀寘鍚粰瀹氱殑瀵硅薄銆?/span>
- public boolean isEmpty() : 鍒ゆ柇褰撳墠闆嗗悎鏄惁涓虹┖銆?/span>
- public int size() : 杩斿洖闆嗗悎涓厓绱犵殑涓暟銆?/span>
- public Object[] toArray() : 鎶婇泦鍚堜腑鐨勫厓绱狅紝瀛樺偍鍒版暟缁勪腑
public class CollectionTest { public static void main(String[] args) { //澶氭€侊細鍒涘缓闆嗗悎绫诲璞?/span> Collection<String> collection = new ArrayList<>(); //鎵撳嵃瀵硅薄鍚嶇О锛屽緱鍒扮殑涓嶆槸瀵硅薄鐨勫唴瀛樺湴鍧€鍊硷紝ArrayList绫婚噸鍐欎簡toString鏂规硶 System.out.println(collection);//[] // public boolean add(E e)锛? 鎶婄粰瀹氱殑瀵硅薄娣诲姞鍒板綋鍓嶉泦鍚堜腑 銆傝繑鍥炲€兼槸涓€涓猙oolean鍊?涓€鑸兘杩斿洖true,鎵€浠ュ彲浠ヤ笉鐢ㄦ帴鏀?/span> collection.add("鏃╅"); collection.add("涓"); collection.add("鏅氶"); collection.add("澶滃"); System.out.println(collection);//[鏃╅, 涓, 鏅氶, 澶滃] /* public boolean remove(E e): 鎶婄粰瀹氱殑瀵硅薄鍦ㄥ綋鍓嶉泦鍚堜腑鍒犻櫎銆? 杩斿洖鍊兼槸涓€涓猙oolean鍊?闆嗗悎涓瓨鍦ㄥ厓绱?鍒犻櫎鍏冪礌,杩斿洖true 闆嗗悎涓笉瀛樺湪鍏冪礌,鍒犻櫎澶辫触,杩斿洖false */ System.out.println(collection.remove("澶滃"));//true System.out.println(collection.remove("鍏冨"));//false /* public boolean contains(E e): 鍒ゆ柇褰撳墠闆嗗悎涓槸鍚﹀寘鍚粰瀹氱殑瀵硅薄銆? 鍖呭惈杩斿洖true 涓嶅寘鍚繑鍥瀎alse */ System.out.println(collection.contains("澶滃"));//false System.out.println(collection.contains("鏃╅"));//true //public int size(): 杩斿洖闆嗗悎涓厓绱犵殑涓暟銆?/span> System.out.println(+collection.size());//3 //public Object[] toArray(): 鎶婇泦鍚堜腑鐨勫厓绱狅紝瀛樺偍鍒版暟缁勪腑銆?/span> Object[] objects = collection.toArray(); //閬嶅巻鏁扮粍 for (int i = 0; i < objects.length; i++) { System.out.print(objects[i] + " ");//鏃╅ 涓 鏅氶 } System.out.println(" "); //public boolean isEmpty(): 鍒ゆ柇褰撳墠闆嗗悎鏄惁涓虹┖銆?闆嗗悎涓虹┖杩斿洖true,闆嗗悎涓嶄负绌鸿繑鍥瀎alse System.out.println(collection.isEmpty());//false //public void clear() :娓呯┖闆嗗悎涓墍鏈夌殑鍏冪礌銆備絾鏄笉鍒犻櫎闆嗗悎,闆嗗悎杩樺瓨鍦?/span> collection.clear(); //鍐嶆鏌ョ湅闆嗗悎涓厓绱犵殑涓暟鍜屽厓绱犳槸鍚︿负绌?/span> System.out.println(collection.size());//0 System.out.println(collection.isEmpty());//true } }