首页 > 基础资料 博客日记
Java集合
2023-07-24 16:47:18基础资料围观226次
本篇文章分享Java集合,对你有帮助的话记得收藏一下,看Java资料网收获更多编程知识
集合
- 集合框架体系
Collection 接口有两个只要的孩子接口 List Set,他们的实现子类都是单列集合
- Collection
- List
- ArrayList(线程不安全)
- LinkedList
- Vector
- Set
- HashSet
- LinkedHashSet
- TreeSet
- List
Map 接口的实现子类 是双列集合,存放的K-V
- Map
- HashMap
- LinkedHashMap
- Hashtable
- Properties
- TreeMap
- HashMap
- 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方法都有 } }
文章来源:https://www.cnblogs.com/maqingsong/p/16486232.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签: