ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java集合

2022-07-18 18:00:26  阅读:166  来源: 互联网

标签:Java name author price List 集合 public String


集合

  • 集合框架体系

Collection 接口有两个只要的孩子接口 List Set,他们的实现子类都是单列集合

  • Collection
    • List
      • ArrayList(线程不安全)
      • LinkedList
      • Vector
    • Set
      • HashSet
      • LinkedHashSet
      • TreeSet

Map 接口的实现子类 是双列集合,存放的K-V

  • Map
    • HashMap
      • LinkedHashMap
    • Hashtable
      • Properties
    • TreeMap
  • Collection
public class Application {
    public static void main(String[] args) {
        //add:添加单个元素
        ArrayList List = new ArrayList();
        List.add("马青松");
        List.add("mqs");
        System.out.println(List);
        //remove:删除指定元素
        List.remove(1);
        System.out.println(List);
        //contains:查找元素是否存在
        System.out.println(List.contains("马青松"));
        //size:获取元素个数
        System.out.println(List.size());
        //isEmpty:判断元素个数
        System.out.println(List.isEmpty());
        //clear:清空
        List.clear();
        System.out.println(List);
        //addAll:添加多个元素
        ArrayList List2 = new ArrayList();
        List2.add("马青松");
        List2.add("目前是");
        List.addAll(List2);
        System.out.println(List);
        //containsAll:查找多个元素是否都存在
        System.out.println(List.containsAll(List2));
        //removeAll:删除多个元素
        List.removeAll(List2);
        System.out.println(List);

    }
}

迭代器遍历

public class Application {
    public static void main(String[] args) {
        Collection col = new ArrayList();
        col.add(new Book("三国演义","罗贯中",10.1));
        col.add(new Book("红楼梦","蓝屏",10.1));
        col.add(new Book("小李飞刀","路旁",10.1));

        //System.out.println("col=" +col);
        //现在希望能够遍历 col集合
        //1.先得到col对应的迭代器
        Iterator iterator = col.iterator();
        //2.使用while循环遍历
        while (iterator.hasNext()){//判断是否还有数据
            Object obj = iterator.next();
            System.out.println(obj);
        }
    }
}

class Book{
    private String name;
    private String author;
    private double price;

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}

List接口方法:

public class Application {
    public static void main(String[] args) {
        //List集合类中元素有序(即添加顺序和取出顺序一致),且可重复
        List list = new ArrayList();
        list.add("jake");
        list.add("tom");
        list.add("mqs");
        list.add("kojds");
        list.add("jake");
        System.out.println(list);
        //List集合中的每个元素都有其对应的顺序索引,即支持索引
        System.out.println(list.get(2));
    }
}

 

 

List底层源码

 

 

 List接口联系:

public class Application {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
       List list = new ArrayList();
       list.add(new Book("西游记","mqs",20.00));
       list.add(new Book("阿衰梦","mqs",40.00));
       list.add(new Book("红楼梦","mqs",10.00));
       list.add(new Book("马青松","mqs",70.00));
        sort(list);
        for (Object o:list){
            System.out.println(o);
        }
    }

    public static void sort( List list){
        for (int i=0; i<list.size()-1; i++){
            for (int j=0; j<list.size()-1-i; j++){
               Book book1 = (Book)list.get(j);
                Book book2 = (Book)list.get(j+1);
                if (book1.getPrice()  > book2.getPrice()){
                        list.set(j,book2);
                        list.set(j+1,book1);
                }
            }
        }
    }

    public static class Book {
        private String name;
        private String author;
        private double price;

        public Book(String name, String author, double price) {
            this.name = name;
            this.author = author;
            this.price = price;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(String author) {
            this.author = author;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        @Override
        public String toString() {
            return  name + '\t' + author + '\t'+ price + '\t';
        }
    }
}

LinkedList的底层实现了双向链表和双端队列特点

可以添加任意元素(元素可以重复)包括null

线程不安全,没事实现同步

LinkedList底层源码

 

public class Application {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.add("MQS");

        System.out.println(linkedList);
        System.out.println(linkedList.get(0));
        System.out.println(linkedList.indexOf("MQS"));
        //ArrayList方法都有
    }
}

 

 

标签:Java,name,author,price,List,集合,public,String
来源: https://www.cnblogs.com/maqingsong/p/16486232.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有