集合:
package cn.itheima.collection;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
/**
* 1、一个User类有String name int age ArrayList<User> 存储数据然后按 年龄倒序打出
*
*/
public class Test1 {
public static void main(String[] args) {
ArrayList<User> list=new ArrayList<User>();
list.add(new User("张三", 23));
list.add(new User("钱七", 27));
list.add(new User("王武", 25));
list.add(new User("李四", 24));
list.add(new User("赵六", 26));
toString(list);
}
public static void toString(List<User> list) {
TreeSet<User> ts=new TreeSet<User>(new Comparator<User>() {
@Override
public int compare(User u1, User u2) {
int num=u2.getAge()-u1.getAge();
return num==0?u2.getName().compareTo(u1.getName()):num;
}
});
ts.addAll(list);
list.clear();
list.addAll(ts);
for (User user : list) {
System.out.println(user.getName()+"---"+user.getAge());
}
}
}
package cn.itheima.collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
/*
* 2、画出collection集合体系 说出MAP集合两种遍历方式 并用代码体现
*
* Collection:
* ||--list:
* ||--ArrayList
* ||--LinkedList
* ||--Vector
* ||--Set:
* ||--HashSet
* ||--LinkedHashSet
* ||--TreeSet
*
*/
public class Test2 {
public static void main(String[] args) {
//demo1();
TreeMap<User, String> hm=new TreeMap<User, String>(new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
int num=o2.getAge()-o1.getAge();
return num==0?o2.getName().compareTo(o1.getName()):num;
}
});
hm.put(new User("张三", 23), "北京");
hm.put(new User("赵六", 26), "深圳");
hm.put(new User("李四", 24), "上海");
hm.put(new User("王武", 25), "广州");
//迭代器
Iterator<User> it=hm.keySet().iterator();
while(it.hasNext()){
User user=it.next();
String s=hm.get(user);
System.out.println(user.getName()+"--"+user.getAge()+"--"+s);
}
System.out.println("--------------");
//增强for
for(Map.Entry<User, String> entry:hm.entrySet()){
User user=entry.getKey();
String s=entry.getValue();
System.out.println(user.getName()+"--"+user.getAge()+"--"+s);
}
}
private static void demo1() {
HashMap<User, String> hm=new HashMap<User, String>();
hm.put(new User("张三", 23), "北京");
hm.put(new User("李四", 24), "上海");
hm.put(new User("王武", 25), "广州");
hm.put(new User("赵六", 26), "深圳");
//迭代器
Iterator<User> it=hm.keySet().iterator();
while(it.hasNext()){
User user=it.next();
String s=hm.get(user);
System.out.println(user.getName()+"--"+user.getAge()+"--"+s);
}
System.out.println("--------------");
//增强for
for(Map.Entry<User, String> entry:hm.entrySet()){
User user=entry.getKey();
String s=entry.getValue();
System.out.println(user.getName()+"--"+user.getAge()+"--"+s);
}
}
}
package cn.itheima.collection;
public class User {
private String name;
private int age;
public User() {
super();
}
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
package cn.itheima.collection;
import java.util.Map;
import java.util.TreeMap;
/*
*3. 给一个“kdiekmckskelfcr” 统计每个字符出现的次数按字典顺序 例如 a(3)b(4)j(4)
*/
public class Test3 {
public static void main(String[] args) {
String str="kdiekmckskelfcr";
TreeMap<Character, Integer> tm=new TreeMap<Character, Integer>();
char[] arr=str.toCharArray();
for (char c : arr) {
//方式一:根据键
//tm.put(c, tm.get(c)==null?1:tm.get(c)+1);
//方式二:根据值
tm.put(c, !tm.containsValue(c)?1:tm.get(c)+1);
}
StringBuilder sb=new StringBuilder();
for(Map.Entry<Character, Integer> entry:tm.entrySet()){
sb.append(entry.getKey()).append("(").append(entry.getValue()).append(")");
}
System.out.println(sb.toString());
}
}
package cn.itheima.collection;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
/*
* 4.编写代码,先实现给ArrayList<Integer>去重,然后按照倒序排序
*/
import java.util.TreeSet;
public class Test4 {
public static void main(String[] args) {
ArrayList<Integer> list=new ArrayList<Integer>();
list.add(11);
list.add(12);
list.add(16);
list.add(15);
list.add(20);
list.add(11);
list.add(12);
list.add(13);
list.add(13);
System.out.println("去重后:");
getSingle(list);
System.out.println("排序后");
sort(list);
}
private static void sort(List<Integer> list) {
TreeSet<Integer> ts=new TreeSet<Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int num=o2.compareTo(o1);
return num;
}
});
ts.addAll(list);
list.clear();
list.addAll(ts);
System.out.println(list);
}
private static void getSingle(List<Integer> list) {
HashSet<Integer> hs=new HashSet<Integer>();
hs.addAll(list);
list.clear();
list.addAll(hs);
System.out.println(list);
}
}
package cn.itheima.collection;
import java.util.ArrayList;
import java.util.Scanner;
/*
* 5.写一个方法体public void deleteNum(ArrayList<String> list) ,
* 要求凡是list成员中包含(0-9)的均删除
*/
public class Test5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();
System.out.println("输入字符串:");
while (list.size() < 5) {
String s=sc.nextLine();
list.add(s);
}
deleteNum(list);
System.out.println(list);
}
public static void deleteNum(ArrayList<String> list) {
for(int i=0;i<list.size();i++){
char[] arr=list.get(i).toCharArray();
for (char c : arr) {
if (c>='0'&&c<='9') {
list.remove(i--);
break;
}
}
}
}
} |
|