set接口
- set接口存放一组唯一无序的对象
- hash Set是set接口常用是想类
- set接口不存在get()方法
显示主题列表
程序类:public class Topic {
private int id;
private String title;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}public Topic(int id, String title) {
super();
this.id = id;
this.title = title;
}
public Topic() {
super();
}
}- import java.util.HashSet;
import java.util.Iterator;
public class SetDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet<Topic> topics=new HashSet<Topic>();
Topic topic1=new Topic(1,"七夕快乐吗?");
Topic topic2=new Topic(2,"上课快乐吗?");
Topic topic3=new Topic(3,"写代码快乐吗?");
topics.add(topic1);
topics.add(topic2);
topics.add(topic3);
//添加多余的被筛掉
topics.add(topic1);
topics.add(topic2);
topics.add(topic3);
/*topic1=new Topic(1,"七夕不快乐");*/
//根据对象的内存地址来判断的
topic1.setId(1);
topic1.setTitle("我不快乐");
topics.add(topic1);
Iterator<Topic> it=topics.iterator();
while(it.hasNext()) {
System.out.println(it.next().getTitle());
}
}
}
hashMap
- 建立类:public class Goods {
private int id;
private String name;
private float price;
public Goods(int id, String name, float price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
public Goods() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Integer get(Object good) {
// TODO Auto-generated method stub
return null;
}
}
- import java.util.HashMap;
import java.util.Iterator;
public class ShopCar {
public static void main(String[] args) {
// TODO Auto-generated method stub
//键值对集合
//遍历键就可以了
//但是键是set不准重合 所以不能重合
HashMap<Goods, Integer> shopcar=new HashMap<Goods,Integer>();
Goods good1=new Goods(1,"心形石头",200);
Goods good2=new Goods(2,"巧克力",199);
Goods good3=new Goods(3,"狗粮",10);
//hashmap添加元素用put
shopcar.put(good1, 10);
shopcar.put(good2, 2);
shopcar.put(good3, 21);
//无序集合遍历的数组是随机出现的
for(Goods good:shopcar.keySet()) {
System.out.println(good.getName()+"\t"+good.getPrice()+"\t"+shopcar.get(good).intValue());
}
/*Iterator<Goods> it=shopcar.keySet().iterator();
while (it.hasNext()) {
Goods it1=it.next();
System.out.println(it1.getName()+"\t"+it1.getPrice()+"\t"+shopcar.get(it1).intValue());
}*/
System.out.println("**************delete*****************");
shopcar.remove(good1);
Iterator<Goods> it2=shopcar.keySet().iterator();
while (it2.hasNext()) {
Goods it1=it2.next();
System.out.println(it1.getName()+"\t"+it1.getPrice()+"\t"+shopcar.get(it1).intValue());
}
}
}
算法collections工具类
- 实例:import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
public class CollationsDemo {
public static void main(String[] args) {
ArrayList<Integer> list1=new ArrayList<Integer>();
list1.add(100);
list1.add(78);
list1.add(87);
list1.add(1);
for (Integer n:list1) {
System.out.println(n);
}
System.out.println("**************sorted(排序)*************");
Collections.sort(list1);
for (Integer n:list1) {
System.out.println(n);
}
System.out.println("**************reverse(翻转)*************");
Collections.reverse(list1);
/*for (Integer n:list1) {
System.out.println(n);
}*/
Iterator<Integer> iterator=list1.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println("**************字符串集合*************");
ArrayList<String> stringset=new ArrayList<String>();
stringset.add("qb");
stringset.add("Qb");
stringset.add("qB");
stringset.add("QB");
System.out.println("**************没有排序*************");
Iterator<String> iterator1=stringset.iterator();
while(iterator1.hasNext()) {
System.out.println(iterator1.next());
}
System.out.println("**************排序后*************");
Collections.sort(stringset);
Iterator<String> iterator2=stringset.iterator();
while(iterator2.hasNext()) {
System.out.println(iterator2.next());
}
}
}
1608

被折叠的 条评论
为什么被折叠?



