java基础——List集合内容输出、Set集合

List集合内容的输出(重点)

List集合内容的输出其实就是从集合中获取对象进行输出。

​​​​​​​使用for循环输出

使用for循环则可定以来与索引输出对象

public static void main(String[] args) {

//创建ArrayList集合的对象

List<Person> persons=new ArrayList<Person>();

persons.add(new Person("张三", 23, "男"));

persons.add(new Person("李四", 31, "女"));

persons.add(new Person("王五", 22, "男"));

persons.add(new Person("赵六", 27, "女"));

//输出集合中对象的姓名

for(int i=0;i<persons.size();i++){

System.out.println(persons.get(i).getName());

}

}

张三

李四

王五

赵六

​​​​​​​使用for...each循环输出

for...each其实是一个专门用来输出对象集合或者数组的循环。

语法:

for(数据类型 对象:对象集合){

    执行的语句;

}

案例:

public static void main(String[] args) {

//创建ArrayList集合的对象

List<Person> persons=new ArrayList<Person>();

persons.add(new Person("张三", 23, "男"));

persons.add(new Person("李四", 31, "女"));

persons.add(new Person("王五", 22, "男"));

persons.add(new Person("赵六", 27, "女"));

//输出集合中对象的姓名

for(Person per:persons){

System.out.println(per.getName());

}

}

张三

李四

王五

赵六

可以发现for...each在输出对象的时候比直接使用for循环简单明了,所以在以后遇到具体的业务的时候大家根据自己的需求去选择性的使用这两者。

​​​​​​​使用迭代器输出

在查看Collection接口中的方法的时候有一个如下的方法:

Iterator<E> iterator(){}

发现此方法返回值为Iterator,Iterator是一个接口,它属于工具,在这个接口中一共有以下的三个方法:

boolean hasNext(){}

next(){}

void remove(){}

上面的三个方法其实就为咱们准备好了一个Java中的迭代器,只不过现在咱们要实现的功能是输出数据和删除数据没有关系。既然iterator()方法是Collection接口的方法那么List接口和其子类对象就可以直接使用了。

使用迭代器完成集合中内容的输出:

public static void main(String[] args) {

//创建ArrayList集合的对象

List<Person> persons=new ArrayList<Person>();

persons.add(new Person("张三", 23, "男"));

persons.add(new Person("李四", 31, "女"));

persons.add(new Person("王五", 22, "男"));

persons.add(new Person("赵六", 27, "女"));

//输出集合中对象的姓名

Iterator<Person> iterator=persons.iterator();//获取迭代器对象

while(iterator.hasNext()){//判断是否有下一个对象

System.out.println(iterator.next().getName());//获取对象

}

}

张三

李四

王五

赵六

 ​​​​​​​Set集合

List类型的集合中添加的元素是可以重复添加但是有序,而Set集合中不能重复添加对象,而且没有顺序。

Set集合在开发中常用的子类有两个:

  1. HashSet
  2. TreeSet

HashSet 

HashSet是Set接口的一个子类,主要的特点是:里面不能存放重复元素,而且是采用散列的存储方式,所以是没有顺序的。 

在API文档中查看HashSet集合的方法的时候,发现方法很少,基本上都是直接从父类覆写过来的方法。

public boolean add(E e)

public boolean remove(Object o)

public void clear()

public boolean contains(Object o)

public boolean isEmpty()

public Iterator<E> iterator()

public int size()

这些方法和List集合相比较少多了,这些方法咱们在使用List集合的时候其实已经全部都接触了,所以在这里咱们直接使用了,主要是来验证List集合和Set集合的区别。

案例:

1.创建HashSet类的对象

public static void main(String[] args) {

//创建集合的对象

HashSet<String> hashSet=new HashSet<String>();

System.out.println(hashSet);

}

[]

输出对象的时候发现其实和List集合没有太大的区别。

2.向集合中添加内容

public static void main(String[] args) {

//创建集合的对象

HashSet<String> hashSet=new HashSet<String>();

//添加正常的数据

hashSet.add("sram");

hashSet.add("edu");

hashSet.add("org");

hashSet.add("com");

System.out.println(hashSet);

}

[com, org, edu, sram]

在向HashSet集合中添加内容的时候,添加完成后发现HashSet集合存放对象是无序的。

3.向集合中添加重复的内容

public static void main(String[] args) {

//创建集合的对象

HashSet<String> hashSet=new HashSet<String>();

//添加正常的数据

hashSet.add("sram");

hashSet.add("edu");

hashSet.add("sram");

hashSet.add("com");

System.out.println(hashSet);

}

[com, edu, sram]

此时在添加重复的内容的时候,程序没有出现任何错误,但是添加完成后,在集合中存放的重复内容只能有一个。

4.HashSet集合中其他常用方法的使用

public static void main(String[] args) {

//创建集合的对象

HashSet<String> hashSet=new HashSet<String>();

//添加正常的数据

hashSet.add("sram");

hashSet.add("edu");

hashSet.add("com");

System.out.println(hashSet);

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

System.out.println(hashSet.isEmpty());

hashSet.remove("com");//删除数据

System.out.println(hashSet);

}

[com, edu, sram]

3

false

[edu, sram]

上面的代码验证了Set集合和List集合的区别,除了它们两者区别外,其他的一些常用的操作都是一样的。

5.输出集合的内容

HashSet集合中的内容是无序的,也不能添加重复的数据,每个对象的索引其实是不固定的,所以直接使用for循环是不行的,如果在以后遇到了Set集合那么就想到如下的两种方式:

for...each

//创建集合的对象

HashSet<String> hashSet=new HashSet<String>();

//添加正常的数据

hashSet.add("sram");

hashSet.add("edu");

hashSet.add("com");

for(String str:hashSet){

System.out.println(str);

}

com

edu

sram

迭代器输出

public static void main(String[] args) {

//创建集合的对象

HashSet<String> hashSet=new HashSet<String>();

//添加正常的数据

hashSet.add("sram");

hashSet.add("edu");

hashSet.add("com");

Iterator<String> iterator=hashSet.iterator();

while(iterator.hasNext()){

System.out.println(iterator.next());

}

}

com

edu

sram

​​​​​​​TreeSet

TreeSet集合中存放的内容需要排序,而这里的排序不是添加内容时的顺序而是要按照指定的属性来完成对象的排序操作。

案例:

1.创建TreeSet对象

public static void main(String[] args) {

//创建集合的对象

TreeSet<String> treeSet=new TreeSet<String>();

System.out.println(treeSet);

}

[]

可以发现TreeSet和HashSet类的操作其实没有太大的区别。

2.使用TreeSet中的添加对象的方法

public static void main(String[] args) {

//创建集合的对象

TreeSet<String> treeSet=new TreeSet<String>();

//添加正常的数据

treeSet.add("sram");

treeSet.add("edu");

treeSet.add("admin");

treeSet.add("com");

treeSet.add("animal");

System.out.println(treeSet);

}

[admin, animal, com, edu, sram]

当TreeSet集合中添加字符串的时候发现添加完成后,内容是有序的,这里的有序不是添加内容的顺序,而是内容按照一定的方式进行排序了。

3.添加重复的内容

public static void main(String[] args) {

//创建集合的对象

TreeSet<String> treeSet=new TreeSet<String>();

//添加正常的数据

treeSet.add("sram");

treeSet.add("edu");

treeSet.add("admin");

treeSet.add("com");

treeSet.add("admin");

System.out.println(treeSet);

}

[admin, com, edu, sram]

此时添加的内容是排序了,但是添加的重复内容就只能有一个存放在此集合中。

4.其他方法的使用

public static void main(String[] args) {

//创建集合的对象

TreeSet<String> treeSet=new TreeSet<String>();

//添加正常的数据

treeSet.add("sram");

treeSet.add("edu");

treeSet.add("admin");

treeSet.add("com");

treeSet.add("admin");

System.out.println(treeSet);

System.out.println(treeSet.isEmpty());

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

treeSet.remove("admin");

System.out.println(treeSet);

}

[admin, com, edu, sram]

false

4

[com, edu, sram]

以上只是使用了部分方法,如果想要使用其他的方法则可以参考API文档。

​​​​​​​向Set集合中添加自定义的类的对象

在之前咱们添加都是JRE给咱们提供的类的对象,现在咱们自定义类来进行对象的添加。

自定义一个类Person类:

package com.sram.entity;

public class Person {

private String name;

private Integer age;

private String sex;

public Person(String name, Integer age, String sex) {

this.name = name;

this.age = age;

this.sex = sex;

}

public Person() {

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

@Override

public String toString() {

return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";

}

}

1、向HashSet集合中添加Person类的对象:

public static void main(String[] args) {

HashSet<Person> hashSet=new HashSet<Person>();

//添加对象

hashSet.add(new Person("张三", 23, "男"));

hashSet.add(new Person("李四", 31, "女"));

hashSet.add(new Person("王五", 22, "男"));

hashSet.add(new Person("赵六", 27, "女"));

System.out.println(hashSet);

}

[Person [name=王五, age=22, sex=男], Person [name=张三, age=23, sex=男], Person [name=李四, age=31, sex=女], Person [name=赵六, age=27, sex=女]]

此时在给HashSet集合添加内容的时候没有出现异常,这是因为HashSet集合在添加的时候是不会给对象进行排序操作的。所以在使用HashSet集合的时候自定义的类和JRE提供的类的操作方式是一样的没有任何的区别。

2.向TreeSet集合中添加Person类的对象

public static void main(String[] args) {

TreeSet<Person> persons=new TreeSet<Person>();

//添加对象

persons.add(new Person("张三", 23, "男"));

persons.add(new Person("李四", 31, "女"));

persons.add(new Person("王五", 22, "男"));

persons.add(new Person("赵六", 27, "女"));

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

}

向TreeSet集合中添加Person类的对象的是出现了一个异常:

Exception in thread "main" java.lang.ClassCastException: com.sram.entity.Person cannot be cast to java.lang.Comparable

这个异常指的是类型转换异常,是因为两个类之间没有能够进行转换的条件,类和类之间的转换其实就是Java面型对象的多态性。

Person类的对象不能转换成Comparable接口的对象,是因为Person类和Comparable接口之间没有实现的关系,在TreeSet集合中集合会给存储的对象进行排序,而这个排序的操作是由Comparable接口来完成的,所以在指定类的时候一定要注意此类是否实现了Comparable接口。

在添加String和Integer类的对象的是之所以不出现上面的异常是因为这两个类已经实现了Comparable接口:

public final class String

extends Object

implements Serializable, Comparable<String>, CharSequence

public final class Integer

extends Number

implements Comparable<Integer>

从两个类的定义格式上可以发现这两个类都实现Comparable接口。

3.让Person类实现Comparable接口

Comparable接口的定义格式:

public interface Comparable<T>{}

发现此接口是一个泛型接口,所以一个类在实现泛型接口的时候就应该满足泛型的设置。

Comparable接口中有一个抽象方法:

int compareTo(T o)

此方法在Java中是用来设置对象排序的条件的。所以如果一个类实现了Comparable接口的话则此类必须覆写Comparable接口的这个方法:

public class Person implements Comparable<Person>{//为了给对象排序

@Override

public int compareTo(Person per) {

return 0;

}

}

 

此时Person类实现了Comparable接口也覆写了接口中compareto()方法,那么接下来就应该指定对象的排序条件了。向TreeSet中添加自定义的对象的时候需要在对象对应的类中实现Compareable接口并覆写其中的compareTo()方法。

上面覆写了compareTo()方法,咱们就可以在方法中进行排序,首先明白排序时需要条件,就是根据上面排序,咱们现在是Person类,类中有三个属性:姓名,年龄,性别;所以咱们现在按照年龄进行排序:

package com.sram.entity;

public class Person implements Comparable<Person>{//为了给对象排序

private String name;

private Integer age;

private String sex;

public Person(String name, Integer age, String sex) {

this.name = name;

this.age = age;

this.sex = sex;

}

public Person() {

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

@Override

public String toString() {

return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";

}

@Override

public int compareTo(Person per) {

if(this.age<per.getAge()){

return -1;

}else if(this.age>per.getAge()){

return 1;

}else{

return 0;

}

}

}

public static void main(String[] args) {

TreeSet<Person> persons=new TreeSet<Person>();

//添加对象

persons.add(new Person("张三", 23, "男"));

persons.add(new Person("李四", 31, "女"));

persons.add(new Person("王五", 22, "男"));

persons.add(new Person("赵六", 27, "女"));

System.out.println(persons);

}

[Person [name=王五, age=22, sex=男], Person [name=张三, age=23, sex=男], Person [name=赵六, age=27, sex=女], Person [name=李四, age=31, sex=女]]

可以发现上面的TreeSet排序后是根据年龄进行了排序,但是有时候人的年龄可能是相等的那么怎么办呢?其实年龄相等了只能说明一个问题:那就是你找的条件不能很好的去实现排序,遇到这样的情况大家可以再去找一诶些其他的条件进行判断。Age相等了那么咱们还可以根据性别排序:年龄相同了则性别为女的在前

package com.sram.entity;

public class Person implements Comparable<Person>{//为了给对象排序

private String name;

private Integer age;

private String sex;

public Person(String name, Integer age, String sex) {

this.name = name;

this.age = age;

this.sex = sex;

}

public Person() {

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

@Override

public String toString() {

return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";

}

@Override

public int compareTo(Person per) {

if(this.age<per.getAge()){

return -1;

}else if(this.age>per.getAge()){

return 1;

}else{

//设定其他的排序条件(女在前)

if(this.sex.equals("女")){

return -1;

}else{

return 0;

}

}

}

}

public static void main(String[] args) {

TreeSet<Person> persons=new TreeSet<Person>();

//添加对象

persons.add(new Person("张三", 23, "男"));

persons.add(new Person("李四", 31, "女"));

persons.add(new Person("王五", 27, "男"));

persons.add(new Person("赵六", 27, "女"));

System.out.println(persons);

}

[Person [name=张三, age=23, sex=男], Person [name=赵六, age=27, sex=女], Person [name=王五, age=27, sex=男], Person [name=李四, age=31, sex=女]]

上面的案例是当年龄相等的时候对性别进行排序。

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值