import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args) {
// 创建
List<String> myList = new ArrayList<>(20);
// 添加元素
myList.add("a");
myList.add("b");
myList.add("c");
// for循环遍历
for (String res : myList){
System.out.println(res);
}
String[] strList = new String[myList.size()];
myList.toArray(strList);
for (int i = 0; i < strList.length; i++) {
System.out.println(strList[i]);
}
// for循环根据下标遍历
for (int i = 0; i < myList.size(); i++) {
myList.get(i);
}
// 迭代器遍历,这个方法所有collection都可以用
Iterator<String> ite = myList.iterator();
while (ite.hasNext()){
System.out.println(ite.next());
}
}
}
import org.jetbrains.annotations.NotNull;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Objects;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
// 创建
TreeSet<Integer> a = new TreeSet<>();
//添加元素
a.add(1);
a.add(12);
a.add(2);
a.add(45);
a.add(1000);
//选择器
TreeSet<C> c = new TreeSet<>(new Comparator<C>() {
@Override
public int compare(C o1, C o2) {
return o2.c - o1.c;
}
});
// 实例化对象
C a1 = new C(1);
C a2 = new C(1324);
C a3 = new C(1423);
C a4 = new C(1877);
C a5 = new C(108);
// 添加实例化的对象
c.add(a1);
c.add(a2);
c.add(a3);
c.add(a4);
c.add(a5);
// 遍历
for (C i : c) {
System.out.println(i);
}
}
}
class A implements Comparable<A>{
int a;
public A(int a){
this.a = a;
}
public A(){
}
@Override
public String toString() {
return "A{" +
"a=" + a +
‘}‘;
}
@Override
public int compareTo(@NotNull A o) {
return this.a - o.a;
}
}
class C{
int c;
public C(int c){
this.c = c;
}
@Override
public String toString() {
return "C{" +
"c=" + c +
‘}‘;
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HasMapTest {
public static void main(String[] args) {
// 创建
HashMap<Integer, String> aMap = new HashMap<>();
// 添加元素
aMap.put(1, "张三");
aMap.put(2, "李四");
aMap.put(3, "王五");
aMap.put(4, "刘六");
// 遍历
Set<Integer> keys = aMap.keySet();
for (int i : keys){
System.out.println(i+"="+aMap.get(i));
}
// 遍历
Set<Map.Entry<Integer, String>> nodes = aMap.entrySet();
for (Map.Entry<Integer, String> node : nodes){
System.out.println(node.getKey()+"="+node.getValue());
}
}
}
package oop.Demo06;
import javafx.animation.ParallelTransition;
import java.util.Properties;
public class PropertiesTest {
public static void main(String[] args) {
//创建
Properties pro = new Properties();
//添加元素
pro.setProperty("username","ltan");
pro.setProperty("password", "fjewj");
//遍历
String name = pro.getProperty("username");
String word = pro.getProperty("password");
//打印
System.out.println(name);
System.out.println(word);
}
}
原文:https://www.cnblogs.com/ltan/p/14716345.html