Collections是一个工具类,而Collection是一个接口,Collections帮助我们对Set,List,Map进行排序,查找,填充的辅助方法
package cn.com.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestCollections {
public static void main(String[] args) {
//Collection是一个接口,而Collectios是一个工具类
List<String> s = new ArrayList<>();
for (int i = 0; i <5; i ) {
s.add("小红:" i);
}
System.out.println(s);
//随机排列元素
Collections.shuffle(s);
System.out.println(s);
//逆序
Collections.reverse(s);
System.out.println(s);
//排序
Collections.sort(s);
System.out.println(s);
System.out.println(Collections.binarySearch(s,"小红:4"));
//用特定的对象重写整个容器
Collections.fill(s," i love you");
System.out.println(s);
}
}
表格存储数据,Map和List结合存储表格,也就是ORM的思想,对象关系映射
package cn.com.collection;
import java.util.*;
public class TestStoreData {
public static void main(String[] args) {
//测试表格数据的存储,ORM的思想 对象关系映射
Map<String,Object> map1 = new HashMap<>();
map1.put("id",100);
map1.put("姓名","小红");
map1.put("年龄",18);
map1.put("薪水",2000);
map1.put("入职日期","2018.9.1");
Map<String,Object> map2 =new HashMap<>();
map2.put("id",200);
map2.put("姓名","小亮");
map2.put("年龄",16);
map2.put("薪水",1500);
map2.put("入职日期","2016.3.3");
Map<String,Object> map3 = new HashMap<>();
map3.put("id",300);
map3.put("姓名","小军");
map3.put("年龄",17);
map3.put("薪水",3500);
map3.put("入职日期","2008.5.1");
Map<String,Object> map4 = new HashMap<>();
map4.put("id",400);
map4.put("姓名","小琪");
map4.put("年龄",20);
map4.put("薪水",2000);
map4.put("入职日期","2008.6.1");
Map<String,Object> map5 = new HashMap<>();
map5.put("id",500);
map5.put("姓名","小明");
map5.put("年龄",19);
map5.put("薪水",1000);
map5.put("入职日期","2019.10.1");
List<Map<String,Object>> list = new LinkedList<>();
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
// Iterator()遍历
for (Map<String,Object> aa:list) {
Set<Map.Entry<String,Object>> a = aa.entrySet();
for (Iterator<Map.Entry<String,Object>> iter = a.iterator(); iter.hasNext(); ) {
Map.Entry<String,Object> tmp = iter.next();
System.out.print(tmp "\t");
}
System.out.println();
}
// for (Map<String,Object> bb: list) {
// Set<String> a = bb.keySet();
// for (Iterator<String> c = a.iterator();c.hasNext() ;) {
// String tmp = c.next();
// System.out.print(tmp ":" bb.get(tmp) "\t");
// }
// System.out.println();
// }
}
}
表格存储数据:javabean和List/Map结合存储表格,也是使用ORM的思想,对象关系映射
package cn.com.collection;
import java.util.*;
public class TestSortData2 {
public static void main(String[] args) {
Persion p1 = new Persion(100,"小红",18,2000,"2019.8.12");
Persion p2 = new Persion(200,"小明",19,1500,"2016.5.20");
Persion p3 = new Persion(300,"小花",16,3600,"2020.1.1");
Persion p4 =new Persion(400,"小军",20,4500,"2018.9.9");
// List<Persion> list = new LinkedList<>();
// list.add(p1);
// list.add(p2);
// list.add(p3);
// list.add(p4);
// for (Persion p : list) {
// System.out.println(p);
// }
Map<Integer,Persion> m = new HashMap<>();
m.put(100,p1);
m.put(200,p2);
m.put(300,p3);
m.put(400,p4);
// Set<Map.Entry<Integer,Persion>> a = m.entrySet();
// for (Iterator<Map.Entry<Integer,Persion>> b = a.iterator();b.hasNext() ; ) {
// Map.Entry<Integer,Persion> tmp= b.next();
// System.out.println(tmp);
// }
Set<Integer> xx = m.keySet();
for (Iterator<Integer> z = xx.iterator();z.hasNext();) {
Integer tmp = z.next();
System.out.println(tmp "======" m.get(tmp));
}
}
}
class Persion {
private int id;
private String name;
private int age;
private int salary;
private String indate;
//要有默认的无参构造器
public Persion() {
}
public int getId() {
return id;
}
public Persion(int id, String name, int age, int salary, String indate) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
this.indate = indate;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getIndate() {
return indate;
}
public void setIndate(String indate) {
this.indate = indate;
}
@Override
public String toString() {
return "id:" id ",姓名:" name ",年龄:" age ",薪水:" salary ",入职日期:" indate;
}
}
来源:https://www./content-4-388301.html
|