day09 Java集合框架

1  java 集合类关系图

      上述类图中,实线边框实现类,比如ArrayList,LinkedList,HashMap等,折线边框抽象类,比如AbstractCollection,AbstractList,AbstractMap等,点线边框接口,比如Collection,Iterator,List等。

      集合类详细内容:http://www.imooc.com/article/1080   

2  集合类关系图List、Set、Map对比

接口

实现类

保持插入顺序

可重复

排序

使用说明

 List

ArrayList

Y

Y

N

擅长于随机访问元素;但插入、删除元素较慢(数组特性)。

LinkedList

Y

Y

N

插入、删除元素较快,但随即访问较慢(链表特性)。

 Set

HashSet

N

N

N

使用散列,最快的获取元素方法。

TreeSet

N

N

Y

将元素存储在红-黑树数据结构中。默认为升序。

LinkedHashSet

Y

N

N

使用散列,同时使用链表来维护元素的插入顺序。

 Map

HashMap

N

N

N

使用散列,提供最快的查找技术。

TreeMap

N

N

Y

默认按照比较结果的升序保存键。

LinkedHashMap

Y

N

N

按照插入顺序保存键,同时使用散列提高查找速度。

   更多对比详细内容:https://blog.csdn.net/hzw05103020/article/details/47207787

3  集合和数组的区别

  数组长度是固定的,集合长度是可变的。数组中可以存储基本数据类型或者引用数据类型,集合只能存储引用数据类型。数组存储的是同一种类型的元素,集合可以存储不同类型的元素。

4  Collection<E>  接口

          public interface Collection<E> extends Iterable<E>{};

                  public interface Iterable<T>{};

   Collection 层次结构中的根接口。Collection 表示一组对象,这些对象也称为Collection 的元素。一些 Collection 允许有重复的元素,而另一些则不允许。一些 Collection 是有序的,而另一些则是无序的。      

方法摘要
 booleanadd(E e)
          确保此 collection 包含指定的元素(可选操作)。
 booleanaddAll(Collection<? extends E> c)
          将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。
 voidclear()
          移除此 collection 中的所有元素(可选操作)。
 booleancontains(Object o)
          如果此 collection 包含指定的元素,则返回 true。
 booleancontainsAll(Collection<?> c)
          如果此 collection 包含指定 collection 中的所有元素,则返回 true。
 booleanequals(Object o)
          比较此 collection 与指定对象是否相等。
 inthashCode()
          返回此 collection 的哈希码值。
 booleanisEmpty()
          如果此 collection 不包含元素,则返回 true。
 Iterator<E>iterator()
          返回在此 collection 的元素上进行迭代的迭代器。
 booleanremove(Object o)
          从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。
 booleanremoveAll(Collection<?> c)
          移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。
 boolean

retainAll(Collection<?> c)
          仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。

          返回值表示此collection 是否发生了改变。

 intsize()
          返回此 collection 中的元素数。
 Object[]toArray()
          返回包含此 collection 中所有元素的数组。

代码示例:

public class CollectionTest {

	public static void main(String[] args) {
		Collection<String> c=new ArrayList<String>();
	    c.add("hello");
	    c.add("world");
	    c.add("java");
	    Object[] objs=c.toArray();
	    for (int i = 0; i < objs.length; i++) {
			System.out.println(objs[i]);
			String s=(String)objs[i];
			System.out.println(s+"---"+s.length());
		}
	    Collection<Student> c1=new ArrayList<Student>();
		Student s1=new Student("name1",18);
		Student s2=new Student("name2",19);
		Student s3=new Student("name3",20);
		Student s4=new Student("name4",21);
		Student s5=new Student("name5",22);
		c1.add(s1);
		c1.add(s2);
		c1.add(s3);
		c1.add(s4);
		c1.add(s5);
	    /*public Iterator<E> iterator() {
                return new Itr();
             }
             private class Itr implements Iterator<E> {}
             */
		Iterator<Student> it=c1.iterator();
		while(it.hasNext()) {
			//System.out.println(it.next());
			Student s=(Student)it.next();
			System.out.println(s.getName()+"---"+s.getAge());
		}
		
		for (Iterator<Student> iterator = c1.iterator(); iterator.hasNext();) {
			Student ss = (Student) iterator.next();
			System.out.println(ss.getName()+"---"+ss.getAge());
		}
	}

}

5  List<E>  接口

            public interface List<E> extends Collection<E>

            有序的 collection(存储顺序和取出顺序一致),元素可重复。

public class ListTest {

	public static void main(String[] args) {
		
	    List<Student> list=new ArrayList<Student>();
		Student s1=new Student("name1",18);
		Student s2=new Student("name2",19);
		Student s3=new Student("name3",20);
		Student s4=new Student("name4",21);
		Student s5=new Student("name5",22);
		list.add(s1);
		list.add(s2);
		list.add(s3);
		list.add(s4);
		list.add(s5);
		Iterator<Student> it=list.iterator();
		while(it.hasNext()) {
			//System.out.println(it.next());
			Student s=(Student)it.next();
			//System.out.println(s.getName()+"---"+s.getAge());
		}
		
		for (Iterator<Student> iterator = list.iterator(); iterator.hasNext();) {
			Student s = (Student) iterator.next();
			//System.out.println(s.getName()+"---"+s.getAge());
		}
		
		for (int i = 0; i < list.size(); i++) {
			Student s=(Student)list.get(i);
			//System.out.println(s.getName()+"---"+s.getAge());
		}
		
		for(Student s:list) {
			//System.out.println(s.getName()+"---"+s.getAge());
		}
		
		/*public ListIterator<E> listIterator() {
	        return new ListItr(0);
	    }
		private class ListItr extends Itr implements ListIterator<E>{}*/
		ListIterator<Student> lit=list.listIterator();
		while(lit.hasNext()) {
			//System.out.println(it.next());
			Student s=(Student)lit.next();
			//System.out.println(s.getName()+"---"+s.getAge());
		}
		while(lit.hasPrevious()) {
			Student s=(Student)lit.previous();
			//System.out.println(s.getName()+"---"+s.getAge());
		}
		
		List<String> list1=new ArrayList<String>();
	    list1.add("hello");
	    list1.add("world");
	    list1.add("java");
//	    Iterator<String> it1=list1.iterator();
//	    while(it1.hasNext()) {
//	    	//Exception in thread "main" java.util.ConcurrentModificationException
//			String s=(String)it1.next();
//			if("world".equals(s)) {
//				list1.add("android");
//			}
//		}
//	    System.out.println(list1);
	    ListIterator<String> it1=list1.listIterator();
	    while(it1.hasNext()) {
			String s=(String)it1.next();
			if("world".equals(s)) {
				it1.add("android");
			}
		}
	    System.out.println(list1);//[hello, world, android, java]
	    
	    for (int i = 0; i <list1.size(); i++) {
	    	String s=(String)list1.get(i);
			if("world".equals(s)) {
				list1.add("android");
			}
		}
	    System.out.println(list1);//[hello, world, java, android]
	}

}

6  常见的数据结构

          、队列、数组、链表、树、哈希表。  

         数据结构(数组和链表)

 

      List的相关子类特点

           ArrayList:
                 底层数据结构是数组,查询快,增删慢。
                 线程不安全,效率高。
           Vector:
                 底层数据结构是数组,查询快,增删慢。
                 线程安全,效率低。
           LinkedList:
                 底层数据结构是链表,查询慢,增删快。
                 线程不安全,效率高。

7 ArrayList<E>类

        public class ArrayList<E> extends AbstractList<E>

         implements List<E>, RandomAccess, Cloneable, java.io.Serializable{};


                      public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {};

                                public abstract class AbstractCollection<E> implements Collection<E> {};

        ArrayList的相关方法

public class ArrayListTest {

	public static void main(String[] args) {
		ArrayList<String> array=new ArrayList<String>();
		array.add("hello");
		array.add("world");
		array.add("java");
		array.add("android");
		Iterator<String> it=array.iterator();
		while(it.hasNext()) {
			String s=(String)it.next();
			System.out.println(s);
		}
		
		for (int i = 0; i < array.size(); i++) {
			String s=(String)array.get(i);
			System.out.println(s);
		}
		
		ArrayList<Student> list=new ArrayList<Student>();
		Student s1=new Student("name1",18);
		Student s2=new Student("name2",19);
		Student s3=new Student("name3",20);
		Student s4=new Student("name4",21);
		Student s5=new Student("name5",22);
		list.add(s1);
		list.add(s2);
		list.add(s3);
		list.add(s4);
		list.add(s5);
		Iterator<Student> its=list.iterator();
		while(its.hasNext()) {
			Student s=(Student)its.next();
			System.out.println(s.getName()+"---"+s.getAge());
		}
		
		for (Iterator<Student> iterator = list.iterator(); iterator.hasNext();) {
			Student s = (Student) iterator.next();
			System.out.println(s.getName()+"---"+s.getAge());
		}
		
		for (int i = 0; i < list.size(); i++) {
			Student s=(Student)list.get(i);
			System.out.println(s.getName()+"---"+s.getAge());
		}
		
		for(Student s:list) {
			System.out.println(s.getName()+"---"+s.getAge());
		}

	}

}

       ArrayList的简单应用(元素去重和删除指定元素)

public class ArrayListTest {

	public static void main(String[] args) {
		ArrayList<String> array=new ArrayList<String>();
		array.add("hello");
		array.add("world");
		array.add("world");
		array.add("world");
		array.add("world");
		array.add("world");
		array.add("java");
		array.add("android");
		array.add("world");
		array.add("java");
		array.add("android");
		//dedupLicationOriginal(array);
		//dedupLicationNew(array);
		//deleteElement(array,"world");
		getNumber();
	}

	/**
	 * @descripe 产生10个1-20之间不重复的随机数
	 */
	private static void getNumber() {
		int count=0;
		ArrayList<Integer>list=new ArrayList<Integer>();
		while(count<10) {
			int number=new Random().nextInt(20)+1;
			if(!list.contains(number)) {
				list.add(number);
				count++;
			}
		}
		System.out.println(list);
	}

	/**
	 * @param list 目标集合
	 * @param target 需要删除的元素
	 * @descripe 删除集合中的某个元素
	 */
	private static void deleteElement(ArrayList<String> list, String target) {
		
		//普通for循环
		for (int i = list.size()-1; i >=0; i--) {
			String s=list.get(i);
			if(target.equals(s)) {
				list.remove(i);
			}
		}
		//迭代器
		Iterator<String> it = list.iterator();
		while(it.hasNext()) {
			String s=it.next();
			if(target.equals(s)) {
				it.remove();
			}
		}
		// 利用CopyOnWriteArrayList
		final CopyOnWriteArrayList<String> cowList=new CopyOnWriteArrayList<String>(list);
		for(String s:cowList) {
			if(target.equals(s)) {
				cowList.remove(s);
			}
		}
		
		System.out.println(list);
	}

	/**
	 * @param array 需要去重的集合
	 * @descripe:在原有集合操作去重
	 */
	private static void dedupLicationOriginal(ArrayList<String> list) {
		
		for (int i = 0; i < list.size()-1; i++) {
			for (int j = i+1; j < list.size(); j++) {
				if(list.get(i).equals(list.get(j))) {
					list.remove(j);
					j--;//集合连续多个重复元素删除后会有替位问题
				}
				
			}
		}
		System.out.println(list);
	}
    
	/**
	 * @param array 需要去重的集合
	 * @descripe:创建新集合去重
	 */
	private static void dedupLicationNew(ArrayList<String> list) {
		HashSet<String> set = new HashSet<String>(); 
		ArrayList<String> newarray=new ArrayList<String>();
		// 增强for循环
		for(String s:list) {
			if(!newarray.contains(s)) {
				newarray.add(s);	
			}
		}
		// 普通for循环
		for (int i = 0; i < list.size(); i++) {
			String s=list.get(i);
			if(!newarray.contains(s)) {
				newarray.add(s);	
			}
		}
		//迭代器
		Iterator<String> it=list.iterator();
		while(it.hasNext()) {
			String s=it.next();
			if(!newarray.contains(s)) {
				newarray.add(s);	
			}
		}
		// Hashset集合去重
		for(String s:list) {
			if(set.add(s)) {
				newarray.add(s);	
			}
		}
		System.out.println(newarray);
	}

}

   ArrayList 源码分析:https://www.kancloud.cn/swg2018/java_collection/542303

                                     https://blog.csdn.net/zxt0601/article/details/77281231

8 Vector<E>类

            public class Vector<E>  extends AbstractList<E>
            implements List<E>, RandomAccess, Cloneable, java.io.Serializable{};

public class VectorTest {

	public static void main(String[] args) {
		Vector<String> vec=new Vector<String>();
		vec.addElement("hello");
		vec.addElement("world");
		vec.addElement("java");
		vec.addElement("android");
        for (int i = 0; i < vec.size(); i++) {
			String s=(String)vec.elementAt(i);
			System.out.println(s);
		}
        Enumeration<String> en=vec.elements();
        while(en.hasMoreElements()) {
        	String s=(String)en.nextElement();
        	System.out.println(s);
        }
        
        
	}
}

8 LinkedList<E>类

         public class LinkedList<E>extends AbstractSequentialList<E>
         implements List<E>, Deque<E>, Cloneable, java.io.Serializable{};

                public abstract class AbstractSequentialList<E> extends AbstractList<E>{};

                public interface Deque<E> extends Queue<E>{};

                       public interface Queue<E> extends Collection<E>{};

public class LinkedListTest {

	public static void main(String[] args) {
		MyStack ms=new MyStack();
		ms.add("hello");
		ms.add("world");
		ms.add("java");
		ms.add("android");
		while(!ms.isEmpty()) {
			System.out.println(ms.get());// android java world hello
		}
	}

}

class MyStack{
	private LinkedList<Object> link;
	public MyStack() {
		link=new LinkedList<Object>();
	}
	public void add(Object obj) {
		link.addFirst(obj);
	}
	
	public Object get() {
		return link.removeFirst();
	}
	
	public boolean isEmpty() {
		return link.isEmpty();
	}
}

9 泛型以及泛型高级(通配符

          JDK5 之后出现的机制。JDK5常见新特性:自动拆装箱,泛型,增强for循环,静态导入,可变参数,枚举。

          泛型类

                •把泛型定义在类上

                •格式:public class 类名<泛型类型1,…>

                •注意:泛型类型必须是引用类型

         型方法

                •把泛型定义在方法上

                •格式:public <泛型类型> 返回类型 方法名(泛型类型 .)

         泛型接口

                •把泛型定义在接口上

                •格式:public  interface 接口名<泛型类型1…>

/*
 * 泛型高级(通配符)
 * ?:任意类型,如果没有明确,那么就是Object以及任意的Java类
 * ? extends E:向下限定,E及其子类
 * ? super E:向上限定,E极其父类
 */
public class GenericDemo {
	public static void main(String[] args) {
		// 泛型如果明确的写的时候,前后必须一致
		Collection<Object> c1 = new ArrayList<Object>();
		// Collection<Object> c2 = new ArrayList<Animal>();
		// Collection<Object> c3 = new ArrayList<Dog>();
		// Collection<Object> c4 = new ArrayList<Cat>();

		// ?表示任意的类型都可以
		Collection<?> c5 = new ArrayList<Object>();
		Collection<?> c6 = new ArrayList<Animal>();
		Collection<?> c7 = new ArrayList<Dog>();
		Collection<?> c8 = new ArrayList<Cat>();

		// ? extends E:向下限定,E及其子类
		// Collection<? extends Animal> c9 = new ArrayList<Object>();
		Collection<? extends Animal> c10 = new ArrayList<Animal>();
		Collection<? extends Animal> c11 = new ArrayList<Dog>();
		Collection<? extends Animal> c12 = new ArrayList<Cat>();

		// ? super E:向上限定,E极其父类
		Collection<? super Animal> c13 = new ArrayList<Object>();
		Collection<? super Animal> c14 = new ArrayList<Animal>();
		// Collection<? super Animal> c15 = new ArrayList<Dog>();
		// Collection<? super Animal> c16 = new ArrayList<Cat>();
	}
}

class Animal {
}

class Dog extends Animal {
}

class Cat extends Animal {
}

10 可变参数

           JDK5 之后出现的机制。

/*
 * 可变参数:定义方法的时候不知道该定义多少个参数
 * 格式:
 * 		修饰符 返回值类型 方法名(数据类型…  变量名){
 * 
 * 		}
 * 
 * 		注意:
 * 			这里的变量其实是一个数组
 * 			如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个
 * 
 *public static <T> List<T> asList(T... a):把数组转成集合 
 * 注意事项:
 * 		虽然可以把数组转成集合,但是集合的长度不能改变。
 * 
 */	
public class ArgsDemo {

	public static void main(String[] args) {
		
		int a = 10;
		int b = 20;
		int result = sum(a, b);
		System.out.println("result:" + result);

		
		int c = 30;
		result = sum(a, b, c);
		System.out.println("result:" + result);

		
		int d = 30;
		result = sum(a, b, c, d);
		System.out.println("result:" + result);

	
		result = sum(a, b, c, d, 40);
		System.out.println("result:" + result);

		result = sum(a, b, c, d, 40, 50);
		System.out.println("result:" + result);
		
		// 定义一个数组
		// String[] strArray = { "hello", "world", "java" };
		// List<String> list = Arrays.asList(strArray);

		List<String> list = Arrays.asList("hello", "world", "java");
		// UnsupportedOperationException
		// list.add("javaee");
		// UnsupportedOperationException
		// list.remove(1);
		list.set(1, "javaee");

		for (String s : list) {
			System.out.println(s);
		}
	}

	public static int sum(int... a) {
		// System.out.println(a);
		//return 0;

		int s = 0;
		
		for(int x : a){
			s +=x;
		}
		
		return s;
	}

	// public static int sum(int a, int b, int c, int d) {
	// return a + b + c + d;
	// }
	//
	// public static int sum(int a, int b, int c) {
	// return a + b + c;
	// }
	//
	// public static int sum(int a, int b) {
	// return a + b;
	// }
}

10  Set<E> 接口

            public interface Set<E> extends Collection<E>{};

             一个不包含重复元素的 collection。无序(存储顺序和取出顺序不一致),元素唯一。

11  HashSet<E>类

            public class HashSet<E> extends AbstractSet<E>
            implements Set<E>, Cloneable, java.io.Serializable{};

                    public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {};

                     

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 作为一名程序员,我在2023对自己的计划包括: 1. 不断学习和提升技能:我会继续学习新的编程语言和框架,以及不断提高编码能力。 2. 参加行业相关的培训和会议:我会参加各种行业相关的培训课程和会议,以便获得最新的行业知识和技能。 3. 参加项目并积累经验:我会参加各种项目,以便积累丰富的经验,并提升个人能力。 4. 在业内建立良好的人脉关系:我会努力与业内的同行建立良好的人脉关系,并与他们保持联系。 5. 寻找更好的发展机会:我会不断寻找更好的发展机会,以便在职业生涯中取得更大的成功。 总之,我在2023会努力工作,不断学习和提升,并寻找更好的发展机会,以在职业生涯中取得更大的成功。 ### 回答2: 2023的计划对于我作为一名程序员来说十分重要,我希望能够提升自己的技能水平并取得更好的职业发展。以下是我的计划: 首先,我计划继续学习并掌握新的编程语言和技术。随着科技的不断发展,编程语言和技术也在不断更新换代,作为一名程序员,保持学习的状态是必不可少的。我将花费更多的时间学习目前主流的编程语言,如Python、Java等,并尝试掌握最新的前端和后端技术。通过不断学习,我希望能够拥有更广泛的技术视野和更强大的技术能力。 其次,我计划参与更多的项目和实践。在理论学习之外,实践是提升技能的关键。我计划积极寻找项目机会,无论是个人项目还是团队项目,都可以提供宝贵的实践机会。通过参与各种项目,我可以锻炼解决问题的能力,提高编码和协作能力。同时,我也希望通过实践中的挑战和失败,不断完善自己,进一步提高自己的技术水平和经验。 第三,我计划参加相关的培训和技术交流活动。参加培训和技术交流活动可以与其他程序员交流和学习,了解行业最新动态和趋势。我计划参加各种技术研讨会、讲座和培训班,通过与行业专家和其他程序员的交流,深入了解各种编程技术和最佳实践。同时,我也希望能够积极参与技术社区,与其他程序员分享自己的经验和见解,不断提高自己的影响力和口碑。 最后,我计划在个人项目和开源社区上做出更多的贡献。通过自己的努力,我希望能够在个人项目中实现一些有意义的功能或解决一些实际问题,并将其开源。通过开源社区的贡献,我可以帮助他人解决问题,同时也能够借助其他人的反馈和指导,不断改进自己的代码和设计能力。 总之,2023对于我作为一名程序员来说是充满挑战和机遇的一。我将不懈努力,持续学习和实践,不断提升自己的技能水平和职业发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值