java里的数据结构

ArrayList

读取歌单SongList.txt中的内容(歌名/演唱者),将歌名存入ArrayList<String>中。

Communication/The Cardigans
Black Dog/Led Zeppelin
Dreams/Van Halen
Comfortably Numb/Pink Floyd
Beth/Kiss
不营业的日常/刘若英
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;

public class Test{
    ArrayList<String> list = new ArrayList<String>();
    public static void main(String[] args){
        Test test = new Test();
        test.go();
    }
    public void go(){
        getSongs();
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
    }
    public void getSongs(){
        File file = new File("SongList.txt");
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = null;
            while((line=reader.readLine()) != null){
                addSong(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public void addSong(String line){
        String[] tokens = line.split("/");
        list.add(tokens[0]);
    }
}

接下来使用Collections.sort()ArrayList(String)中的元素按照字母顺序进行排序。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;

public class Test{
    ArrayList<String> list = new ArrayList<String>();
    public static void main(String[] args){
        Test test = new Test();
        test.go();
    }
    public void go(){
        getSongs();
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
    }
    public void getSongs(){
        File file = new File("SongList.txt");
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = null;
            while((line=reader.readLine()) != null){
                addSong(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public void addSong(String line){
        String[] tokens = line.split("/");
        list.add(tokens[0]);
    }
}

在这里插入图片描述
如果不用String描述一首歌,而是用一个对象呢?就像下面这样。

//Song.java
public class Song {

    String title;
    String artist;
    String rating;

    public Song(String t,String a,String r){
        title = t;
        artist = a;
        rating = r;
    }

    public String getTitle() {
        return title;
    }

    public String getArtist() {
        return artist;
    }

    public String getRating() {
        return rating;
    }

    public String toString(){
        return title;
    }
}
Communication/The Cardigans/5
Black Dog/Led Zeppelin/4
Dreams/Van Halen/4
Comfortably Numb/Pink Floyd/4
Beth/Kiss/5
不营业的日常/刘若英/5

继续调用Collections.sort()来对ArrayList<Song>进行排序。

//Test.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;

public class Test{
    ArrayList<Song> list = new ArrayList<Song>();
    public static void main(String[] args){
        Test test = new Test();
        test.go();
    }
    public void go(){
        getSongs();
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
    }
    public void getSongs(){
        File file = new File("SongList.txt");
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = null;
            while((line=reader.readLine()) != null){
                addSong(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public void addSong(String line){
        String[] tokens = line.split("/");
        list.add(new Song(tokens[0],tokens[1],tokens[2]));
    }
}

在这里插入图片描述
报错了!!
我们来看看sort方法的要求,public static <T extends Comparable<? super T>> void sort(List<T> list),即T必须实现Comparable接口(这里的 extends适用于继承、实现这两种情况)。
所以,接下来我们让 Song这个类实现Comparable这个接口。

//Song.java
public class Song implements Comparable<Song>{

    String title;
    String artist;
    String rating;

    public Song(String t,String a,String r){
        title = t;
        artist = a;
        rating = r;
    }

    public String getTitle() {
        return title;
    }

    public String getArtist() {
        return artist;
    }

    public String getRating() {
        return rating;
    }

    public String toString(){
        return title;
    }

    @Override
    public int compareTo(Song o) {
        return title.compareTo(o.getTitle());
    }
}

再执行下,就能输出正确的排序结果了。
事实上,Collections.sort()这个方法有重载。

  • public static <T extends Comparable<? super T>> void sort(List<T> list)
    这个我们刚刚已经用过了
  • public static <T> void sort(List<T> list,Comparator<? super T> c)
    接下来我们使用Comparator来实现歌曲排序。

使用TitleComparator让歌曲按歌名排序。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Test {
    ArrayList<Song> list = new ArrayList<Song>();

    public static void main(String[] args){
        Test t = new Test();
        t.go();
    }

    public class TitleComparator implements Comparator<Song>{
        @Override
        public int compare(Song o1, Song o2) {
            return o1.title.compareTo(o2.title);
        }
    }

    public void go(){
        getSongs();
        System.out.println(list);
        TitleComparator comparator = new TitleComparator();
        Collections.sort(list,comparator);
        System.out.println(list);

    }
    public void getSongs(){
        File file = new File("SongList.txt");
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = null;
            while((line = reader.readLine()) != null){
                addSong(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void addSong(String line){
        String[] tokens = line.split("/");
        list.add(new Song(tokens[0],tokens[1],tokens[2]));

    }
}

使用ArtistComparator让歌曲按演唱者排序。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Test {
    ArrayList<Song> list = new ArrayList<Song>();

    public static void main(String[] args){
        Test t = new Test();
        t.go();
    }
    public class ArtistComparator implements Comparator<Song>{

        @Override
        public int compare(Song o1, Song o2) {
            return o1.getArtist().compareTo(o2.getArtist());
        }
    }
    public void go(){
        getSongs();
        System.out.println(list);
        ArtistComparator comparator = new ArtistComparator();
        Collections.sort(list,comparator);
        System.out.println(list);

    }
    public void getSongs(){
        File file = new File("SongList.txt");
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = null;
            while((line = reader.readLine()) != null){
                addSong(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void addSong(String line){
        String[] tokens = line.split("/");
        list.add(new Song(tokens[0],tokens[1],tokens[2]));
    }
}

在这里插入图片描述

HashSet

下面是一份有重复的歌曲名单,我们看看怎么用HashSet来去重。

Communication/The Cardigans/5
Black Dog/Led Zeppelin/4
Dreams/Van Halen/4
不营业的日常/刘若英/5
Comfortably Numb/Pink Floyd/4
Beth/Kiss/5
不营业的日常/刘若英/5
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

public class Test{
    ArrayList<Song> list = new ArrayList<Song>();
    public static void main(String[] args){
        Test test = new Test();
        test.go();
    }
    public void go(){
        getSongs();
        System.out.println(list);

        Collections.sort(list);
        System.out.println(list);

        HashSet<Song> set = new HashSet<Song>();
        set.addAll(list);
        System.out.println(set);

    }
    public void getSongs(){
        File file = new File("SongList.txt");
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = null;

            while((line=reader.readLine())!=null){
                addSong(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addSong(String line){
        String[] tokens = line.split("/");
        list.add(new Song(tokens[0],tokens[1],tokens[2]));
    }
}

在这里插入图片描述
可以看到,把list放入set后,不仅没有去除重复,还把之前排好的序给搞乱了。
所以,对象怎样才算相等呢?
我们来了解下 引用相等性 和 对象相等性。

  • 引用相等性
    堆上同一对象的两个引用。
    堆上同一对象的两个引用,这两个引用调用hashCode()会返回相同的结果。
    如果hashCode()方法没有被覆盖,hashCode()的默认行为是返回每个对象特有的序号。大多数Java版本会根据内存位置计算此序号,所以不会有相同的hashcode。
  • 对象相等性
    堆上两个不同的对象在某种意义上视为相等。
    堆上两个不同的对象在某种意义上要视为相等,就必须覆盖从Object继承过来的hashCode()方法和equals()方法。
    具有相同hashcode的对象不一定相等,最后还需要使用equals()来作最终判定。

所以,接下来我们就要覆写hashCode()equals()这两个方法。

//Song.java
public class Song implements Comparable<Song>{
    public String title;
    public String artist;
    public String rating;

    public Song(String t,String a,String r){
        title = t;
        artist = a;
        rating = r;
    }

    public int hashCode(){
        return title.hashCode();
    }

    public boolean equals(Object aSong){
        Song o = (Song) aSong;
        return title.equals(o.getTitle());
    }

    public String getTitle() {
        return title;
    }

    public String getArtist() {
        return artist;
    }

    public String getRating() {
        return rating;
    }

    public String toString(){
        return title;
    }

    @Override
    public int compareTo(Song o) {
        return title.compareTo(o.getTitle());
    }
}
//Test.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

public class Test{
    ArrayList<Song> list = new ArrayList<Song>();
    public static void main(String[] args){
        Test test = new Test();
        test.go();
    }
    public void go(){
        getSongs();
        System.out.println(list);

        Collections.sort(list);
        System.out.println(list);

        HashSet<Song> set = new HashSet<Song>();
        set.addAll(list);
        System.out.println(set);

    }
    public void getSongs(){
        File file = new File("SongList.txt");
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = null;

            while((line=reader.readLine())!=null){
                addSong(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addSong(String line){
        String[] tokens = line.split("/");
        list.add(new Song(tokens[0],tokens[1],tokens[2]));
    }
}

运行下Test.java,输出结果如下,符合预期哈。
在这里插入图片描述

TreeSet

//Test.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.TreeSet;

public class Test{
    ArrayList<Song> list = new ArrayList<Song>();
    public static void main(String[] args){
        Test test = new Test();
        test.go();
    }
    public void go(){
        getSongs();
        System.out.println(list);

        Collections.sort(list);
        System.out.println(list);

        TreeSet<Song> set = new TreeSet<Song>();
        set.addAll(list);
        System.out.println(set);

    }
    public void getSongs(){
        File file = new File("SongList.txt");
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = null;

            while((line=reader.readLine())!=null){
                addSong(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addSong(String line){
        String[] tokens = line.split("/");
        list.add(new Song(tokens[0],tokens[1],tokens[2]));
    }
}

在这里插入图片描述

HashSet

既能去重,也能排序,可以考虑用HashSet

//Book.java
public class Book {
    public String title;
    public Book(String t){
        title = t;
    }
    public String toString(){
        return title;
    }
}
//Test.java
import java.util.TreeSet;

public class Test {
    public static void main(String[] args){
        Test test = new Test();
        test.go();
    }
    public void go(){
        Book b1 = new Book("How Cats Work");
        Book b2 = new Book("Remix your Body");
        Book b3 = new Book("Finding Emo");
        TreeSet<Book> set = new TreeSet<Book>();
        set.add(b1);
        set.add(b2);
        set.add(b3);
        System.out.println(set);
    }
}

在这里插入图片描述
报错了!!因为Book是不可以比较的。对此,解决方法有二,

  1. Book实现Comparable接口,创建TreeSet实例时使用其默认构造函数。
//Book.java
public class Book implements Comparable<Book>{
    public String title;
    public Book(String t){
        title = t;
    }
    public String toString(){
        return title;
    }

    @Override
    public int compareTo(Book o) {
        return title.compareTo(o.title);
    }
}
//Test.java

import java.util.TreeSet;

public class Test {
    public static void main(String[] args){
        Test test = new Test();
        test.go();
    }
    public void go(){
        Book b1 = new Book("How Cats Work");
        Book b2 = new Book("Remix your Body");
        Book b3 = new Book("Finding Emo");
        TreeSet<Book> set = new TreeSet<Book>();
        set.add(b1);
        set.add(b2);
        set.add(b3);
        System.out.println(set);
    }
}
  1. 使用Comparator,创建TreeSet实例时向TreeSet构造函数中传入Comparator实例作参数
//Book.java
public class Book{
    public String title;
    public Book(String t){
        title = t;
    }
    public String toString(){
        return title;
    }
}
//Test.java
import java.util.Comparator;
import java.util.TreeSet;

public class Test {
    public static void main(String[] args){
        Test test = new Test();
        test.go();
    }
    public class TitleComparator implements Comparator<Book> {

        @Override
        public int compare(Book o1, Book o2) {
            return o1.title.compareTo(o2.title);
        }
    }
    public void go(){
        Book b1 = new Book("How Cats Work");
        Book b2 = new Book("Remix your Body");
        Book b3 = new Book("Finding Emo");
        TreeSet<Book> set = new TreeSet<Book>(new TitleComparator());
        set.add(b1);
        set.add(b2);
        set.add(b3);
        System.out.println(set);
    }
}

HashMap

//Test.java
import java.util.HashMap;

public class Test{
    public static void main(String[] args){
        new Test().go();
    }
    public void go(){
        HashMap<String,Integer> map = new HashMap<String,Integer>();
        map.put("John",100);
        map.put("Tom",98);
        map.put("Lucy",99);
        System.out.println(map);
        System.out.println(map.get("John"));
    }
}

在这里插入图片描述

数组

初始化、遍历数组

double[] a = new double[10];
for(int i=0;i<a.length;i++) {
	a[i] = Math.floor(Math.random()*100+1);
	System.out.println(a[i]);
}
int[] b = {10,20,30};
for(int item:b) {
	System.out.println(item);
}
int[] c = new int[] {4,5,6};
System.out.println(java.util.Arrays.toString(c));
数组与多态
//Animal.java
public class Animal {
    public void eat(){
        System.out.println("Animal eating");
    }
}
//Dog.java
public class Dog extends Animal {
    public void bark(){

    }
}
//Cat.java
public class Cat extends Animal {
    public void meow(){

    }
}
//Test.java
public class Test{

    public static void main(String[] args){
        new Test().go();
    }
    public void go(){
        Animal[] animals = {new Dog(),new Cat(),new Dog()};
        takeAnimals(animals);
        Dog[] dogs = {new Dog(),new Dog(),new Dog()};
        takeAnimals(dogs);
    }
    public void takeAnimals(Animal[] animals){
        for(Animal animal:animals){
            animal.eat();
        }
    }
}

泛型

泛型与多态
//Test.java
import java.util.ArrayList;

public class Test{
    public static void main(String[] args){
        new Test().go();
    }
    public void go(){
        ArrayList<Animal> animals = new ArrayList<Animal>();
        animals.add(new Dog());
        animals.add(new Cat());
        animals.add(new Dog());
        takeAnimals(animals);
        ArrayList<Dog> dogs2 = new ArrayList<Dog>();
        dogs2.add(new Dog());
        dogs2.add(new Dog());
        dogs2.add(new Dog());
        takeAnimals(dogs2);
    }
    public void takeAnimals(ArrayList<Animal> animals){
        for(Animal animal:animals){
            animal.eat();
        }
    }
}

在这里插入图片描述
编译器报错了!!
呃,这是为什么呢?Dog不是Animal的子类吗,所以可以调用eat()方法,这是没有问题的啊。
是的,单看上面的public void takeAnimals(ArrayList<Animal> animals){}是没有问题的。
但是如果将takeAnimals修改成如下,这就有问题了。

public void takeAnimals(ArrayList<Animal> animals){
	animals.add(new Cat());
}

ArrayList<Dog>中是不允许添加Cat类型的。
数组的类型检查发生在运行期间,泛型的类型检查发生在编译期间,因此上面的示例在使用多态时,数组这边编译器没有报错,但泛型这边编译器报错了,这也顺便证明了 泛型具有更高的类型安全性。
在这里插入图片描述
刚刚我们只是了解了编译器报错的原因,现在我们来解决吧。解法有二,其实就是两种写法。

  • public void takeAnimals(ArrayList<? extends Animal> animals)
    ?是万能字符。
    使用<?>的声明中,编译器不允许集合中加入任何东西。也就是说,ArrayList<? extends Animal> animals时,animals.add(new Cat())不可能编译通过。
//Test.java
import java.util.ArrayList;

public class Test{
    public static void main(String[] args){
        new Test().go();
    }
    public void go(){
        ArrayList<Animal> animals = new ArrayList<Animal>();
        animals.add(new Dog());
        animals.add(new Cat());
        animals.add(new Dog());
        takeAnimals(animals);
        ArrayList<Dog> dogs = new ArrayList<Dog>();
        dogs.add(new Dog());
        dogs.add(new Dog());
        dogs.add(new Dog());
        takeAnimals(dogs);
    }

    public void takeAnimals(ArrayList<? extends Animal> animals){
        for(Animal animal:animals){
            animal.eat();
        }
    }
}
  • public <T extends Animal> void takeAnimals(ArrayList<T> animals)
    其中,extends适用于 继承 和 实现 这两种情况。
//Test.java
import java.util.ArrayList;

public class Test{
    public static void main(String[] args){
        new Test().go();
    }
    public void go(){
        ArrayList<Animal> animals = new ArrayList<Animal>();
        animals.add(new Dog());
        animals.add(new Cat());
        animals.add(new Dog());
        takeAnimals(animals);
        ArrayList<Dog> dogs = new ArrayList<Dog>();
        dogs.add(new Dog());
        dogs.add(new Dog());
        dogs.add(new Dog());
        takeAnimals(dogs);
    }

    public <T extends Animal> void takeAnimals(ArrayList<T> animals){
        for(Animal animal:animals){
            animal.eat();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值