java之14天 集合 (二)
LinkedList的使用
- /**
- * LinkedList:特有方法
- *
- * addFirst()
- * addLast()
- *
- * getFist()
- * getLast()
- * 获取元素 但不删除元素 ,如果集合中没有元素,会出现 NoSuchException
- *
- * removeFirst()
- * removeLast()
- * 也可以获取元素,但是元素会被删除,如果集合中没有元素,会出现,NoSuchException
- * JDK1.6 后出现了 替代方法了
- * 添加
- * offerFirst()
- * offerLast()
- *
- * //获取
- * peekFirst()
- * peekLast()
- * 获取元素 但不删除元素 ,如果集合中没有元素,会返回 null
- *
- * //移除
- * pollFirst()
- * pollLast()
- * 也可以获取元素,但是元素会被删除,如果集合中没有元素 会返回null
- */
- public class LinkedListDemo {
- public static void main(String[] args) {
- LinkedList link=new LinkedList();
- link.addFirst(“java01”);
- link.addLast(“java02”);
- link.addLast(“java03”);
- link.addFirst(“java04”);
- sop(link);
- //获取第一个 或者 最后一个
- sop(link.getFirst());
- sop(link.getLast());
- //获取 大小
- sop(“size=”+link.size());
- //移除第一个
- sop(“remove=”+link.removeFirst());
- sop(link);
- while(!link.isEmpty()){
- sop(link.removeFirst());
- }
- }
- public static void sop(Object obj){
- System.out.println(obj);
- }
- }
LinkedList的练习
- /**
- * 使用LinkedList模拟堆栈 或者队列数据结构
- * 堆栈: 先进后出 (FILO:First in Last out) 如同一个杯子
- * 队列: 先进先出 (FIFO:First in First out) 如果一个水管
- *
- */
- class DuiLie{
- private LinkedList link;
- public DuiLie(){
- this.link=new LinkedList();
- }
- public void myAdd(Object obj){
- link.addFirst(obj);
- }
- public Object myGet(){
- //return link.removeLast(); //队列
- return link.removeFirst(); //堆栈
- }
- public boolean isNull(){
- return link.isEmpty();
- }
- }
- public class LinkedListTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- DuiLie dl=new DuiLie();
- dl.myAdd(“java01”);
- dl.myAdd(“java02”);
- dl.myAdd(“java03”);
- dl.myAdd(“java04”);
- while(!dl.isNull())
- System.out.println(dl.myGet());
- }
- }
HashSet的使用
- /**
- *|–Set: 元素是无序的(存入和取出 的顺序不一定一致),元素不可以重复.
- * |–HashSet: 底层数据结构是哈希表
- * hashSet是如何保证元素唯一性呢?
- * 是通过元素的两个方法, hashCode 和 equals 来完成的
- * 如果元素的hashCode值相同,才会判断equals 是否为true
- * 如果元素的hashCode值不同,不会调用 equals.
- *
- * 注意: 对于判断元素是否存在,以及删除等操作,依赖的方法是元素的hashCode和equals方法.
- *
- *
- * |–TreeSet:
- *
- *
- */
- class Student{
- private String name;
- private int age;
- public Student(String name,int age){
- this.name = name;
- this.age = age;
- }
- public int hashCode(){
- System.out.println(this.name);
- return 7*name.hashCode()+13*age;
- }
- @Override
- public boolean equals(Object obj) {
- // TODO Auto-generated method stub
- if(!(obj instanceof Student))
- return false;
- Student s=(Student)obj;
- System.out.println(this.name+”..equals..”+s.name);
- return this.name.equals(s.name) && this.age==s.age;
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
- }
- public class HashSetDemo {
- public static void sop(Object obj){
- System.out.println(obj);
- }
- public static void main(String[] args) {
- HashSet hs=new HashSet();
- hs.add(new Student(“a1”,11));
- hs.add(new Student(“a2”,12));
- hs.add(new Student(“a3”,13));
- //重复的没有被添加进去, 先算hashCode 使用equals 比较
- hs.add(new Student(“a2”,12));
- hs.remove(new Student(“a2”,13));
- hs.remove(new Student(“a4”,14));
- sop(hs.contains(new Student(“a1”,11)));
- Iterator it=hs.iterator();
- while(it.hasNext()){
- Student s=(Student)it.next();
- sop(s.getName()+”….”+s.getAge());
- }
- }
- public static void method(){
- Student stu1 = new Student(“aa”,17);
- Student stu2 = new Student(“aa”,17);
- System.out.println(stu1.hashCode());
- System.out.println(stu2.hashCode());
- System.out.println(stu1 == stu2);
- HashMap<Student,String> hm=new HashMap<Student,String>();
- hm.put(stu1, “aa”);
- hm.put(stu2, “bb”);
- Set<Student> ss=hm.keySet();
- Collection<String> cv=hm.values();
- for (Student student : ss) {
- sop(student);
- }
- for (String str : cv) {
- sop(str);
- }
- }
- }