您的位置:首页 > 编程语言 > Java开发

《第一行代码Java》Java类集框架部分学习笔记与代码

2018-02-03 11:58 806 查看
   总结:类集就是Java数据结构实现,类集就是动态对象数组

Collection但对象保存(单身贵族),Map偶对象保存(甜蜜情侣)

  .类集在整个Java中最为核心的用处就在于实现了动态对现象数组的操作,并且定义了大量的操作方法

核心接口为:Collection,List,Set,Map,Iterator,Enumberation。

Collection是进行但对象保存的最大父接口,add()与iteartor()方法最为常用,Collection接口它本身

不能区分集合中的元素是否重复,实际开发中用它的两个子接口:List,Queue(数据允许重复),Set(不允许数据重复)

List接口交Collection接口提拱了get(索引)取得内容,List的两个子类ArrayList异步处理,非线程安全,Vector类采用同步处理是线程安全的





List接口常用的是一个ArrayList实现类

package org.java.classcollection;

import java.util.ArrayList;
import java.util.List;

public class ArrayListDemo {
/**
 *
 * List扩充的方法:get(i)      public  E set(int x,E element)
 * @param args
 */
    public static void main(String[] args) {
        List<String>  all=new  ArrayList<String>();
        //size()方法以及isEmpty()是否为空方法
        System.out.println("长度:"+all.size()+",是否为空:"+all.isEmpty());
        all.add("hello");
        all.add("hello");
        all.add("world");
        System.out.println("长度:"+all.size()+",是否为空:"+all.isEmpty());
        for(int i=0;i<all.size();i++)
        {
            String  str=all.get(i);//i 集合下标的索引值从0开始
            System.out.print(str+" ");
        }
        all.set(1, "你好");//修改指定编号为1的值为你好,即把第二个元素值改为你好
        System.out.println("------------------");
        for(int i=0;i<all.size();i++)
        {
            String  str=all.get(i);//i 集合下标的索引值从0开始
            System.out.print(str+" ");
        }
    }
}
/*输出:
长度:0,是否为空:true
长度:3,是否为空:false
hello hello world ------------------
hello 你好 world */

Set接口(没有get()方法取内容,不能重复保存数据)主要有HaspSet子类(散列存储)和TreeSet子类(有序存放)

package org.java.classcollection;

import java.util.HashSet;
import java.util.Set;

public class HashSetDemo {
public static void main(String[] args) {
Set<String>  allSet=new HashSet<String>();
allSet.add("qwert");
allSet.add("qwert");//重复的数据没保存
allSet.add("asdfg");
allSet.add("zxcvb");
System.out.println(allSet);
}
}
//结果[qwert, zxcvb, asdfg],重复的数据没保存而且是无序的


package org.java.classcollection;

import java.util.Set;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        Set<String>  allSet=new TreeSet<String>();
        allSet.add("qwert");
        allSet.add("qwert");//重复的数据没保存
        allSet.add("asdfg");
        allSet.add("zxcvb");
        allSet.add("Asdfg");
        System.out.println(allSet);
    }
}
/*输出:有序  a开头[Asdfg, asdfg, qwert, zxcvb]
   按照ASCII码比较A:65  a:A+32  95 从小到大*/

集合输出的基本操作一般有4种:Iterator输出、ListIterator输出、foreach,  Enumeration输出但主要以Iterator输出, Enumberation为主,而Enumeration枚举输出的Enumeration出现较早要与Vector类一起使用,

 * 因为接口方法在Vector中定义的public  Enumeration <E> elements()

package org.java.classcollection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class IteratorDemo {
public static void main(String[] args) {
List<String> all=new  ArrayList<String>();
all.add("hello");
all.add("hello");
all.add("world");
Iterator<String> iter=all.iterator();//实例化Iterator接口
while(iter.hasNext()){
String  str=iter.next();
System.out.println(str);
}
}
}

/*hello
hello
world*/

 


package org.java.classcollection;

import java.util.Enumeration;
import java.util.Vector;
/**
*
* Enumeration出现较早要与Vector类一起使用,
* 因为接口方法在Vector中定义的public  Enumeration <E> elements()
* @author coder
*
*/
public class EnumerationDemo {

public static void main(String[] args) {
Vector<String> all=new Vector<String>();
all.add("asdfg");
all.add("qwert");
all.add("zxcvb");
Enumeration<String>  enu=all.elements();//取得Enumeration接口对象
while(enu.hasMoreElements()){
String  str=enu.nextElement();
System.out.println(str);
}

}
}
/*asdfg
qwert
zxcvb*/

   .Map集合存数据是为了查找,Collection是为了输出

Map主要有两个常用子类HashMap(常用    非线程安全,允许key或者value允许为null)和Hashtable(线程安全,不允许设置null)

实际开发中Map中的key值的类型一般使用系统提供的String,Integer,String最常用,不建议使用自定义类作为key

package org.java.classcollection;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/*Map集合中的key不允许有重复*/

public class MapDemo {
public static void main(String[] args) {
Map<String, Integer> map=new  HashMap<String, Integer>();
map.put("a", 1);
map.put("hello", 0);
map.put("二", 3);
map.put("a", 66);
map.put("b", 2);
map.put(null, 0);

System.out.println(map);
//key值不存在返回null
System.out.println(map.get("一"));

System.out.println(map.get("a"));
System.out.println(map.get(null));
System.out.println("---------------");

//取得所有的key值  这里是String类型

//以下输出只是为了证明key值不允许重复
Set<String>  set=map.keySet();
Iterator <String>  iterator=set.iterator();//遍历时要加遍历类型
while(iterator.hasNext()){
String  str=iterator.next();
System.out.println(str);
}

}
}
/*
*
* {null=0, a=66, b=2, hello=0, 二=3}
null
66
0
*
* 使用HaspMap定义的Map集合是无序的
* 有重复的key会覆盖旧的内容
* 使用HaspMap保持数据允许value值为空
* {null=0, a=66, b=2, hello=0, 二=3}
null
66
0
---------------
null
a
b
hello
二

*
*
*
*
*/

package org.java.classcollection;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class IteratorMap {
public static void main(String[] args) {
Map<String, Integer> map = new Hashtable<String, Integer>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.put("three", 33);
// 对于Map集合利用Iterator的输出步骤:
// 1.利用Map接口的entrySet()方法将Map集合变为Set集合里面的泛型是
// Map.Entry
// 2.利用Set集合中的iterator()方法取得实例对Set集合进行输出
// 每一次输出的都是Map.Entry接口对象,利用此对象进行key 与value的输出
Set<Map.Entry<String, Integer>> set = map.entrySet();
Iterator<Map.Entry<String, Integer>> iter = set.iterator();
while (iter.hasNext()) {
Map.Entry<String, Integer> mEntry = iter.next();
System.out.println(mEntry.getKey() + "=" + mEntry.getValue());

}

}

}

/*two=2
one=1
three=33

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: