• java 集合


    Java集合,数目是可变的,并且集合只能存储引用数据类型,不能放基本类型
    所有集合类都存放在 java.util包中,所以使用集合时,需要导入该包
    集合又称为类集或容器
    Java中的集合类主要由两个接口派生:Collection/Map
    Collection接口派生了 List, Set集合
    定义形式如下:
    public interface Collection<E> extends Interable<E>
    
    Collection 接口的方法定义
    
    //向集合插入一个元素
    public boolean add(E o)
    
    //向集合添加一个集合 c 的所有元素
    public boolean addAll(Collection<? extends E> c)
    
    //清除集合中所有的元素
    public void clear()
    
    //判断集合中是否存在指定的元素。若存在,则返回 true,否则返回 false
    public boolean contains(Object o)
    
    //判断集合中是否存在指定的集合 c 的所有元素,若存在,则返回 true,否则为 false
    public boolean containsAll(Collection<?> c)
    
    //比较两个集合是否相等
    public boolean equals(Object o)
    
    //返回此 collection 的哈希值
    public int hashCode()
    
    //判断此集合是否为空。若为空,则返回 true,否则返回 false
    public boolean isEmpty()
    
    // 为 Iterator接口实例化,用于遍历集合中的元素
    public Iterator<E> iterator()
    
    //从集合中删除指定的元素
    public boolean remove(Object o)
    
    //从集合中删除指定集合c中出现的所有元素
    public boolean removeAll(Collection<?> c)
    
    //从集合中删除指定集合 c 中出现的所有元素
    public boolean retainAll(Collection<?> c)
    
    //返回此集合中的元素个数
    public int size()
    
    //返回包含此元素中所有元素的对象数组
    public Object[] toArray()
    
    //返回追念此元素中所有元素的数组,并可以指定返回的数组类型
    
    一般情况下,不会直接操作 Collection 接口,而是调用其子接口:
    List<E>:集合中的元素按照索引值来排序,允许存放重复的元素
    
    Queue<E>:队列接口,以 先进先出的方式排序
    
    Set<E>:集合中的元素不按特定方式排序,不能存放重复的,但是用别的方法实现排序
    
    SortedSet<E>:可以对集合中的元素进行排序
    public <T> T[] toArray(T[] a)
    
    List 接口,主要有两个实现类: ArrayList 和 LinkedList
    
    List接口的定义如下:
    public interface List<E> extends Collection<E>
    常用方法:
    
    //在集合的指定位置插入一个元素
    public void add(int index, E element)
    
    //在集合的指定位置插入集合 c 的所有元素
    public boolean addAll(int index, Collection<? extends E> c)
    
    //返回此集合中指定位置的元素
    public E get(int index)
    
    //返回此集合中首次出现指定元素的索引,如果不存在,则返回 -1
    public int indexOf(Object o)
    
    //返回此集合中最后出现指定元素的索引,如果没有,则返回 -1
    public int lastIndexOf(Object o)
    
    //为 ListIterator接口实例化,用于遍历集合中的元素
    public ListIterator<E> listIterator()
    
    //同上,只是按指定的位置开始
    public ListIterator<E> listIterator(int index)
    
    //删除指定位置的元素
    public E remove(int index)
    
    //替换指定位置的元素
    public E set(int index, E element)
    
    //返回一个新的元素集合,包含从指定的位置到指定的位置元素
    public List<E> subList(int fromIndex, int toIndex)
    
    数组列表类  ArrayList类
    
    //增加数组的容量
    public void ensureCapacity(int minCapacity)
    
    //删除集合中索引在 fromIndex(包括)和toIndex(不包括)之间的所有元素
    protected void removeRange(int fromIndex, int toIndex)
    
    //调整为恰当的大小
    public void trimSize()
    
    //向集合中添加元素
    import java.util.Collection;
    import java.util.List;
    import java.util.ArrayList;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		// 通过 ArrayList实例化 Collection
    		Collection<String> collection = new ArrayList<String>();
    		//通过 ArrayList实例化 list
    		List<String> list = new ArrayList<String>();
    		//添加元素
    		collection.add("1");
    		collection.add("2");
    		collection.add("3");
    		// 打印集合中的元素
    		System.out.println("collection集合: "+collection);
    		// 添加元素
    		list.add("A");
    		list.add("C");
    		list.add(1, "B");
    		list.addAll(0, collection);
    		System.out.println("list集合:"+list);
    	}
    }
    /*
    collection集合: [1, 2, 3]
    list集合:[1, 2, 3, A, B, C]
    */
    
    // 删除集合中的元素
    import java.util.Collection;
    import java.util.List;
    import java.util.ArrayList;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		// 通过 ArrayList实例化 collection
    		Collection<String> collection = new ArrayList<String>();
    		//通过 ArrayList 实例化 List
    		List<String> list = new ArrayList<String>();
    		//添加元素
    		collection.add("1");
    		collection.add("2");
    		collection.add("3");
    		System.out.println("collection 集合:"+collection);
    		list.add("A");
    		list.add("C");
    		list.add(1, "B");
    		//向指定的位置添加一个集合的所有元素
    		list.addAll(0,collection);
    		System.out.println("list集合删除前:"+list);
    		//删除元素 B
    		list.remove("B");
    		// 删除指定位置的元素
    		list.remove(3);
    		//删除指定集合的元素
    		list.removeAll(collection);
    		System.out.println("list集合删除后:"+list);
    	}
    }
    /*
    collection 集合:[1, 2, 3]
    list集合删除前:[1, 2, 3, A, B, C]
    list集合删除后:[C]
    */
    // 集合的其他操作
    import java.util.Collection;
    import java.util.List;
    import java.util.ArrayList;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		// 通过 ArrayList实例化 collection
    		Collection<String> collection = new ArrayList<String>();
    		//通过 Arraylist实例化 List
    		List<String> list = new ArrayList<String>();
    		//添加元素
    		collection.add("1");
    		collection.add("2");
    		collection.add("3");
    		list.add("A");
    		list.add("C");
    		list.add(1,"B");
    		//向指定位置添加一个集合的所有的元素
    		list.addAll(0,collection);
    		System.out.println("list集合:"+list);
    		//截取集合
    		List<String> subList = list.subList(1, 5);
    		System.out.println("subList集合:"+subList);
    		//获取指定位置的元素
    		System.out.println("设置list集合元素前::"+list.get(3));
    		//设置指定位置的元素
    		list.set(3,"set");
    		System.out.println("设置list集合元素后::"+list.get(3));
    		// 获取指定元素在集合中的索引位置,若不存在,则返回 -1
    		System.out.println("list集合中,元素3的位置:"+list.indexOf("3")+",元素D的位置:"+list.indexOf("D"));
    		// 将集合转为字符串数组
    		String arrString[] = list.toArray(new String[]{});
    		for(String str: arrString)
    		{
    			System.out.println(str+" ");
    		}
    		//判断集合是否为空
    		System.out.println("
    list集合是滞为空:"+list.isEmpty());
    		//清除集合中的所有元素
    		list.clear();
    		System.out.println("
    list集合是为空:"+list.isEmpty());
    	}
    }
    /*
    list集合:[1, 2, 3, A, B, C]
    subList集合:[2, 3, A, B]
    设置list集合元素前::A
    设置list集合元素后::set
    list集合中,元素3的位置:2,元素D的位置:-1
    1
    2
    3
    set
    B
    C
    
    list集合是滞为空:false
    
    list集合是为空:true
    */
    链表类 LinkedList 类
    是用 链表结构保存元素
    常用方法:
    public void addFirst(E o)
    public void addLast(E o)
    public Iterator<E> descendingIterator()
    public E element()
    public E get(int index)
    public E getFirst()
    public E getLast()
    public int indexOf(Object o)
    public int lastIndexOf(Object o)
    
    public ListIterator<E> listIterator(int index)
    public boolean offer(E o)
    public boolean offerFirst(E e)
    public boolean offerLast(E e)
    
    public E peek()
    public E peekFirst()
    public E peekLast()
    
    public E poll()
    public E pollFirst()
    public E pollLast()
    public E pop()
    public void push(E e)
    public E remove()
    
    public E remove(int index)
    public boolean remove(Object o)
    public E removeFirst()
    public boolean removeFirstOccurrence(Object o)
    public E removeLast()
    public boolean removeLastOccurrence(Object o)
    public E set(int index, E element)
    
    //在链表开头和结尾增加数据
    import java.util.List;
    import java.util.LinkedList;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		LinkedList<String> link = new LinkedList<String>();
    		link.add("1");
    		link.add("2");
    		link.add("3");
    		System.out.println("添加前:"+link);
    		//在链表开头处增加
    		link.addFirst("F");
    		//在链表结尾处增加
    		link.addLast("L");
    		System.out.println("添加后:"+link);
    	}
    }
    /*
    添加前:[1, 2, 3]
    添加后:[F, 1, 2, 3, L]
    */
    获取链表头
    import java.util.List;
    import java.util.LinkedList;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		LinkedList<String> link = new LinkedList<String>();
    		link.add("1");
    		link.add("2");
    		link.add("3");
    		link.addFirst("F");
    		link.addLast("L");
    		LinkedList<String> newLink = new LinkedList<String>(link);
    		System.out.println("添加元素后:
    "+link);
    		System.out.println("get()方法获取表头:"+link.getFirst());
    		System.out.println("使用 get()方法后:
    "+link);
    		System.out.println("element()方法获取表头:"+link.element());
    		System.out.println("使用element()方法后:
    "+link);
    		System.out.println("peek()方法获取表头:"+link.peek());
    		System.out.println("使用peek()方法后:"+link);
    		System.out.println("poll()方法获取表头:"+link.poll());
    		System.out.println("使用poll()方法后:"+link);
    		System.out.println("pop()方法获取表头:"+link.pop());
    		System.out.println("使用pop()方法后:"+link);
    		System.out.println("
    ********************
    ");
    		int len = newLink.size();
    		for(int i=0; i<len; i++)
    		{
    			System.out.print(newLink.poll()+"	");
    		}
    		System.out.println();
    	}
    }
    /*
    添加元素后:
    [F, 1, 2, 3, L]
    get()方法获取表头:F
    使用 get()方法后:
    [F, 1, 2, 3, L]
    element()方法获取表头:F
    使用element()方法后:
    [F, 1, 2, 3, L]
    peek()方法获取表头:F
    使用peek()方法后:[F, 1, 2, 3, L]
    poll()方法获取表头:F
    使用poll()方法后:[1, 2, 3, L]
    pop()方法获取表头:1
    使用pop()方法后:[2, 3, L]
    
    ********************
    
    F       1       2       3       L
    */
    
    Set 接口
    是一个不包含重复元素的 Collection,但可以包含 null 元素,两个实现类 HashSet和 TreeSet
    
    散列集: HashSet类
    是按照哈希算法来存取集合中的元素的,当向 HashSet集合中添加元素时,就会调用该元素的 hashCode()方法,
    获取其哈希码值,然后按个这哈希值来计算出该元素存放的位置.
    
    // 没有重写 equals()和hashCode()方法,
    // 因为三个重复的元素是不在同一个内存地址的对象的,
    // 使用 Person类中默认的 equals()比较结果是 false,所以出现以下表面重复现象
    import java.util.Set;
    import java.util.HashSet;
    class Person
    {
    	private String name;
    	private int age;
    	public Person(){}
    	public Person(String name, int age)
    	{
    		this.name = name;
    		this.age = age;
    	}
    	public String toString()
    	{
    		return ("姓名:"+name+", 年龄:"+age+"
    ");
    	}
    }
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		//通过 hashSet实例化 set
    		Set<Person> set = new HashSet<Person>();
    		// 添加元素,并且有重复的
    		set.add(new Person("小二", 21));
    		set.add(new Person("小四", 22));
    		set.add(new Person("小二", 21));
    		set.add(new Person("小二", 21));
    		System.out.println(set);
    	}
    }
    /*
    [姓名:小四, 年龄:22
    , 姓名:小二, 年龄:21
    , 姓名:小二, 年龄:21
    , 姓名:小二, 年龄:21
    ]
    */
    // 重写 equals()和hashCode()方法
    // 通过重写,排序重复值
    import java.util.Set;
    import java.util.HashSet;
    class Person
    {
    	private String name;
    	private int age;
    	public Person(){}
    	public Person(String name, int age)
    	{
    		this.name = name;
    		this.age = age;
    	}
    	// 重写 equals()方法
    	public boolean equals(Object o)
    	{
    		if(this == o)
    		{
    			return true;
    		}
    		if(o == null)
    		{
    			return false;
    		}
    		if(!(o instanceof Person))
    		{
    			return false;
    		}
    		Person per = (Person)o;
    		if(this.name.equals(per.name) && this.age == per.age)
    		{
    			return true;
    		}else
    		{
    			return false;
    		}
    	}
    	//重写 hashCode()方法
    	public int hashCode()
    	{
    		final int prime = 13;
    		int result = 13;
    		result = prime * result + ((name == null) ? 0 : name.hashCode());
    		result = result * prime + age;
    		return result;
    	}
    	public String toString()
    	{
    		return ("姓名:"+name+",年龄:"+age+"
    ");
    	}
    }
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		// 通过 HashSet实例化set
    		Set<Person> set = new HashSet<Person>();
    		set.add(new Person("小二", 21));
    		set.add(new Person("小一", 30));
    		set.add(new Person("小二", 21));
    		set.add(new Person("小一", 30));
    		System.out.println(set);
    	}
    }
    /*
    [姓名:小一,年龄:30
    , 姓名:小二,年龄:21
    ]
    */
    // 树集: TreeSet类
    实现了 java.util包中的 set接口和 SortedSet接口。 
    TreeSet集合中的元素默认情况下
    是升序(自然排序).若自定义自己排序,可以使用
    TreeSet类的构造方法 TreeSet(Comparator compatator)
    常用方法如下:
    //构造一个新的 TreeSet实例,该集合以升序方式排序
    public TreeSet()
    
    //构造一个新的 TreeSet实例,并根据指定的排序
    public TreeSet(Compatator<? super E> comparator)
    public boolean add(E e)
    public boolean addAll(Collection<? extends E> c)
    public E ceiling(E e)
    public void clear()
    public Comparator<? super E> comparator()
    public boolean contains(Object o)
    public Iterator<E> descendingIterator()
    public E first()
    public E floor(E e)
    public SortedSet<E> headSet(E toElement)
    public boolean isEmpty()
    public Iterator<E> iterator()
    public E last()
    public E lower(E e)
    public E pollFirst()
    public E pollLast()
    public boolean remove(Object o)
    public int size()
    public SortedSet<E> subSet(E fromElement, E toElement)
    public SortedSet<E> tailSet(E fromElement)
    
    // TreeSet集合的默认排序,并且排除重复值
    import java.util.Set;
    import java.util.TreeSet;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		Set<Integer> tset = new TreeSet<Integer>();
    		tset.add(1);
    		tset.add(4);
    		tset.add(3);
    		tset.add(2);
    		tset.add(3);
    		System.out.println(tset);
    	}
    }
    // [1, 2, 3, 4]
    // =============
    // 自定义排序实现接口
    import java.util.Set;
    import java.util.TreeSet;
    class Person implements Comparable<Person>
    {
    	private String name;
    	private int age;
    	public Person(){}
    	public Person(String name, int age)
    	{
    		this.name = name;
    		this.age = age;
    	}
    	public int compareTo(Person per)
    	{
    		if(this.age > per.age)
    		{
    			return 1;
    		}else if(this.age < per.age)
    		{
    			return -1;
    		}else
    		{
    			return this.name.compareTo(per.name);
    		}
    	}
    	public String toString()
    	{
    		return ("姓名:"+name+",年龄:"+age+"
    ");
    	}
    }
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		Set<Person> tset = new TreeSet<Person>();
    		tset.add(new Person("小二", 21));
    		tset.add(new Person("小一", 22));
    		tset.add(new Person("小二", 21));
    		tset.add(new Person("小二", 21));
    		tset.add(new Person("小四", 25));
    		System.out.println(tset);
    	}
    }
    /*
     [姓名:小二,年龄:21
    , 姓名:小一,年龄:22
    , 姓名:小四,年龄:25
    ]
    */
    // ===========
    集合的输出
    Iterator:迭代器输出,常用
    foreach: 1.5后新增加
    Iterator()方法即可获得该集合的迭代器,接口方法:
    
    //判断集合中是否还存在下一个元素。若存在,则为 true,否则为 false
    public boolean hasNext()
    
    //返回集合中的下一个元素,E代表元素的数据类型
    public E next()
    
    //将集合中上一个由 next()方法返回的元素删除
    public void remove()
    
    //输出集合中的所有元素
    import java.util.List;
    import java.util.LinkedList;
    import java.util.Iterator;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		LinkedList<String> link = new LinkedList<String>();
    		link.add("1");
    		link.add("2");
    		link.add("3");
    		link.add("A");
    		Iterator<String> it = link.iterator();
    		while(it.hasNext())
    		{
    			System.out.print(it.next()+" ");
    		}
    	}
    }
    // 1 2 3 A
    // ============
    //删除集合中指定的元素
    import java.util.List;
    import java.util.LinkedList;
    import java.util.Iterator;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		LinkedList<String> link = new LinkedList<String>();
    		link.add("1");
    		link.add("2");
    		link.add("3");
    		link.add("A");
    		link.add("B");
    		Iterator<String> it = link.iterator();
    		//判断是否还有元素,若有,则作处理
    		while(it.hasNext())
    		{
    			String strIt = it.next(); //取得元素
    			//判断是否与指定的相等
    			if(strIt.equals("3") || strIt.equals("B"))
    			{
    				it.remove();
    			}else
    			{
    				System.out.print(strIt+" ");
    			}
    		}
    	}
    }
    // 1 2 A
    // ===========
    //删除全部元素,可用 removeAll()或以下方法
    import java.util.List;
    import java.util.LinkedList;
    import java.util.Iterator;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		LinkedList<String> link = new LinkedList<String>();
    		link.add("1");
    		link.add("2");
    		link.add("3");
    		link.add("A");
    		link.add("B");
    		Iterator<String> it = link.iterator();
    		//判断是否还有元素,若有,则作处理
    		while(it.hasNext())
    		{
    			it.next(); //取得元素
    			it.remove();	
    		}
    		System.out.println("元素数目:"+link.size());
    	}
    }
    // 元素数目:0
    // ===========
    foreach
    格式如下:
    for(类型 临时变量 : 集合)
    {
    	// code
    }
    
    import java.util.List;
    import java.util.LinkedList;
    import java.util.Iterator;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		LinkedList<String> link = new LinkedList<String>();
    		link.add("1");
    		link.add("2");
    		link.add("A");
    		for (String str : link)
    		{
    			System.out.print(str+" ");
    		}
    	}
    }
    // 1 2 A
    // ------------
    
    Map接口
    并没有继承 Collection接口,可用于保存具有映射关系的数据, key,value对
    子类 HashMap和TreeMap
    接口的定义:
    public interface Map<K,V>
    常用方法:
    //删除此Map集合中所有的 key-value
    public void clear()
    
    //判断指定的 key是否存在,有则为 true
    public boolean containsKey(Object key)
    
    //判断指定的 value是否存在,有则为 true
    public boolean containsValue(Object value)
    
    //将此 Map对象转换为 set集合
    public Set<Map.Entry<K,V>> entrySet()
    
    //比较指定的对象 o与此 Map对象是否相等,若相等,则为 true
    public boolean equals(Object o)
    
    //返回指定 key所映射的 value值,如果没有,则返回 null
    public V get(Object key)
    
    //返回 Map集合的哈希码值
    public int hashCode()
    
    //判断是否为空
    public boolean isEmpty()
    
    //返回此Map集合中所有的 key值
    public Set<K>keySet()
    
    //向此Map集合添加新的元素
    public V put(K key, V value)
    
    //将指定的 Map集合中的所有内容添加到此 Map集合中
    public void putAll<Map<? extends K, ? extends V>m)
    
    //根据 key值删除相应的  value值
    public V remove(Object key)
    
    //返回此 Map集合的元素个数
    public int size()
    
    //获取 map集合中所有的 value值
    public Collection<V> values()
    // -------------
    Map.Entry接口
    定义如下:
    public static interface Map.Entry<K,V>
    方法如下:
    // 比较指定对象与此 Entry是否相等
    public boolean equals(Object o)
    
    //获取此 Entry中包含的 key值
    public K getKey()
    
    //获取此 Entry中包含的 value值
    public V getValue()
    
    //返回此 Entry的哈希码值
    public int hashCode()
    
    //设置此 Entry的value的值
    public V setValue(V value)
    
    哈希映射类: HashMap类
    定义如下:
    public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>,Cloneable,Serializable
    // 
    向 Map集合添加和获取集合中的内容
    
    import java.util.Map;
    import java.util.HashMap;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		Map<Integer,String> map = new HashMap<Integer, String>();
    		map.put(1,"A");
    		map.put(2,"B");
    		map.put(3,"C");
    		System.out.println("获取内容:"+map.get(3));
    	}
    }
    // 获取内容:C
    // ===========
    
    // 获取 Map集合中全部的 Key和value
    import java.util.Collection;
    import java.util.Set;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.HashMap;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		Map<Integer, String> map = new HashMap<Integer, String>();
    		map.put(1, "A");
    		map.put(2, "B");
    		map.put(3, "C");
    		//将集合中全部的 key转换为 set集合
    		Set<Integer> set = map.keySet();
    		Iterator<Integer> itKey = set.iterator();
    		System.out.println("Map集合中全部的 key:");
    		while(itKey.hasNext())
    		{
    			System.out.print(itKey.next()+"  "); // 输出 key值
    		}
    		System.out.println();
    		//将集合中全部的 value转换为 collection集合
    		Collection<String> c = map.values();
    		Iterator<String> itValue = c.iterator();
    		System.out.println("Map集合中全部的 value:");
    		while(itValue.hasNext())
    		{
    			System.out.print(itValue.next()+" ");
    		}
    	}
    }
    /*
    Map集合中全部的 key:
    1  2  3
    Map集合中全部的 value:
    A B C
    */
    // 使用 iterator输出 Map集合
    import java.util.Set;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.HashMap;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		Map<Integer, String> map = new HashMap<Integer,String>();
    		map.put(1, "A");
    		map.put(2,"B");
    		// 将 map集合转换成 set集合
    		Set<Map.Entry<Integer, String>> set = map.entrySet();
    		Iterator<Map.Entry<Integer, String>> it = set.iterator();
    		System.out.println("key =============value");
    		while(it.hasNext())
    		{
    			Map.Entry<Integer, String> mapEntry = it.next();
    			System.out.println(mapEntry.getKey()+"--------"+mapEntry.getValue());
    		}
    	}
    }
    /*
    key =============value
    1--------A
    2--------B
    */
    // ===========
    
    // 使用 foreach 输出Map集合
    import java.util.Map;
    import java.util.HashMap;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		Map<Integer,String> map = new HashMap<Integer, String>();
    		map.put(1, "A");
    		map.put(2, "B");
    		System.out.println("Key ---------value");
    		for(Map.Entry<Integer, String> mapEntry : map.entrySet())
    		{
    			System.out.println(mapEntry.getKey()+"---------"+mapEntry.getValue());
    		}
    	}
    }
    /*
    Key ---------value
    1---------A
    2---------B
    */
    
    有序树映射类: TreeMap类
    定义如下:
    public class TreeMap<K,V> extends AbstractMap<K,V>
    implements NavigableMap<K,V>,Cloneable,Serializable
    不但实现Map接口,还实现SortedMap接口,因此具体顺序性,主要是对所有的 key进行排序
    // 输出有序的 Map集合
    import java.util.Collection;
    import java.util.Set;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.HashMap;
    import java.util.TreeMap;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		Map<String, String> hashMap = new HashMap<String,String>();
    		hashMap.put("bb", "B值--1");
    		hashMap.put("zz", "Z值--2");
    		hashMap.put("aa", "A值--3");
    		//将 map集合转为 set集合
    		Set<Map.Entry<String, String>> hashSet = hashMap.entrySet();
    		Iterator<Map.Entry<String, String>> hashIt = hashSet.iterator();
    		System.out.println("使用hashMap输出");
    		while(hashIt.hasNext())
    		{
    			Map.Entry<String, String> hashEntry = hashIt.next();
    			System.out.println(hashEntry.getKey()+"---->"+hashEntry.getValue());
    		}
    		Map<String,String> treeMap = new TreeMap<String,String>();
    		// 向TreeMap集合中添加HashMap集合
    		treeMap.putAll(hashMap);
    		//将 Map集合转换为 Set集合
    		Set<Map.Entry<String, String>> treeSet = treeMap.entrySet();
    		Iterator<Map.Entry<String, String>> treeIt = treeSet.iterator();
    		System.out.println("
    使用TreeMap输出:");
    		while(treeIt.hasNext())
    		{
    			Map.Entry<String, String> treeEntry = treeIt.next();
    			System.out.println(treeEntry.getKey()+"---------"+treeEntry.getValue());
    		}
    	}
    }
    /*
    使用hashMap输出
    bb---->B值--1
    zz---->Z值--2
    aa---->A值--3
    
    使用TreeMap输出:
    aa---------A值--3
    bb---------B值--1
    zz---------Z值--2
     */
    // ---------
    
    集合工具类:Collections
    用于操作List,Set和Map等集合的工具类,该类提供了大量的方法可
    以对集合元素进行排序,查询和修改等操作
    定义如下:
    public class Collections extends Object
    
    常用方法:
    //返回一个空的 List集合
    public static List EMPTY_LIST
    
    //返回一个空的Map集合
    public static Map EMPTY_MAP
    
    //返回一个空的 Set集合
    public static Set EMPTY_SET
    
    //将所有指定的元素添加到指定集合中
    public static <T> boolean addAll(Collection<? super T> c,T .. elements)
    
    //使用二分搜索法搜索列表
    public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
    
    //将src集合中的所有元素复制到 desc集合中
    public static <T> void copy(List<? super T> desc, List<? extends T> src)
    
    //判断两个集合中是否存在相同的元素
    public static boolean disjoint(Collection<?> c1, Collection<?> c2)
    
    //使用指定元素替换指定集合中的所有元素
    public static <T> void fill(List<? super T> list, T obj)
    
    //根据元素的自然排序,返回指定的最大元素
    public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
    
    //根据指定比较器产生的顺序,返回集合中最大的元素
    public static <T> T max(Collection<? extends T> coll,Comparator<? super T>comp)
    
    //
    public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)
    
    //
    public static <T> T min(Collection<? extends T> coll, Comparator<? super T>comp)
    
    //
    public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal)
    
    //反序输出集合的所有元素
    public static void reverse(List<?> list)
    
    //根据自然排序,对指定集合进行升序排序
    public static <T extends Comparable<? super T>> void sort(List<T> list)
    
    //
    public static <T> void sort(List<T> list, Comparator<?super T> c)
    
    //在指定列表的指定位置处交换元素
    public static void swap(List<?> list, int i, int j)
    
    //将指定集合设置成支持的同步集合
    public static <T> Collection<T> synchroizedCollection(Collection<T> c)
    
    // 向集合添加元素并执行反序操作
    import java.util.Collection;
    import java.util.Collections;
    import java.util.List;
    import java.util.ArrayList;
    import java.util.Iterator;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		List<String> list = new ArrayList<String>();
    		list.add("A");
    		list.add("B");
    		// 通过 collections添加元素
    		Collections.addAll(list,"1","2","3");
    		Iterator<String> it1 = list.iterator();
    		System.out.println("输出集合中睥元素:");
    		while(it1.hasNext())
    		{
    			System.out.print(it1.next()+"  ");
    		}
    		//反序集合中的元素顺序
    		Collections.reverse(list);
    		System.out.println("
    对集合反序输出:");
    		Iterator<String> it2 = list.iterator();
    		while(it2.hasNext())
    		{
    			System.out.print(it2.next()+" ");
    		}
    	}
    }
    /*
    输出集合中睥元素:
    A  B  1  2  3
    对集合反序输出:
    3 2 1 B A
    */
    // ===========
    //对集合中的元素进行查找和替换
    import java.util.Collection;
    import java.util.Collections;
    import java.util.List;
    import java.util.ArrayList;
    import java.util.Iterator;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		List<String> list = new ArrayList<String>();
    		list.add("A");
    		list.add("M");
    		Collections.addAll(list,"A","A","D","E","C");
    		Iterator<String> it = list.iterator();
    		System.out.println("排序前:");
    		while(it.hasNext())
    		{
    			System.out.print(it.next()+" ");
    		}
    		// 排序
    		Collections.sort(list);
    		it = list.iterator();
    		System.out.println("
    排序后:");
    		while(it.hasNext())
    		{
    			System.out.print(it.next()+" ");
    		}
    		System.out.println("
    集合中是否存在元素M:"+Collections.binarySearch(list,"M"));
    		System.out.println("集合中是否存在元素U:"+Collections.binarySearch(list,"U"));
    		// 替换元素
    		Collections.replaceAll(list,"A","1");
    		it = list.iterator();
    		System.out.println("替换后:");
    		while(it.hasNext())
    		{
    			System.out.print(it.next()+" ");
    		}
    	}
    }
    /*
    排序前:
    A M A A D E C
    排序后:
    A A A C D E M
    集合中是否存在元素M:6
    集合中是否存在元素U:-8
    替换后:
    1 1 1 C D E M
    */
    
    向量类:Vector类,其实与 ArrayList类差不多,只是早期的
    //验证 Vector类
    import java.util.Iterator;
    import java.util.Vector;
    import java.util.Collections;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		Vector<String> vector = new Vector<String>();
    		vector.add("A");
    		vector.addElement("B");
    		vector.addElement("C");
    		Collections.addAll(vector, "M","N","O");
    		// 删除指定位置的元素
    		vector.remove(1);
    		Iterator<String> it = vector.iterator();
    		System.out.println("使用Iterator");
    		while(it.hasNext())
    		{
    			System.out.print(it.next()+" ");
    		}
    		System.out.println("
    使用用foreach");
    		for(String str:vector)
    		{
    			System.out.print(str+" ");
    		}
    		System.out.println("
    使用get()");
    		int len = vector.size();
    		for(int i=0; i<len; i++)
    		{
    			System.out.print(vector.get(i)+" ");
    		}
    	}
    }
    /*
    使用Iterator
    A C M N O
    使用用foreach
    A C M N O
    使用get()
    A C M N O
    */
    //栈:Stack类
    定义如下:
    public class Stack<E> extends Vector<E>
    常用方法:
    //判断栈是否为空
    public boolean empty()
    
    //获取栈顶元素,但不删除它
    public E peek()
    
    //获取栈顶元素,并删除它
    public E pop()
    
    //将元素添加到栈顶中
    public E push(E item)
    
    //查找指定元素在栈中的位置,起始位置是1
    public int search(Object o)
    
    //验证 Stack类
    import java.util.Iterator;
    import java.util.Stack;
    import java.util.Collections;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		Stack<String> stack = new Stack<String>();
    		stack.push("A");
    		stack.push("B");
    		stack.push("C");
    		Iterator<String> it = stack.iterator();
    		while(it.hasNext())
    		{
    			System.out.print(it.next()+" ");
    		}
    		System.out.println("
    出栈:"+stack.pop());
    		System.out.println("出栈:"+stack.pop());
    	}
    }
    /*
    A B C
    出栈:C
    出栈:B
    */
    //哈希表: Hashtable类
    定义如下:
    public class Hashtable<K,V>
    extends Dictionary<K,V>
    implements Map<K,V>,Cloneable,Serializable
    可以看出 Hashtable类是Map的子类
    
    // 验证 Hashtable类
    import java.util.Set;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Hashtable;
    public class Hi
    {
    	public static void main(String[] args)
    	{
    		Map<String, String> map = new Hashtable<String,String>();
    		map.put("QBC", "第一值");
    		map.put("CBA","第二值");
    		map.put("OPL", "第三值");
    		map.put("YAZ", "第四值");
    		//将 Map集合黑铁为 Set集合
    		Set<Map.Entry<String, String>> set = map.entrySet();
    		Iterator<Map.Entry<String,String>> it = set.iterator();
    		System.out.println("Key ========= Value");
    		while(it.hasNext())
    		{
    			Map.Entry<String,String> mapEntry = it.next();
    			System.out.println(mapEntry.getKey()+" ----------- "+mapEntry.getValue());
    		}
    	}
    }
    /*
    Key ========= Value
    YAZ ----------- 第四值
    QBC ----------- 第一值
    CBA ----------- 第二值
    OPL ----------- 第三值
    */
    

      

  • 相关阅读:
    原创 C++应用程序在Windows下的编译、链接(四)动态链接
    IE浏览器 json异常
    Linux系统github使用
    Mysql in 排序
    转 php四种基础算法:冒泡,选择,插入和快速排序法
    转 mysql取今天,明天,工作日,周末,本周,下周,下月数据
    下载远程图片到本地
    转 PHP中SQL_CALC_FOUND_ROWS与FOUND_ROWS()和count()
    星级点评
    21个值得收藏的Javascript技巧
  • 原文地址:https://www.cnblogs.com/lin3615/p/4333177.html
Copyright © 2020-2023  润新知