【Java基础】集合
为什么80%的码农都做不了架构师?>>>
常用子类:LinkedList、ArrayList、Vector。ArrayList是List接口最常用的一个子类。LinkedList完成的是一个链表操作。
实现子类:HashMap、HashTable、TreeMap等
TreeMap与TreeSet类似,TreeMap中元素根据key自动排序。
自定义类作为集合元素时,集合本身不知道如何排序,必须类实现Comparable接口,并覆写compareTo()方法。
public class Person implements Comparable<Person> {private String name;private int age;public Person(String name, int age) {this.age = age;this.name = name;}@Overridepublic int compareTo(Person o) {//按名字顺序排序return this.name.compareTo(o.name);}@Overridepublic String toString() {return this.name + ":" + this.age;}
}import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.ListIterator;
public class Test001 {public static void main(String[] args) {ArrayList<Person> person = new ArrayList<Person>();person.add(new Person("何明", 20));person.add(new Person("小米", 22));person.add(new Person("Jane", 21));person.add(new Person("Katty", 20));person.add(new Person("Blue Key",20));person.add(new Person("乔布斯",20));Collections.sort(person); //排序Iterator<Person> ite=person.iterator(); //Iterator迭代while(ite.hasNext())System.out.println(ite.next());for(Person p:person) //foreach遍历System.out.println(p);System.out.println(person); ListIterator<Person> lite= person.listIterator();//ListIterator迭代while(lite.hasNext()) //从前往后遍历while(lite.hasPrevious()) //从后向前比遍历,此操作需要从前往后遍历一趟System.out.println(lite.previous());}
}
//输出结果:
//[Blue Key:20, Jane:21, Katty:20, 乔布斯:20, 何明:20, 小米:22]import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class MapDemo {public static void main(String[] args) {Map<String, Integer> map = new TreeMap<>(); //按key排序map.put("诸葛亮", 3);map.put("孙悟空", 5);map.put("爱迪生", 8);map.put("孔乙己", 11);map.put("猪八戒", 7);Set<Map.Entry<String, Integer>> tree = map.entrySet();Iterator<Map.Entry<String, Integer>> ite = tree.iterator();while(ite.hasNext()) {Map.Entry<String,Integer> e=ite.next();System.out.println(e.getKey()+"-->"+e.getValue());}for (Map.Entry<String, Integer> entry : map.entrySet()) {System.out.print(entry.getKey() + "-->" + entry.getValue()+" ");//孔乙己-->11 孙悟空-->5 爱迪生-->8 猪八戒-->7 诸葛亮-->3}}
}
四、Map的实现子类中,当键为自定义类时,要想通过一个匿名对象找到值,则需要覆写equals()、hashCode()方法区分是否同一个对象。
//构造一个Person类
public class Person implements Comparable<Person> {public Person(String name, int age) {this.age = age;this.name = name;}@Overridepublic int compareTo(Person o) {return this.name.compareTo(o.name);}@Overridepublic String toString() {return this.name + ":" + this.age;}@Overridepublic boolean equals(Object obj) {//覆写equals()方法if (this == obj) {return true;} else if (!(obj instanceof Person)) {return false;}Person p = (Person) obj;if (this.name == p.name && this.age == p.age) {return true;} else {return false;}}@Overridepublic int hashCode(){ //覆写hashCode()方法//公式:Person.hashcode=name.hashcode*agereturn this.name.hashCode()*this.age; }private String name;private int age;
}
//测试代码
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class MapDemo01 {public static void main(String[] args) {Map<Person, String> map = new TreeMap<>();map.put(new Person("诸葛亮", 9), "zgl");map.put(new Person("孙悟空", 5), "swk");map.put(new Person("爱迪生", 8), "ads");map.put(new Person("孔乙己", 11), "kyj");map.put(new Person("猪八戒", 7), "zbj");/** 通过一个Person类的匿名对象找到value值* 需要覆写Person类的hashCode()方法与equals()方法*/System.out.println(map.get(new Person("诸葛亮", 9)));//输出TreeMapSet<Map.Entry< Person, String>> tree = map.entrySet();Iterator<Map.Entry< Person, String>> ite = tree.iterator();while (ite.hasNext()) {Map.Entry<Person, String> e = ite.next();System.out.println(e.getKey() + "-->" + e.getValue());}//foreach输出for (Map.Entry<Person, String> entry : map.entrySet()) {System.out.println(entry.getKey() + "-->" + entry.getValue());}}
}
五、总结
转载于:https://my.oschina.net/aram/blog/102568
【Java基础】集合相关推荐
- java基础—集合框架
java基础-集合框架 JDK1.2开始引入了集合框架的概念,以弥补java中只有数组这种容器的单一问题,这些框架多数由接口构成,另外也包含了一些对于接口实现的类,其中这些接口的最上层接口为java. ...
- JAVA基础+集合+多线程+JVM
1. Java 基础 1.1. 面向对象和面向过程的区别 面向过程性能比面向对象高. 因为类调用时需要实例化,开销比较大,比较消耗资源,所以当性能是最重要的考量因素的时候 等一般采用面向过程开发.但是 ...
- (阿里offer)春招知识点总结1:java基础+集合+并发+jvm+ssm
写在前面:大量总结不是系统的总结,而是作者本人根据自己情况总结的,许多我很清楚的地方就不在提及,大量源码也均未提及,尤其是框架的总结提及很浅.仅作为自己短期技术栈的一个总结,只作为参考,不是一定正确的 ...
- java基础集合操作工具类Collections简述(java集合四)
对集合中的元素进行排序 Collections中的sort方法使用 public class ListDemo {public static void main(String[] args) {Lis ...
- java基础----集合操作---实例----List集合的初始化
文章内容:最近使用LeapMotion进行软件开发时,使用到了java API---->List集合,使用过程中遇到了一些小bug,特写此博客记录相关心得. 程序编写背景:使用leapMotio ...
- Java基础---集合的概述---list、set、map、泛型、collections
集合的概述 单列集合的体系: Collection 单列集合的顶层接口 List Collection的子接口,增加了一些特有的方法,有序 可以重复 有索引 ArrayList 底层采用的数组结构实现 ...
- Java基础-------集合框架
一,集合简介 1.1 集合由来: 对象的存储:①数组(基本数据类型 & 引用数据类型) ②集合(引用数据类型) >数组存储数据的弊端:长度一旦初始化以后,就不可变:真正给数组 ...
- JAVA基础---集合(一)--集合框架概述
为什么用集合如何合理用集合,以及如何实现的和他们的实现原理,如果搞清了对于之后学习其他知识和开发是很有大帮助性的. 什么是集合框架? 集合框架是表示和操作集合的统一体系结构.所有集合框架都包含以下内容 ...
- 【面试专栏】第三篇:Java基础:集合篇-List、Queue
面试中问完基础基本上就是考集合,因为集合的使用在业务开发中经常使用,而且集合的数据结构也是算法的基础,所以你对集合的掌握深度可能决定你有没有接着面的资格 List & Queue 实现类 Li ...
- java基础 —— 集合、异常、反射、io流、多线程
1.集合 (Collection) #mermaid-svg-Po6HS0iqu2RMZvt5 .label {font-family: 'trebuchet ms', verdana, arial; ...
最新文章
- android 动态获取权限有哪些,Android 6.0+ 动态获取权限
- 数据挖掘深入理解和学习路径
- 数据库分页存储过程(5)
- html中如何让字段闪烁,Javascript结合CSS实现边框闪烁提示
- Java的反射作用_java反射机制的作用与优点
- 125	Valid Palindrome
- 机器学习理论与实战:逻辑回归
- [大牛翻译系列]Hadoop(7)MapReduce:抽样(Sampling)
- MySQL数据库基础教程索引
- linux打开dwg格式文件怎么打开软件,DWG 文件扩展名: 它是什么以及如何打开它?...
- App Store 受欢迎榜单(美国区)-分析出未来方向
- python爬取有道翻译的代码_有道翻译的爬取
- 排列组合算法的javascript实现
- 用SendMessage/PostMessage发送WM_SIZE消息
- android 9.0背光调节流程
- 统计学总结之Bias(偏差),Error(误差),和Variance(方差)的区别
- 【iOS】—— ARC学习
- 基于融云SDK实现高仿微信
- 响应USB接口为串口类型的拔插事件
- 黑苹果记(ThinkPad X200)