java练习题(set集合)

第一题

请将如下4个字符串数据["aaa""bbb""ccc""ddd"],依次添加到HashSet集合中,并遍历查看存储结果。反馈该题
查看

参考答案

package cn.Work1206.hooong01;

/*
请将如下4个字符串数据["aaa","bbb","ccc","ddd"],依次添加到HashSet集合中,并遍历查看存储结果。
 */
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

public class Demo {
    public static void main(String[] args) {
        HashSet<String> hashSet = new HashSet<>();
        hashSet.add("aaa");
        hashSet.add("bbb");
        hashSet.add("ccc");
        hashSet.add("ddd");
        for (String s : hashSet) {
            System.out.println(s);
        }
    }
}

第二题

20191月份的世界编程语言排行榜从高到低依次如下: Java、C、Python、C++、Visual Basic .NET、JavaScript... 请将以上语言名称作为字符串元素,按顺序存入set集合中,并遍历查看。要求存储和遍历的顺序保持一致。

参考答案

package cn.Work1206.hooong01;

import java.util.LinkedHashSet;

/*
2019年1月份的世界编程语言排行榜从高到低依次如下:
 Java、C、Python、C++、Visual Basic .NET、JavaScript...
  请将以上语言名称作为字符串元素,按顺序存入set集合中,
并遍历查看。要求存储和遍历的顺序保持一致。
 */
public class Demo02 {
    public static void main(String[] args) {
        LinkedHashSet<String> strings = new LinkedHashSet<>();
        strings.add("Java");
        strings.add("C");
        strings.add("Python");
        strings.add("C++");
        strings.add("Visual Basic");
        strings.add("NET");
        strings.add("JavaScript");
        System.out.println(strings);
    }
}

第三题

下面有一些程序员经常访问的IT网站(排名不分先后): itheima, chinaunix, itpub, csdn, 51cto, iteye... 请将以上网站名称作为字符串元素,按照字典顺序存入set集合中,并遍历查看结果。

参考答案

package cn.Work1206.hooong01;

import java.util.TreeSet;

/*
下面有一些程序员经常访问的IT网站(排名不分先后):
 , , , , , ...
 请将以上网站名称作为字符串元素,
按照字典顺序存入set集合中,并遍历查看结果。
 */
public class Demo03 {
    public static void main(String[] args) {
        TreeSet<String> strings = new TreeSet<>();
        strings.add("itheima");
        strings.add("itpub");
        strings.add("csdn");
        strings.add("51cto");
        strings.add("iteye");
        strings.add("chinaunix");
        System.out.println(strings);
    }
}

第四题

以下代码完成的功能是遍历HashSet集合,打印每个字符串元素以及元素的长度。但运行程序后出现异常,请排查错误原因并改正,以保证代码能够正确运行。
public class Test04 {
    public static void main(String[] args) {
        // 1. 创建HashSet集合
        HashSet<String> hs = new HashSet<>();
        // 2. 添加数据到集合
        hs.add("itcast");
        hs.add("itheima");
        hs.add("javase");
        // 3. 迭代器遍历集合元素
        Iterator<String> it = hs.iterator();
        while(it.hasNext()){
            System.out.println(it.next()); // 元素
            System.out.println(it.next().length()); // 元素长度
        }
    }
}

参考答案

package cn.Work1206.hooong01;

import java.util.HashSet;
import java.util.Iterator;

public class Deno04 {
   // 以下代码完成的功能是遍历HashSet集合,打印每个字符串元素以及元素的长度。
   // 但运行程序后出现异常,请排查错误原因并改正,以保证代码能够正确运行。
    public static void main(String[] args) {
        // 1. 创建HashSet集合
        HashSet<String> hs = new HashSet<>();
        // 2. 添加数据到集合
        hs.add("itcast");
        hs.add("itheima");
        hs.add("javase");
        // 3. 迭代器遍历集合元素
        Iterator<String> it = hs.iterator();
        while(it.hasNext()){
            String next = it.next();
            System.out.println(next); // 元素
            System.out.println(next.length()); // 元素长度
        }
    }
}

第五题

现有若干图书信息(包含名称title、作者author、定价price)需要存储到set集合中,保证集合中无重复元素,并遍历查看。可以认为所有信息都相同的图书为重复数据。

参考答案

package cn.Work1206.hooong01;
/*
现有若干图书信息(包含名称、作者、定价)需要存储到set集合中,保证集合中无重复元素,
并遍历查看。可以认为所有信息都相同的图书为重复数据。
 */
public class Book {
    private String title;
    private String author;
    private double price;

    public Book() {
    }

    public Book(String title, String author, double price) {
        this.title = title;
        this.author = author;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book book = (Book) o;

        if (Double.compare(book.price, price) != 0) return false;
        if (title != null ? !title.equals(book.title) : book.title != null) return false;
        return author != null ? author.equals(book.author) : book.author == null;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = title != null ? title.hashCode() : 0;
        result = 31 * result + (author != null ? author.hashCode() : 0);
        temp = Double.doubleToLongBits(price);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }
}
package cn.Work1206.hooong01;

import java.util.HashSet;

/*
现有若干图书信息(包含名称title、作者author、定价price)
需要存储到set集合中,保证集合中无重复元素,
并遍历查看。可以认为所有信息都相同的图书为重复数据。
 */
public class Demo05 {
    public static void main(String[] args) {
        HashSet<Book> books = new HashSet<>();
        books.add(new Book("推背图","??",199.98));
        books.add(new Book("烧饼歌","刘伯温",99.98));
        books.add(new Book("推背图","??",199.98));
        System.out.println(books);
    }
}

第六题

在某次考试中,学生的成绩信息如下: 姓名 年龄 成绩 Tom 20 90 Jerry 22 95 John 20 100 Lily 22 100 Lucy 22 90 Kevin 22 90 请分别用Comparable和Comparator两个接口对以上同学的成绩做降序排序,如果成绩一样,那在成绩排序的基础上按照年龄由小到大排序,成绩和年龄都一样,则按照姓名的字典顺序排序。

参考答案

package cn.Work1206.hooong01;

public class Student{
    private String name;
    private int Chinese;
    private int English;

    public Student() {
    }

    public Student(String name, int chinese, int english) {
        this.name = name;
        Chinese = chinese;
        English = english;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChinese() {
        return Chinese;
    }

    public void setChinese(int chinese) {
        Chinese = chinese;
    }

    public int getEnglish() {
        return English;
    }

    public void setEnglish(int english) {
        English = english;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", Chinese=" + Chinese +
                ", English=" + English +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (Chinese != student.Chinese) return false;
        if (English != student.English) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + Chinese;
        result = 31 * result + English;
        return result;
    }
}
package cn.Work1206.hooong01;

import java.util.Collection;
import java.util.Comparator;
import java.util.TreeSet;

/*
在某次考试中,学生的成绩信息如下:
姓名 年龄 成绩  22 95    22 100  22 90  22 90
请分别用Comparable和Comparator两个接口对以上同学的成绩做降序排序,
如果成绩一样,那在成绩排序的基础上按照年龄由小到大排序,
成绩和年龄都一样,则按照姓名的字典顺序排序。
 */
public class Demo06 {
    public static void main(String[] args) {
       TreeSet<Student> students = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int i = o1.getChinese() + o1.getEnglish() - o2.getChinese() - o2.getEnglish();
                int i1 = i == 0 ? o1.getName().compareTo(o2.getName()) : i;
                return i1;
            }
        });
        students.add(new Student("Tom",20,90 ));
        students.add(new Student("John",20 ,100 ));
        students.add(new Student("Lily",22,100));
        students.add(new Student("Lucy",22,90));
        students.add(new Student("Kevin",22,90));
        students.add(new Student("Jerry",22,95));
        for (Student student : students) {
            System.out.println(student);
        }
    }
}

第七题

现获取到用户输入的一段字符串(可从键盘录入),例如:aaaabbbcccccdd。请编写程序获取其中无重复的字符组成一个新的字符串,即:abcd。请不要改变字符的输入顺序。

参考答案

package cn.Work1206.hooong01;

import java.util.LinkedHashSet;
import java.util.Scanner;

/*
现获取到用户输入的一段字符串(可从键盘录入),
例如:aaaabbbcccccdd。请编写程序获取其中无重复的字符组成一个新的字符串,
即:abcd。请不要改变字符的输入顺序。
 */
public class Demo07 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入字符串:");
        String s = scanner.nextLine();
        String[] split = s.split("");
        LinkedHashSet<String> strings = new LinkedHashSet<>();
        for (int i = 0; i < split.length; i++) {
            strings.add(split[i]);
        }
        System.out.println(strings);
    }
}
发布了68 篇原创文章 · 获赞 59 · 访问量 2311

猜你喜欢

转载自blog.csdn.net/Ruoice/article/details/103429502