您的位置:首页 > 编程语言 > Java开发

有关于java集合(set、list、map)的总结

2017-12-28 20:33 549 查看
一、set、list集合

1、set、list使用的方法有:

add  添加

clear  删除

 contains   判断集合中有没有相应的元素

 isEmpty   判断集合是否为空

Iteraor    iterator()   返回一个迭代器

remove    删除

size()      求元素数目

Object[] toAttray()  返回一个数组,该数组包含集合中的所有元素;即使使用范型,也只能转换是Object类型数组

2、set主要的实现类:

2.1、HashSet:用于存储(hash算法存取数据,存取速度快,调用hashCode()获取hash码,根据码值计算确定存储位置)

2.2、TreeSet:用于排序,不能重复(重复数据保留最先存入的数据)

TreeSet的排序方式有:

(1)自然排序:需要实现Comparable接口,重写compareTo方法

例程:

public int compareTo(Object o){

Student student=(Student)o;

return this.name.compareTo(student.name);

if(this.getAge()-student.getAge()>0)return  1;

else if(this.getAge()-student.getAge()<0)return -1;

else

return 0;

}//根据年龄升序排序

(2)客户化排序:重写一个类实现Comparator接口,重写其中的compare方法(比较两个对象的 大小如果保持不变为-1,如果想改变当前顺序设置为1)

例程:

public int compare(Object o1,Object o2){

Student s1=(Student)o1;

Student s2=(Student)o2;

//按照名字排序

// if(s1.getName().compareTo(s2.getName())>0) return 1;

// else if(s1.getName().compareTo(s2.getName())<0) return -1;

// else  return 0;

//按照分数排序

if(s1.getScore()-s2.getScore()>0) return 1;

else if(s1.getScore()-s2.getScore()<0) return -1;

else return 0;

}

3、list
实现方式:

3.1ArrayList:基于索引,查询速度快

3.2Linkedlist:基于链表,添加删除要比较容易些

三、map集合

HashMap:根据需求重写 代码,如果要年龄和名字一致,认为是同一个人,需要重写方法equals、HashCode

1、遍历map的方法

方法一:

Set<Integer> set= map.KeySet();

For(Integer key:set){

String value=map.get(key);//拿到所有的键

System.out.println(“key:”+key+” value”+ value);//通过相应的键,拿到对应的值

}

//同一个键值不能同时赋值,操作后原来对应的value值被覆盖。

方法二:

entrySet();返回映射关系

返回类型:Set<Map.Entry<K,V>>

拿键值:getKey()

拿值:getValue()

Set<Map.Entry<Integer,String> set=map.entrySet()

for(Map.Entry<Integer,String>en:set){

System.out.println(“key:”+en.getKey()+” value:”+en.getValue());

}

另一种写法:

Set<Map.Entry<Student,Integer>> set=map.entrySet();

for(Map.Entry<Student,Integer> en:set){

Student key=en.getKey();//键值为类对象的好处:在以后打印的时候,可以自由的选择按照那种方式打印输出。不受限制。

System.out.println(key.getName()+" "+en.getValue());

}

 

循环输出的方式:
加强for循环:

for(Person person:set){

System.out.println(person);

}

迭代器输出:

Iterator<Person>
iterator=set.iterator();

while(iterator.hasNext()){

Person person=iterator.next();

System.out.println(person);

}

四、三个集合的使用的差异

set     不可重复   无序

list     可重复       有序(放入有先后)

map  不可重复   无序

可执行的核心代码:

仅供参考:

HashMap测试样例代码:

如果年龄与名字一致,认为是同一个人,进行覆盖操作。比较String类型,使用HashMap,需要重写方法equals和HashCode

package com.briup.chap06;

import java.util.*;

public class MapTest{

public static void main(String args[]){

Map<Student,Integer> map=new
HashMap<Student,Integer>();

Student s=new Student("tom",20,90);

Student s1=new Student("Jack",20,99);

Student s2=new Student("cspe",23,90);

Student s3=new Student("mark",23,88);

map.put(s,s.getScore());

map.put(s1,s1.getScore());

map.put(s2,s2.getScore());

map.put(s3,s3.getScore());

打印方式一:

Set<Map.Entry<Student,Integer>> set=map.entrySet();

for(Map.Entry<Student,Integer> en:set){

Student key=en.getKey();//键值为类对象的 好处:在以后打印的时候,可以自由的选择按照那种方式打印输出。不受限制

System.out.println(key.getName()+" "+en.getValue());

}

打印方式二:

Set<Student> set=map.keySet();//返回键的set视图

for(Student key:set){

//获取键值。注意类型,键为什么类型,定义什么类型的变量

//int value=map.get(key);

System.out.println(key);//按照toString方法打印name&age

}

}

}

class Student{

private int age;

private String name;

private int score;

public Student(){}

public Student(String name,int age,int score){

this.name=name;

this.age=age;

this.score=score;

}

public void setName(String name){

this.name=name;

}

public String getName(){

return name;

}

public void setAge(int age){

this.age=age;

}

public int getAge(){

return age;

}

public void setScore(int score){

this.score=score;

}

public int getScore(){

return score;

}

public String toString(){

return "name:"+name+" age:"+age+" score:"+score;

}

public boolean equals(Object o){

if(o instanceof Student){

Student s=(Student)o;

return this.name.equals(s.name)&&

(this.age==age);

}

return false;

}

public int hashCode(){

return name.hashCode()+age;

}

}

TreeMap测试样例代码:

排序:根据年龄排序、根据名字字典序排序    。

package com.briup.chap06;

import java.util.*;

public class MapTest{

 

public static void main(String args[]){

Map<Student,Integer>map=new  TreeMap<Student,Integer>(new  StudentComparator());

Student s1=new Student("Jack",20,99);

Student s2=new Student("cspe",23,90);

Student s3=new Student("mark",23,88);

map.put(
f4e0
s,s.getScore());

map.put(s1,s1.getScore());

map.put(s2,s2.getScore());

map.put(s3,s3.getScore());

Set<Map.Entry<Student,Integer>> set=map.entrySet();

for(Map.Entry<Student,Integer> en:set){

Student key=en.getKey();

System.out.println(key.getName()+" "+en.getValue());

}

}

}

class StudentComparator implements Comparator{

public int compare(Object o1,Object o2){

Student s1=(Student)o1;

Student s2=(Student)o2;

//根据分数排序

if(s1.getScore()>s2.getScore()) return 1;

else  if(s1.getScore()<s2.getScore())return -1;

else return 0;

//根据年龄排序

/* if(s1.getName().compareTo(s2.getName())>0) return 1;

else if(s1.getName().compareTo(s2.getName())<0)return -1;

else

return 0;*/

}

}

class Student{

private int age;

private String name;

private int score;

public Student(){}

public Student(String name,int age,int score){

this.name=name;

this.age=age;

this.score=score;

}

public void setName(String name){

this.name=name;

}

public String getName(){

return name;

}

public void setAge(int age){

this.age=age;

}

public int getAge(){

return age;

}

public void setScore(int score){

this.score=score;

}

public int getScore(){

return score;

}

public String toString(){

return "name:"+name+" age:"+age+" score:"+score;

}

}

HashSet测试样例代码:

证明String重写了
equals和HashCode(),并通过自己重写两个函数,验证结果

package com.briup.chap06;

import java.util.*;

public class SetTest{

public static void main(String args[]){

Teacher t=new Teacher("tom",20);

Teacher t2=new Teacher("tom",20);

Set<Teacher> set =new HashSet<Teacher>();

set.add(t);

set.add(t2);

System.out.println(set.size());

String s=new String("china");

String s2=new String("china");

Set<String> set1=new HashSet<String>();

set1.add(s);

set1.add(s2);

System.out.println(set1.size());

}

 

}

class Teacher{

private String name;

private int age;

public Teacher(){}

public Teacher(String name,int age){

this.name=name;

this.age=age;

}

public void setName(String name){

this.name=name;

}

public String getName(){

return name;

}

public int getAge(){

return age;

}

public void setAge(int age){

this.age=age;

}

public String toString(){

return "name:"+name+" age:"+age;

}

public boolean equals(Object o){

if(o instanceof Teacher){

Teacher t=(Teacher)o;

return this.name.equals(t.name)

&&(this.age==t.age);

}

else

return false;

}

public int hashCode(){

return name.hashCode()+age;

}

}

输出结果:

1

1

TreeSet测试样例代码:

(1)客户化排序

升序

package day04;

 

import java.util.Comparator;

import java.util.Set;

import java.util.TreeSet;

 

//客户化排序

public class TreeSetTest2 {

public static void main(String[] args) {

Set<Student> set=new TreeSet<>(new CompartorStudent());

set.add(new Student("tom",11,88));

set.add(new Student("mark",14,86));

set.add(new Student("jack",13,78));

set.add(new Student("mark",14,87));

for(Student student:set){

System.out.println(student);

}

// System.out.println();

 

}

}

class CompartorStudent
implements Comparator{

public int compare(Object o1,Object o2){

Student s1=(Student)o1;

Student s2=(Student)o2;

// if(s1.getName().compareTo(s2.getName())>0) return 1;

// else if(s1.getName().compareTo(s2.getName())<0) return -1;

// else  return 0;

if(s1.getScore()-s2.getScore()>0)
return 1;

else if(s1.getScore()-s2.getScore()<0)
return -1;

else return 0;

}

}

class Student {

private String name;

private int age;

private int score;

public Student(){}

public Student(String name,int age,int score){

this.name=name;

this.age=age;

this.score=score;

}

public void setName(String name){

this.name=name;

}

public void setAge(int age){

this.age=age;

}

public void setScore(int score){

this.score=score;

}

public String getName(){

return name;

}

public int getAge(){

return age;

}

public int getScore(){

return score;

}

public String toString(){

return "name:"+name+" age:"+age+" score:"+score;

}

}

(2)自然排序

package day04;

 

import java.util.Set;

import java.util.TreeSet;

import java.util.function.IntToDoubleFunction;

 

public class TreeSetTest {

 

public static void main(String[] args) {

Set<Student> set=new TreeSet<>();

set.add(new Student("tom",11,88));

set.add(new Student("cspe",12,87));

set.add(new Student("jack",13,78));

set.add(new Student("mark",14,97));

for(Student student:set){

System.out.println(student);

}

}

 

}

class Student
implements Comparable{

private String name;

private int age;

private int score;

public Student(){}

public Student(String name,int age,int score){

this.name=name;

this.age=age;

this.score=score;

}

public void setName(String name){

this.name=name;

}

public void setAge(int age){

this.age=age;

}

public void setScore(int score){

this.score=score;

}

public String getName(){

return name;

}

public int getAge(){

return age;

}

public int getScore(){

return score;

}

public String toString(){

return "name:"+name+" age:"+age+" score:"+score;

}

public int compareTo(Object o){

Student student=(Student)o;

// return this.name.compareTo(student.name);

if(this.getAge()-student.getAge()>0)return  1;

else if(this.getAge()-student.getAge()<0)return -1;

else 

return 0;

}

}

总结:在集合的学习中,我认为必须掌握的要点:理解区别(面试中会涉及),学会使用(实践中需要会用),在实际代码操作中能够操作灵活,那么就达到目的了。

重点注意集合的遍历,尤其 是map集合 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 集合