Java涔婥ollection鎺ュ彛锛堥泦鍚堬級

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java涔婥ollection鎺ュ彛锛堥泦鍚堬級相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/oar' title='oar'>oar   宸ュ叿   浜嗚В   鍒犻櫎鍏冪礌   print   鍔熻兘   鎺屾彙   

闆嗗悎姒傝堪

  • 闆嗗悎鍒板簳鏄粈涔堝憿?闆嗗悎锛氶泦鍚堟槸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>

鎶€鏈浘鐗? src=

娉ㄦ剰浜嬮」锛?/strong>

  • 涓婂浘姗欒壊妗嗛噷濉啓鐨勯兘鏄帴鍙g被鍨嬶紝鑰岃摑鑹叉閲屽~鍐欑殑閮芥槸鍏蜂綋鐨勫疄鐜扮被
  • 闆嗗悎鏈韩鏄竴涓伐鍏凤紝瀹冨瓨鏀惧湪java.util鍖呬腑銆傚湪 Collection 鎺ュ彛瀹氫箟鐫€鍗曞垪闆嗗悎妗嗘灦涓渶鏈€鍏辨€х殑鍐呭銆?nbsp;

鍗曞垪闆嗗悎鐨勪綋绯荤粨鏋勫浘

鎴戜滑鍏堢湅涓嬪浘澶ф浜嗚В涓€涓嬪崟鍒楅泦鍚堢殑缁撴瀯浣撶郴锛屼笅闈㈡垜浠潵璇︾粏瀛︿範

鎶€鏈浘鐗? src=

Collection 甯哥敤鍔熻兘

Collection鏄墍鏈夊崟鍒楅泦鍚堢殑鐖舵帴鍙o紝鍥犳鍦–ollection涓畾涔変簡鍗曞垪闆嗗悎(List鍜孲et)閫氱敤鐨勪竴浜涙柟娉曪紝杩欎簺鏂规硶鍙敤浜庢搷浣滄墍鏈夌殑鍗曞垪闆嗗悎銆傛柟娉曞涓?/span>
  • 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() : 鎶婇泦鍚堜腑鐨勫厓绱狅紝瀛樺偍鍒版暟缁勪腑
浠g爜涓句緥
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

    }

}

 

 

以上是关于Java涔婥ollection鎺ュ彛锛堥泦鍚堬級的主要内容,如果未能解决你的问题,请参考以下文章

Dubbo鎺ュ彛涓嶇煡閬撴€庝箞娴嬶紵鏁欎綘涓夎浠g爜瀹炵幇dubbo鎺ュ彛娴嬭瘯

鎺ュ彛鏌ヨ鏂规硶

alsa鎺ュ彛

IHttpClientFactory璋冪敤鎺ュ彛

鎺ュ彛娴嬭瘯 姒傝堪