JavaSE:集合总结(Collection,Map)
今天来总结JavaSE部份的集合。首先来从整体来看: 我们主要要学习的内容: Collection:
Map:
1.Collection:由于Collection是所有集合类的父接口,所以它中的方方法接口和具体实现类都会继承或实现,那末先来学习Collection:
接下来学习:
再看看这个结构,我们要学的就是这些内容:
1.1 ArrayList: 注意: 1.2 LinkedList:
1.3 Vector: 2 Set 子接口
和List的特点恰恰相反,Set中的元素不能是重复的,但是可以是无序的。 问题:怎样来保证两个元素是不能重复的? 固然只有具体的实现类才有承接对象的能力,而Set的这个特点对它的实现类都是适用的。 2.1 HashSet: 2.2 LinkedHashSet: 2.3 TreeSet:
说实话,这么多方法,我只用到了comparator()!!! 定制排序: /*
* 1.请从键盘随机输入10个整数保存到List中,并按倒序、从大到小的顺序显示出来
*
* 2.请把学生名与考试分数录入到Map中,并按分数显示前3名成绩学员的名字。
*/
@Test
public void test3(){
scan = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < 10; i++){
list.add(scan.nextInt());
}
Collections.sort(list);
Collections.reverse(list);
for(Integer i : list){
System.out.println(i);
}
}
/*
* 1. 定义1个Employee类, 该类包括:private成员变量name,age,birthday,其中 birthday 为 MyDate
* 类的对象; 并为每个属性定义 getter,setter 方法; 并重写 toString 方法输出 name,birthday
*
* MyDate类包括: private成员变量month,day,year;并为每个属性定义 getter,setter 方法;
*
* 创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中
* 分别按以下两种方式对集合中的元素进行排序,并遍历输出:
*
* 1). 使Employee 实现 Comparable 接口,并按 name 排序 2). 创建 TreeSet 时传入
* Comparator对象,按生日日期的前后排序。
*
*
*/
@SuppressWarnings("unused")
@Test
public void test2() {
class MyCommparator implements Comparator<Employee> {
@Override
public int compare(Employee o1,Employee o2) {
if (o1.equals(o2))
return 0;
else if (o1.getName().equals(o2.getName()))
return new Integer(o1.getAge()).compareTo(new Integer(o2
.getAge()));
else
return o1.getName().compareTo(o2.getName());
}
}
Comparator<Employee> cpt = new Comparator<Employee>() {
@Override
public int compare(Employee o1,Employee o2) {
if (o1.equals(o2))
return 0;
else if (o1.getName().equals(o2.getName()))
return new Integer(o1.getAge()).compareTo(new Integer(o2
.getAge()));
else
return o1.getName().compareTo(o2.getName());
}
};
TreeSet<Employee> ts = new TreeSet<Employee>(
new Comparator<Employee>() {
@Override
public int compare(Employee o1,Employee o2) {
if (o1.equals(o2))
return 0;
else if (o1.getName().equals(o2.getName()))
return new Integer(o1.getAge())
.compareTo(new Integer(o2.getAge()));
else
return o1.getName().compareTo(o2.getName());
}
});
ts.add(new Employee("zhangsan",23,new MyDate(12,12,1993)));
ts.add(new Employee("zhaoliu",27,new MyDate(10,11,1977)));
ts.add(new Employee("wangwu",25,new MyDate(22,1,1989)));
ts.add(new Employee("zhaoliu",new MyDate(3,2,1993)));
ts.add(new Employee("tianqi",28,new MyDate(2,4,1991)));
for (Employee t : ts) {
System.out.println(t);
}
}
// Collection: List:arrayList linkedList / Set: hashSet->linkedHashSet
// treeSet
@Test
public void test1() {
String[] str = new String[5];
for (String s : str) {
s = "atguigu";
System.out.println(s);
}
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
}
@Test
public void test() {
List<Object> list = new ArrayList<Object>();
list.add(123);
list.add("zhang");
for (Object l : list) {
System.out.println(l);
}
Iterator<Object> itor = list.iterator();
while (itor.hasNext()) {
System.out.println(itor.next());
}
ListIterator<Object> itor1 = list.listIterator();
itor1.add("lisi");
while (itor1.hasNext()) {
System.out.println(itor1.next());
}
} /*1. 定义1个Employee类,
该类包括:private成员变量name,birthday,其中 birthday 为 MyDate 类的对象;
并为每个属性定义 getter,setter 方法;
并重写 toString 方法输出 name,birthday
*/
public class Employee{
private String name;
private int age;
private MyDate birthday;
public Employee() {
}
public Employee(String name,int age,MyDate birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
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 MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee [name=" + name + ",age=" + age + ",birthday="
+ birthday + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((birthday == null) ? 0 : birthday.hashCode());
return result;
}
// @Override
// public int compareTo(Employee o) {
// if(this == o)
// return 0;
// else if(this.getName().equals(o.getName()))
// return new Integer(this.age).compareTo(new Integer(o.age));
// else
// return this.getName().compareTo(o.getName());
// }
} //private成员变量month,setter 方法;
public class MyDate {
private int month;
private int day;
private int year;
public MyDate() {
}
public MyDate(int month,int day,int year) {
this.month = month;
this.day = day;
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
@Override
public String toString() {
return "MyDate [month=" + month + ",day=" + day + ",year=" + year
+ "]";
}
} 3 Queue 到这里Collection集合就先告1段落。 4 集合元素的遍历:
2.Map
1 Map 注意理解映照:我昨天的博客“JDBC的进化2“中所说的ORM就是1种映照关系。还记得么?回顾1下:ORM:对象关系映照,没1张表对应这1个类,每列对应1个属性,每行对应1个对象。 方法:
1.1 HashMap 1.1.1 LinkedHashMap 1.2 TreeMap 1.3 Hashtable 1.3.1 Properties Properties pros = new Properties();
pros.load(new FileInputStream("jdbc.properties"));
String user = pros.getProperty("user");
System.out.println(user);
Ok,Map也结束了。。。 3 工具类(Collections)
到此为止,是否是以为就总结完了?No,其实还没有开始总结,上面的仅仅是回顾。我们来总结。 总结:没有好的作图工具(思惟导图有很大的局限性,可能也是我研究的不够深),我只能是手画了,有时候图形能表达的东西更多,也更容易理解记忆。 先来这1张整体的 手机像素有点渣,看不清第1张的,可以看下面的两张。 画的不好,但是我认为可以表达意思了。 来,开始的分析: 我是这样分析的,从整体再到具体(思惟),先纵向再横向: 1.Collection 和Map,有甚么联系和区分? 联系:Collection和Map都是java.util下的集合接口,Collection和Map属于同1层次。 区分:Collection中寄存的是1个值(只能可以是任意的对象),而Map中寄存的是键值对(固然键和值,也只能可以是任意对象) *再往下走,忘画1个Queue了。 2.List和Set和Queue有甚么区分和联系 联系:都是Collection的子接口。 区分:List中的元素是有序的,可以重复的,而Set中的元素是无序的,不可以重复的;Queue是1个古老的类,是线程安全的,现在很少使用。 *Map和他俩不是1个层次的,没法进行比较 3.ArrayList,LinkedList和Vector的区分和联系 联系:都是List的实现类 区分:LinkedList相比于ArrayList增加了链表结构来保护元素的顺序,适用于频繁的插入和删除操作,而迭代速度没有ArrayList快。Vector是1个古老的实现类,是线程安全的,它中的方法和ArrayList类似,但是效力低于ArrayList *再来比较Set中各个元素 *到比较Map了 最后就是整体的横向对照: 这几个效力的比较是我自己根据他们的理解来判断的,没有学过数据结构和算法,等我学完后,就可以明确的给出1个效力了,得斟酌它们的数据结构和算法复杂度。有兴趣的朋友可以和我1起研究。 5.Map就不做说明了。对照上面的Set,相信你自己也能明白了。 差点忘了,还有1个要补充的: @Test
public void test5(){
// when the element is "def",insert "bbb" or modify to "bbbb",and remove "def";
List<String> list = new ArrayList<String>();
list.add("abc");
list.add("def");
list.add("ghi");
ListIterator<String> li = list.listIterator();
while(li.hasNext()){
String str = li.next();
if(str.equals("def")){
// li.add("bbb");
li.set("aaaa");
li.remove();
}
}
System.out.println(list);
} Ok,总算是就集合总结完了,有耐心看完的朋友,相信你会收获不小。固然,我这总结的可能也会有很多的漏洞,欢迎大家指导。睡觉了。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |