java基础_集合List与Set接口

  1. List接口继承了Collection的方法  当然也有自己特有的方法向指定位置添加元素   add(索引,添加的元素);
  2. 移除指定索引的元素   remove(索引)
  3. 修改指定索引的元素   set(索引,修改的元素)
  4. package com.List集合;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Demo01 {
    
        public static void main(String[] args) {
            /*
             * add(索引,插入的元素)方法
             */
            List<String> list = new ArrayList<>();
            list.add("a");
            list.add("b");
            list.add("c");
            list.add(1, "q");
            System.out.println(list);
            
            /*
             * 元素修改set(索引,修改的元素)
             */
            list.set(0, "w");
            System.out.println(list);
            
            list.remove(0);
            System.out.println(list);
            list.remove("b");
            System.out.println(list);
            
        }
    
    }
  5. ArrayList集合  在这里我就不多介绍了   (查找元素快,增删慢)
  6. LinkedList集合  (查找元素慢,增删快)
  • LinkedList集合几个方法需要掌握
  • addFrist()  在集合的首部添加元素
  • addLast()  在集合的尾部添加元素
  • removeFirst() 移除集合首部的元素
  • removeLast() 移除集合尾部的元素
  • getFrist() 获取集合首部的元素
  • getLast() 获取集合尾部的元素
  • isEmpty() 判断集合是否为空
  • 		LinkedList<String> linkedList = new LinkedList<>();
    		linkedList.add("a");
    		linkedList.add("a");
    		linkedList.add("b");
    		linkedList.add("a");
    		
    		linkedList.addFirst("0");
    		linkedList.addLast("9");
    		System.out.println(linkedList);
    		
    		
    		linkedList.removeLast();
    		System.out.println(linkedList);
    		linkedList.removeFirst();
    		System.out.println(linkedList);
    		
    		String first = linkedList.getFirst();
    		System.out.println(first);
    		String last = linkedList.getLast();
    		System.out.println(last);
    		
    		boolean b = linkedList.isEmpty();
    		System.out.println(b);
    
  •  
  • Set接口(无序,不重复的)
  • 我们直接学习HashSet接口
  • HashSet当你存储相同元素时,是添加不进去的 他会判断集合中是否存在,这里我们用自定义的引用类型说明
  • package com.Set集合;
    
    public class Person {
        private String name;
        private int age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public Person(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
        public Person() {
            super();
            // TODO Auto-generated constructor stub
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + age;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Person other = (Person) obj;
            if (age != other.age)
                return false;
            if (name == null) {
                if (other.name != null)
                    return false;
            } else if (!name.equals(other.name))
                return false;
            return true;
        }
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + "]";
        }
        
    }

    这里注意我们需要重写HashClod方法与equlas()方法  当然在打印的时候需要重写toString()方法

  •     public static void main(String[] args) {
            HashSet<Person> hashSet = new HashSet<>();
            hashSet.add(new Person("张三",12));
            hashSet.add(new Person("李四",12));
            hashSet.add(new Person("张三",12));
            hashSet.add(new Person("张三",21));
    //        System.out.println(hashSet);
            Iterator<Person> iterator =hashSet.iterator();
            while(iterator.hasNext()){
                Person next = iterator.next();
                System.out.println(next);
            }
        }
    }
    //执行结果为:张三 12
    李四 12
    张三 21
  • 这就是HashSet的去重复元素的功能  结果是无序的
  • Set接口下还有一个LinkedHashSet集合,这是弥补了Set无序的缺点
  • public class Demo02_LinkedHashSet {
        public static void main(String[] args) {
            LinkedHashSet<String> hashSet = new LinkedHashSet<>();
            hashSet.add("a");
            hashSet.add("b");
            hashSet.add("c");
            hashSet.add("d");
            //增强for遍历
            for (String string : hashSet) {
                System.out.print(string+" ");
            }
           //迭代器遍历 
            Iterator<String> iterator = hashSet.iterator();
            while(iterator.hasNext()){
                String str = iterator.next();
                System.out.println(str);
            }
            
            System.out.println(hashSet);
        }
原文地址:https://www.cnblogs.com/hd976521/p/6242497.html