我是靠谱客的博主 耍酷小丸子,这篇文章主要介绍Java进阶 -- Set集合和泛型,现在分享给大家,希望可以做个参考。

HashSet和TreeSet集合

  • Set单列集合接口特征:
  1. 无序:存和取的顺序不一定一致。
  2. 不可以存储重复元素
  3. 没有索引,不能使用fori遍历,只能使用增强for循环遍历。
    hash值的作用:默认前情况下用来表示对象在内存中的地址,一般情况下不同的对象hash值是不一样的,所以我们常说地址值不一样。除非重写了hashCode()方法hash值才可能一。
  • HashSet集合

    1. 特点:
      底层使用哈希表结构(数组中存放链表)。
      Set集合中元素唯一的条件.
  • LinkedHasSet集合
    底层原理:哈希表+链表
    链表保证集合元素有序,是指元素的存取有序
    哈希表保证集合元素唯一

  • TreeSet集合
    特点:元素有序,不是指元素在存取过程中有序,是指元素在存入集合中,会进行排序,有一定的顺序。
    排序的条件:条件二选一
    1、TreeSet集合中的元素实现Comparable自然排序的接口,定义排序规则。
    2、元素不需要实现Comparable接口,但是在创建TreeSet集合对象的时候传递一个Comparator比较器对象

  • Comparable和Comparator区别?
    Comparable自然排序的接口,需要元素对象去实现这个接口。
    Comparator比较器接口,给TreeSet对象使用。

集合类型特点
共同点无序、不能存储重复着、没有索引。
HashSet底层是哈希表结构。元素唯一的条件:元素要重写hashCode()方法和equals()方法。
LinkedHashSet底层是链表+哈希表结构 。元素有序:存和取的顺序一致。元素唯一的条件:元素要重写hashCode()方法和equals()方法。
TreeSet元素默认自然排序

泛型

概念:将固定的数据类型参数化,也叫做参数化类型。
好处:
1、将运行期的异常提前到了编译器,保证存储的数据的正确性。
2、避免了强制类型转换。

  • 泛型的分类:
    泛型类:
    第一步:在类名后面定义泛型,那么这个类的任何地方都可以使用泛型。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class GenericClass<T> { //data只能保存String类型的数据 //private String data; /* 需求:需要data能保存任意类型的数据 */ private T data; public T getData() { return data; } public void setData(T data) { this.data = data; } }

第二步:在创建对象的时候确定泛型

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
public static void main(String[] args) { //创建GenericClass对象,并确定泛型 GenericClass<String> gc1 = new GenericClass<>(); gc1.setData("java"); System.out.println(gc1.getData()); //存储Student类型 GenericClass<Student> gc2 = new GenericClass<>(); gc2.setData(new Student("真皮沙发", 22)); System.out.println(gc2.getData()); }
  • 泛型接口:
    第一步:在接口名后面定义泛型,接口中的任何地方都可以使用泛型
复制代码
1
2
3
4
5
public interface GenericInterface<T>{ //在抽象方法中使用泛型 public T method(T t); }

第二步:为接口定义实现类,和泛型类的定义一样

复制代码
1
2
3
4
5
6
public class GenericInterfaceImpl<T> implements GenericInterface<T>{ public T method(T t){ } }

第三步:创建实现类对象的时候确定泛型
GenericInterface<String> gif=new GenericInterfaceImpl<String>();

  • 泛型方法:
    第一步:在方法的的修饰符后面定义泛型,在方法的任意位置(返回值、参数列表,方法体中)都可以使用泛型。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
public class GenericMethod { //定义一个泛型方法 public static <E> E show(E e){ //判断e是否是String类型 if(e instanceof String){ String s= (String) e; s="hello "+s; return (E) s; } return e; } }

第二步:在调用方法的时候通过传递的参数值确定泛型的类型

复制代码
1
2
3
4
5
6
7
8
9
10
11
public static void main(String[] args) { //创建对象 GenericMethod gm=new GenericMethod(); //调用show方法 String s = gm.show("java"); System.out.println(s); Student stu = gm.show(new Student("zhangsan", 20)); System.out.println(stu); }
  • 通配符泛型:<?>表示泛型的父类,一般应用在方法参数列表中
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class GenericTest { public static void main(String[] args) { //创建集合对象 List<Number> list1=new ArrayList<Number>(); //调用method方法 method(list1); //创建集合对象 /*List<Integer> list2=new ArrayList<Integer>(); //调用method方法 method(list2);*/ //创建集合对象 List<Object> list3=new ArrayList<Object>(); //调用method方法 method(list3); } /* Number和Integer是子父类关系,但是装有Number的list集合和装有Integer的list集合不是子父类关系 List<? extends Number>:定义通配符?的上限,最大类型只能是Number类型,所以传递List<Object>报错 List<? super Number>:定义通配符?的下限,最小类型只能是Number类型,所以传递List<Integer>报错 */ /*public static void method(List<? extends Number> list){ System.out.println(list); }*/ public static void method(List<? super Number> list){ System.out.println(list); } }

最后

以上就是耍酷小丸子最近收集整理的关于Java进阶 -- Set集合和泛型的全部内容,更多相关Java进阶内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(77)

评论列表共有 0 条评论

立即
投稿
返回
顶部