JavaSE- 集合框架-day17-14 LinkedList-实现堆栈-队列-HashSet-TreeSet

    xiaoxiao2022-07-07  183

    Arralist:

    可调整大小的数组的实现List接口。 实现所有可选列表操作,并允许所有元素,包括null 。 除了实现List 接口之外,该类还提供了一些方法来操纵内部使用的存储列表的数组的大小。 (这个类是大致相当于Vector,不同之处在于它是不同步的)。

     ArrayList 存自定义对象:

    package collection; public class Person{ private String name; private int age; Person(String name, int age){ this.name=name; this.age=age; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public String getName() { return name; }} package collection; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ArrayListDemo { public static void main(String[] args) { List<Person > list =new ArrayList<Person>() ; list.add(new Person("aaaaa",12)); list.add(new Person("bbbbb",35)); list.add(new Person("ccccc",55)); list.add(new Person("ddddd",66)); Iterator iterator = list.iterator(); while(iterator.hasNext()){ Person person = (Person) iterator.next(); System.out.println( person.getName()+" ---"+person.getAge()); } }

    HashSet

    set的实现不包含重复元素(set不包含满足 e1.equals(e2))。最多包含一个null 元素。内部是哈希表不同步。接口与Collection 一致。 package collection; import java.util.HashSet; import java.util.Iterator; public class HashSetDemo { public static void main(String[] args) { HashSet hashSet =new HashSet(); hashSet.add("123"); hashSet.add("2233"); hashSet.add("2332"); Iterator iterator =hashSet.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } } }

    哈希表

    1.哈希表确定元素是哈希值(hashCode 方法)否相同,如果相同判断内容(equals 方法)是否相同。如果哈希值不同,是不需要判断equals.

    TreeSet

    1.TreeSet 使用二叉树都会原理 对新add 的对象按照指定顺序(升序、降序),每增加一个对象 都会进行排序。对象插入到二叉树指定位置。 2. Integer和String  都可以进行默认的TreeSet排序,而自定的对象不可以,需要自定义的类实现Comparable接口, 并且覆盖相应的compareTo()函数,才可以正常使用。 3.在覆写comapare()函数时,要相应的值才能使得TreeSet按照一定的规律来排序(升序,this 对象<指定对象条件下返回-1), 降序,this .对象> 指定的条件下返回-1 )升序是:比较此对象与指定对象的顺序。如果该对象小于、等于或者大于指定对象,则分别返回负数整数,零或者整

    package day18; import java.io.StringReader; import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; /** * Created by zengjx on 2019/5/23. */ class Person implements Comparable { public String name; public int age; Person(String name,int age){ this.age=age; this.name=name; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public String getName() { return name; } @Override public int compareTo(Object o) { Person per=(Person)o; //升序 比较对象与指定对象的顺序,如果该对象小于 等于或者大于指定对象分别返回负整数,零,正整数。 // return this.age<per.age? -1: (this.age== per.age )? 0: 1; //降序排列 return this.age <per.age ?1:(this.age==per.age )? 0:-1; } } public class TreeDemo1 { public static void main(String[] args){ TreeSet treeSet =new TreeSet(); treeSet.add(new Person("川普1",1)); treeSet.add(new Person("川普2",2)); treeSet.add(new Person("川普3",3)); treeSet.add(new Person("川普4",4)); Iterator iterator =treeSet.iterator(); while(iterator.hasNext()){ Person per=(Person)iterator.next(); System.out.println(" "+per.name+" "+per.age); } } }

    输出结果:降序    

    川普4 4      川普3 3  川普2 2  川普1 1

    升序:

     川普1 1  川普2 2  川普3 3  川普4 4

     

     

    1,用linkedlist模拟堆栈和队列? //堆栈 :先进后出First In Last Out FILO //队列: 先进先出First In First Out FIFO package collection; import java.util.Iterator; import java.util.LinkedList; import java.util.NoSuchElementException; public class LinkDemo1 { private LinkedList<String > linkedList =new LinkedList<String >(); public static LinkDemo1 linkDemo1 =new LinkDemo1(); private LinkDemo1(){ } public boolean put(String s){ linkedList.addFirst(s);//头部插入 return true; } public String pop(){ Iterator iterator = linkedList.iterator(); while (!linkedList.isEmpty()) { // String temp=linkedList.getFirst();//头部取出 不删除这个元素 String temp= linkedList.removeFirst();//头部取出这个元素,并且删除这个元素。 return temp; } return null; } public static LinkDemo1 getInstance(){ return linkDemo1; } public void get(){ try{ linkedList.getFirst(); }catch (NoSuchElementException e){ System.out.println(" 列表为空"); } } }

      user:

    public static void listdemo2(){ LinkDemo1 linkDemo1 =LinkDemo1.getInstance(); linkDemo1.get(); linkDemo1.put("1"); linkDemo1.put("2"); linkDemo1.put("3"); System.out.println(linkDemo1.pop()); System.out.println(linkDemo1.pop()); System.out.println(linkDemo1.pop()); }

    队列:

    package collection; import java.util.LinkedList; public class LinkDemo2 { private static LinkedList linkedList =new LinkedList<String >(); public static LinkDemo2 linkDemo2 =new LinkDemo2() ; private LinkDemo2 (){ } public static LinkDemo2 getInstance(){ return linkDemo2; } public String pop(){//队列删除元素 String temp=null; if(!linkedList.isEmpty()){ temp =(String )linkedList.pop(); } return temp; } public boolean put(String s){//入队 boolean ret=false; if(linkedList!=null){ linkedList.addFirst(s); ret=true; } return ret; } public void show(){ } } public static void listdemo3(){ LinkDemo2 linkDemo2 =LinkDemo2.getInstance(); linkDemo2.put("1111"); linkDemo2.put("2222"); linkDemo2.put("33333"); System.out.println(linkDemo2.pop()); System.out.println(linkDemo2.pop()); System.out.println(linkDemo2.pop()); }

     

     

    2. HashSet结构是什么?该结构有什么特点?

    哈希表

    3. HashSet是如何保证元素唯一性的?

    通过 对象hashCode,equals 方法来完成对象的唯一性。

    如果对象的hashCode 不一样,则不用判断equals,直接存储到哈希表中。

    如果对象的hashCode 值相同,那么要再次判断对象的equals方法是否为true.

    如果为true 则视为相同元素,不存。

    如果元素存储到HashSet集合中,必须覆盖hashCode方法和equals 方法。

    一般,如果定义的类产生很多对象,比如人,学生,书通常都需要equals ,hashCode 方法。

    建立对判断是否相同的依据。

    4.TreeSet结构及其特点?

    基于TreeMap的Navigable实现,使用元素的自然顺序对元素进行排列。不同步。

    此操作最好在创建时进行,以防止对 set 的意外非同步访问:

    SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));

    5.Tree 如何保证元素唯一性?

    6.TreeSet两种排序方式,有什么区别?

    7.查阅集合的技巧?

    8. 泛型的理解?

    9.集合涉及的代码-狂敲!

     

     

     

    最新回复(0)