
1.collection和List有好多相同名,但又略微不同的方法
比如Collection的add和remove返回值是bollean,而list的返回值是voidheobject。
另外collection的remove内部填写内容,而list的remove内部填写索引。
2.Collection常用方法及继承结构
public static void main(String[] args) {
Collection c = new ArrayList();
//常用方法
//1.增加 boolean add
c.add(100);
c.add(200);
c.add(300);
c.add(400);
c.add(500);
c.add(100);
c.add(100);
//2.boolean remove 这里写的是内容
c.remove(100);
c.remove(100);
c.remove(100);
//3.boolean contains
System.out.println(c.contains(100));
//5.void clear
c.clear();
//6.boolean isEmpty
System.out.println(c.isEmpty());
}
2.1 list常用方法及之前没有在意的知识点
public static void main(String[] args) {
List l = new ArrayList();//初始长度为10,1.5倍扩容
//1.增加void add
l.add(100);
l.add(200);
l.add(300);
l.add(400);
l.add(500);
l.add(100);
l.add(100);
//2.object remove注意这里写的是索引
l.remove(1);
//3.object get()
System.out.println(l.get(1));
//4.object set
l.set(1,111);
System.out.println(l.get(1));
}
2.1.1 arraylist
//注意直接通过构造方法可以将hashSet转化为arrayList
HashSet h = new HashSet();
h.add(100);
h.add(100);
List l = new ArrayList(h);
for (int i = 0; i < l.size(); i++) {
System.out.println(l.get(i));//因为hashset无序不可重复,所以只添加了一个100
}
2.1.2 linkedlist
List list2 = new ArrayList(); // 这样写表示底层你用了数组。
List list3 = new linkedList(); // 这样写表示底层你用了双向链表。
2.1.3 vector
2.2map常用方法及之前没有在意的知识
相关方法
list有set方法,添加使用的时add,而map添加和修改都通过put,因为key不可重复
Map map = new HashMap();
//1.V put()
map.put(1,'a');
map.put(2,'b');
map.put(3,'c');
map.put(4,'d');
//2.V get
System.out.println(map.get(1));
//3.void clear
map.clear();
//4.boolean isEmpty
System.out.println(map.isEmpty());
//5.boolean containsKey()和boolean containsValue
map.put(1,'a');
map.put(2,'b');
map.put(3,'c');
map.put(4,'d');
map.put(5,'e');
map.put(6,'f');
map.put(7,'d');
System.out.println("有对应的key吗"+map.containsKey(3));
System.out.println("有对应的value吗"+map.containsValue('c'));
//6.collection values
System.out.println(map.values() instanceof Collection);
//7.set keSet
Set hs = map.keySet();
for (Object i:hs) {
System.out.println(i);
}
//8.entrySet
Set set = map.entrySet();
for (Object i:set) {
System.out.print(i+" ");
}
2.2.1
package myMap.HashMap;
public class Test01 {
public static void main(String[] args) {
}
}
2.2.2
通过实现comparable实现
package myMap.TreeMap;
import java.util.Set;
import java.util.TreeSet;
public class Test {
public static void main(String[] args) {
beauty biYue = new beauty("貂蝉",19,3);
beauty xiuHua = new beauty("杨玉环",25,4);
beauty chenYu = new beauty("西施",21,1);
beauty luoYan = new beauty("王昭君",24,2);
Set theFourBeauty = new TreeSet<>();
theFourBeauty.add(biYue);
theFourBeauty.add(xiuHua);
theFourBeauty.add(chenYu);
theFourBeauty.add(luoYan);
for (beauty b:theFourBeauty) {
System.out.println(b);
}
}
}
class beauty implements Comparable{
private String name;
private int age;
private int popularity;
public beauty(){}
public beauty(String name, int age, int popularity) {
this.name = name;
this.age = age;
this.popularity = popularity;
}
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 getPopularity() {
return popularity;
}
public void setPopularity(int popularity) {
this.popularity = popularity;
}
@Override
public int compareTo(beauty o) {
if (this.getPopularity() == o.getPopularity()){
return 0;
}else {
return this.getPopularity() - o.getPopularity();
}
}
@Override
public String toString() {
return "beauty{" +
"姓名:" + name +
"知名度:" + popularity +
'}';
}
}
使用比较器
package myMap.TreeMap;
import java.util.*;
public class Test02 {
public static void main(String[] args) {
classics xiYou = new classics(1306,1);
classics shuiHu = new classics(1325,4);
classics hongLou = new classics(1623,3);
classics sanGuo = new classics(1106,2);
TreeMap map = new TreeMap<>(new classicsComparator());
map.put(xiYou,27);
map.put(shuiHu,66);
map.put(hongLou,88);
map.put(sanGuo,49);
Set s = map.keySet();
Iterator it = s.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
class classics{
private int birth;
private int popularity;
public classics( int birth, int popularity) {
this.birth = birth;
this.popularity = popularity;
}
public classics(){}
public int getBirth() {
return birth;
}
public void setBirth(int birth) {
this.birth = birth;
}
public int getPopularity() {
return popularity;
}
public void setPopularity(int popularity) {
this.popularity = popularity;
}
@Override
public String toString() {
return "classics{" +
"birth=" + birth +
", popularity=" + popularity +
'}';
}
}
class classicsComparator implements Comparator {
@Override
public int compare(classics o1, classics o2) {
return o1.getPopularity()-o2.getPopularity();
}
}
2.2.3
2.2.4
public static void main(String[] args) {
// 创建对象
Properties pro = new Properties();
// 存
pro.setProperty("username", "test");
pro.setProperty("password", "test123");
// 取
String username = pro.getProperty("username");
String password = pro.getProperty("password");
System.out.println(username);
System.out.println(password);
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)