您的位置:首页 > 科技 > IT业 > 苏州论坛建站模板_编程培训费用_关联词有哪些四年级_链接生成二维码

苏州论坛建站模板_编程培训费用_关联词有哪些四年级_链接生成二维码

2024/11/19 3:30:36 来源:https://blog.csdn.net/2301_81819439/article/details/143248225  浏览:    关键词:苏州论坛建站模板_编程培训费用_关联词有哪些四年级_链接生成二维码
苏州论坛建站模板_编程培训费用_关联词有哪些四年级_链接生成二维码

写完一定记得 Ctrl+Alt+L 让代码格式标准

1.使用List和Map存放多个图书信息,遍历并输出。其中商品属性:编号,名称,单价,出版社;使用商品编号作为Map中的key。

  • Books类

    package set.saturdayPlan;public class Books {private Integer id;//编号private String name;//名称private Double price;//价格private String publishingHouse;//出版社//无参构造public Books() {}//有参构造public Books(Integer id, String name, Double price, String publishingHouse) {this.id = id;this.name = name;this.price = price;this.publishingHouse = publishingHouse;}//get , set 方法public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}public String getPublishingHouse() {return publishingHouse;}public void setPublishingHouse(String publishingHouse) {this.publishingHouse = publishingHouse;}//重写toString@Overridepublic String toString() {return "Books{" +"id=" + id +", name='" + name + '\'' +", price=" + price +", publishingHouse='" + publishingHouse + '\'' +'}';}}
    
  • List    

    package set.saturdayPlan;import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;public class BooksTextList {//程序入口public static void main(String[] args) {//创建List集合对象List<Books> books = new ArrayList<>();//添加图书信息books.add(new Books(123, "西游记", 60.5, "清华出版社"));books.add(new Books(124, "红楼梦", 70.5, "北京出版社"));books.add(new Books(126, "水浒传", 70.2, "南京出版社"));books.add(new Books(125, "三国演义", 80.9, "山东出版社"));//遍历ListSystem.out.println("===============================================================");//方法一 forfor (int i = 0; i < books.size(); i++) {System.out.println(books.get(i));}System.out.println("===============================================================");//方法二 增强forfor (Books book : books) {System.out.println(book);}System.out.println("===============================================================");//方法三 迭代器Iterator<Books> iterator = books.iterator();while (iterator.hasNext()) {Books books1 = iterator.next();System.out.println(books1);}}
    }
    

    正常写一个遍历就可以。

  • Map

  1. 直接使用Map
    package set.saturdayPlan;import java.util.*;public class BooksTextMap {//程序入口public static void main(String[] args) {//创建Map集合对象Map<Integer, Books> map = new TreeMap<>();//输入图书信息Books boo1 = new Books(123, "西游记", 60.5, "清华出版社");Books boo2 = new Books(124, "红楼梦", 70.5, "北京出版社");Books boo3 = new Books(126, "水浒传", 70.2, "南京出版社");Books boo4 = new Books(125, "三国演义", 80.9, "山东出版社");//存储到Map集合 使用商品编号作为Map中的keymap.put(boo1.getId(), boo1);map.put(boo2.getId(), boo2);map.put(boo3.getId(), boo3);map.put(boo4.getId(), boo4);//遍历MapSystem.out.println("=======================================================================");//方法一 keySet() 增强forSet<Integer> set = map.keySet();for (Integer integer : set) {System.out.println(map.get(integer));}System.out.println("=======================================================================");//方法二 keySet() 迭代器Iterator<Integer> iterator = map.keySet().iterator();while (iterator.hasNext()) {Integer integer = iterator.next();System.out.println(map.get(integer));}System.out.println("=======================================================================");//方法三 values() 增强forCollection<Books> values = map.values();for (Books value : values) {System.out.println(value);}System.out.println("=======================================================================");//方法四 values() 迭代器Iterator<Books> iterator1 = map.values().iterator();while (iterator1.hasNext()) {Books value = iterator1.next();System.out.println(value);}System.out.println("=======================================================================");//方法五 entrySet() 增强forSet<Map.Entry<Integer, Books>> entries = map.entrySet();for (Map.Entry<Integer, Books> entry : entries) {System.out.println(entry.getValue());}System.out.println("=======================================================================");//方法六 迭代器Iterator<Map.Entry<Integer, Books>> iterator2 = map.entrySet().iterator();while (iterator2.hasNext()) {Map.Entry<Integer, Books> entry = iterator2.next();System.out.println(entry.getValue());}}
    }
    

    正常写一个遍历就可以。

  2. LIst   To  Map
    //List To MapMap<Integer, Books> map = new TreeMap<>();Iterator<Books> iterator1 = books.iterator();while (iterator1.hasNext()) {Books books1 = iterator1.next();map.put(books1.getId(), books1);//使用商品编号作为Map中的key}//遍历MapSystem.out.println("==================================================================");//方法一  keySet()增强forSet<Integer> set = map.keySet();for (Integer integer : set) {System.out.println(map.get(integer));}System.out.println("==================================================================");//方法二 keySet()迭代器Iterator<Integer> iterator2 = map.keySet().iterator();while (iterator2.hasNext()) {Integer ks2 = iterator2.next();System.out.println(map.get(ks2));}System.out.println("==================================================================");//方法三 values()增强forCollection<Books> values = map.values();for (Books value : values) {System.out.println(value);}System.out.println("==================================================================");//方法四 values()迭代器Iterator<Books> iterator3 = map.values().iterator();while (iterator3.hasNext()) {Books value2 = iterator3.next();System.out.println(value2);}System.out.println("==================================================================");//方法五 entrySet()增强forSet<Map.Entry<Integer, Books>> entries = map.entrySet();for (Map.Entry<Integer, Books> entry : entries) {System.out.println(entry.getValue());}System.out.println("==================================================================");//方法六 entrySet()迭代器Iterator<Map.Entry<Integer, Books>> iterator4 = map.entrySet().iterator();while (iterator4.hasNext()) {Map.Entry<Integer, Books> entry2 = iterator4.next();System.out.println(entry2.getValue());}

    在List类的基础上,不对List集合进行遍历,将List集合中的存储到Map集合中,对Map集合进行遍历,正常写一个遍历就可以。

 2.使用HashSet和TreeSet存储多个商品信息,遍历并输出;其中商品属性:编号,名称,单价,出版社;要求向其中添加多个相同的商品,验证集合中元素的唯一性。

  提示:向HashSet中添加自定义类的对象信息,需要重写hashCode和equals( )

             向TreeSet中添加自定义类的对象信息,需要实现Comparable接口,指定比较规则

  • HashSet----Books类

      //重写equals()和hashCode()@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Books books = (Books) o;return Objects.equals(id, books.id) && Objects.equals(name, books.name) && Objects.equals(price, books.price) && Objects.equals(publishingHouse, books.publishingHouse);}@Overridepublic int hashCode() {return Objects.hash(id, name, price, publishingHouse);}

    与上一题所需属性相同,沿用上一题的Books类进行修改,重写hashCode和equals( )

  • HashSet

    package set.saturdayPlan;import com.jr.demo1.Book;import java.util.*;public class BooksTextHashSet {//程序入口public static void main(String[] args) {//创建HashSet集合对象HashSet<Books> books = new HashSet<>();//输入商品信息books.add(new Books(123, "西游记", 60.5, "清华出版社"));books.add(new Books(124, "红楼梦", 70.5, "北京出版社"));books.add(new Books(126, "水浒传", 70.2, "南京出版社"));books.add(new Books(125, "三国演义", 80.9, "山东出版社"));//遍历Set//方法一 增强forfor (Books book : books) {System.out.println(book);}System.out.println("=================================================================");//方法二 迭代器Iterator<Books> iterator = books.iterator();while (iterator.hasNext()) {Books books1 = iterator.next();System.out.println(books1);}}
    }
    

    正常写一个遍历就可以。

  • TreeSet----Books类    【方法一】

      //内部比较器@Overridepublic int compareTo(Books o) {return this.id-o.id;//根据编号排序}
    

    与上一题所需属性相同,沿用上一题的Books类进行修改,重写内部比较器comparaTo方法,调用内部比较器Comparable接口。

  • TreeSet   【方法一】

    package set.saturdayPlan;import java.util.*;public class BooksTextTreeSet {//程序入口public static void main(String[] args) {//创建TreeSet集合对象TreeSet<Books> books = new TreeSet<>();//输入商品信息books.add(new Books(123, "西游记", 60.5, "清华出版社"));books.add(new Books(124, "红楼梦", 70.5, "北京出版社"));books.add(new Books(126, "水浒传", 70.2, "南京出版社"));books.add(new Books(125, "三国演义", 80.9, "山东出版社"));//遍历Set//方法一 增强forfor (Books book : books) {System.out.println(book);}System.out.println("=================================================================");//方法二 迭代器Iterator<Books> iterator = books.iterator();while (iterator.hasNext()) {Books books1 = iterator.next();System.out.println(books1);}}
    }
    
  • TreeSet----Books类 【方法二】

沿用上一题Books类,不需要做任何改变。

  • TreeSet----外部比较器Comparator   【方法二】

    package set.saturdayPlan;import java.util.Comparator;public class BooksComparator implements Comparator<Books> {//方法重写@Overridepublic int compare(Books o1, Books o2) {/* return o1.getId()-o2.getId();*/  //更简单if (o1.getId().equals(o2.getId())) {  //更严谨return 0;} else {return o1.getId() - o2.getId();}}
    }
    
  • TreeSet  【方法二】

 //创建TreeSet集合对象TreeSet<Books> books = new TreeSet<>(new BooksComparator());//外部比较器

与方法一TreeSet相同,只在创建集合对象时,加入外部比较器Comparator。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com