string、stringBuffer、stringBuilder与HashMap、ArrayList类

string、stringBuffer、stringBuilder区别

1、先介绍三者的相同之处:都可用于存储字符串,但是在存储的过程中又有不同之处:string只能存放不可变的字符串,而stringbuffer与stringbuilder可存放可变的字符串

2、从使用安全方面进行区别:string与stringbuilder是线程非安全的,而stringbuffer是线程安全的(因为在stringbuffer的实现方法中有加锁操作)

String,StringBuffer,StringBuilder都是final类,不允许被继承在本质上都是字符数组,不同的是,String的长度是不可变的而后两者长度可变,在进行连接操作时,String每次返回一个新的String实例,而StringBuffer和StringBuilder的append方法直接返回this,所以当进行大量的字符串连接操作时,不推荐使用String,因为它会产生大量的中间String对象。

StringBuffer和StringBuilder的一个区别是,StringBuffer在append方法前增加了一个synchronized修饰符,以起到同步的作用,为此也降低了执行效率;若要在toString方法中使用循环,使用StringBuilder。

对于三者的总结:1)如果操作少量的数据用String

                                    2)单线程下操作大量的数据用StringBuilder

                                    3)多线程下操作大量的数据用StringBuffer

HashMap和Hashtable的联系和区别 
实现原理相同,功能相同,底层都是哈希表结构,查询速度快,在很多情况下可以互用,早期的版本一般都是安全的。

  • HashMap和Hashtable都实现了Map接口,但决定用哪一个之前先要弄清楚它们之间的分别。主要的区别有:线程安全性,同步(synchronization),以及速度。
  • HashMap几乎可以等价于Hashtable,除了HashMap是非synchronized的,并可以接受null(HashMap可以接受为null的键值(key)和值(value),而Hashtable则不行)。
  • HashMap是非synchronized,而Hashtable是synchronized,这意味着Hashtable是线程安全的,多个线程可以共享一个Hashtable;而如果没有正确的同步的话,多个线程是不能共享HashMap的。Java 5提供了ConcurrentHashMap,它是HashTable的替代,比HashTable的扩展性更好。
  • 另一个区别是HashMap的迭代器(Iterator)是fail-fast迭代器,而Hashtable的enumerator迭代器不是fail-fast的。所以当有其它线程改变了HashMap的结构(增加或者移除元素),将会抛出ConcurrentModificationException,但迭代器本身的remove()方法移除元素则不会抛出ConcurrentModificationException异常。但这并不是一个一定发生的行为,要看JVM。这条同样也是Enumeration和Iterator的区别。
  • 由于Hashtable是线程安全的也是synchronized,所以在单线程环境下它比HashMap要慢。如果你不需要同步,只需要单一线程,那么使用HashMap性能要好过Hashtable。 
    HashMap不能保证随着时间的推移Map中的元素次序是不变的。
  • 遍历HashMap的四种方法

    public class HashMapDemo {
    public static void main(String[] args) {
    // 创建一个hashmap
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("张三", "25");
    map.put("李四", "26");
    map.put("王五", "27");
    map.put("麻子", "28");
    // 第一种:普通使用,二次取值, 推荐常用掌握*****
    System.out.println("\n通过Map.keySet遍历key和value:");
    for (String key : map.keySet()) {
    System.out.println("Key: " + key + " Value: " + map.get(key));
    }

    // 第二种
    System.out.println("\n通过Map.entrySet使用iterator遍历key和value: ");
    Iterator map1it = map.entrySet().iterator();
    while (map1it.hasNext()) {
    Map.Entry<String, String> entry = (Entry<String, String>) map1it
    .next();
    System.out.println("Key: " + entry.getKey() + " Value: "
    + entry.getValue());
    }

    // 第三种:推荐,尤其是容量大时 推荐常用掌握*****
    System.out.println("\n通过Map.entrySet遍历key和value");
    for (Map.Entry<String, String> entry : map.entrySet()) {
    System.out.println("Key: " + entry.getKey() + " Value: "
    + entry.getValue());
    }
    // 第四种 ,这种的话不推荐使用
    System.out.println("\n通过Map.values()遍历所有的value,但不能遍历key");
    for (String v : map.values()) {
    System.out.println("The value is " + v);
    }
    }
  • 集合ArrayList是接口List的一种子类,它的特点是:存储的元素是有序的.底层的数据结构是数组.查询快,增删慢.在众多集合中ArrayList的遍历又是比较特殊的,下面就写一下它的四中遍历方式,代码如下     
  • public class ArraylistDemo {
public static void main(String[] args) {
// 创建一个集合
List<String> al = new ArrayList<String>();
al.add("张三");
al.add("李四");
al.add("王二");
al.add("麻五");
//遍历方式1  Iterator的遍历方式
       /* Iterator<String> it1 = al.iterator();  
        while(it1.hasNext()){  
            System.out.println(it1.next());  
        }  */
          
        //遍历方式2  Iterator的遍历方式
        /*for(Iterator it2 = al.iterator();it2.hasNext();){  
            System.out.println(it2.next());  
        } */ 
          
        //遍历方式3  temp比较方式 推荐常用掌握*****
       /* for(String temp:al){  
            System.out.println(temp);  
        }  */
          
        //遍历方式4  增强for循环的遍历方式 推荐常用掌握*****
        for(int i = 0;i<al.size();i++){  
            System.out.println(al.get(i));  
        }  

}

遍历输出结果:

张三
李四
王二
麻五

猜你喜欢

转载自blog.csdn.net/qq_30764991/article/details/80641080