package com.xdf.collection;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import org.junit.Test;
public class CollectionDemo {
/**
* Collections 所有集合类的工具类
*/
@Test
public void test01() {
List<String> list = new ArrayList<>();
list.add("z");
list.add("a");
list.add("v");
list.add("b");
System.out.println("没有排序====》" + list);
// 01给集合升序
Collections.sort(list);
System.out.println("升序====》" + list);
// 02 给集合降序
Collections.reverse(list);
System.out.println("降序====》" + list);
// 03查询某个元素在集合中的位置 必须先升序排序
Collections.sort(list);
int index = Collections.binarySearch(list, "v");
System.out.println("元素v存在的位置是===>" + index);
// 04随机排序
Collections.shuffle(list);
System.out.println("随机排序==》" + list);
}
/**
* 实现对 对象的排序
*/
@Test
public void test02() {
List<Student> list = new ArrayList<>();
list.add(new Student(50, "小黑50"));
list.add(new Student(10, "小黑10"));
list.add(new Student(80, "小黑80"));
list.add(new Student(20, "小黑20"));
list.add(new Student(60, "小黑60"));
Collections.sort(list);
for (Student student : list) {
System.out.println(student);
************************************************
package com.xdf.collection;
public class PonitArray {
/**
* 01.可以传递0 或者 N个 int类型的数据
* 02.怎么操作数组 怎么操作...
* 03.必须位于参数列表中的最后一个位置
*/
public static void main(String[] args) {
String[] sales = { "啤酒", "饮料", "矿泉水" };
slaeSometing("小粉");
}
/**
* @param name 谁 在 出售 什么 东西
* @param something 出售的东西
*/
private static void slaeSometing(String name, String... something) {
System.out.println(name + "在出售");
for (int i = 0; i < something.length; i++) {
System.out.print(something[i] + ",");
}
}
}
**********************************************
package com.xdf.collection;
//只有实现Comparable接口的类 才能被 排序!
public class Student implements Comparable {
private int id; // 学生编号
private String name;
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 Student() {
super();
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
// 按照自己的规则 去进行 排序
@Override
public int compareTo(Object o) {
if (o instanceof Student) {
Student stu = (Student) o;
if (stu.getId() == this.id) {
return 0;
} else if (stu.getId() < this.id) {
return -1;
} else {
return 1;
}
} else {
return 0;
*********************************************
package com.xdf.linkdemo;
/**
* 单链表
*/
public class MyLink {
private int data;// 存储链表的值
private MyLink next;// 指针指向下一个数据
public MyLink(int i) { // 创建带参构造
data = i;
}
// 向链表中增加值
public void add(MyLink link) {
link.next = next;
next = link;
}
private void append(MyLink myLink) {
MyLink x = this;
while (x.next != null) {
x = x.next;
}
x.next = myLink;
}
private void show() {
MyLink x = this;
while (x != null) {
System.out.println(x.data);
x = x.next;
}
}
public static void main(String[] args) {
MyLink link = new MyLink(10);
link.append(new MyLink(20));// 拼接
link.append(new MyLink(30));// 拼接
link.add(new MyLink(40)); // 新增
link.add(new MyLink(60)); // 新增
link.append(new MyLink(50));// 拼接
link.append(new MyLink(70));// 拼接
link.show();
*************************************************
package com.xdf.list;
import java.util.ArrayList;
import java.util.List;
public class ArrayListDemo {
/**
* public ArrayList() {
this.elementData = EMPTY_ELEMENTDATA;
}
private static final Object[] EMPTY_ELEMENTDATA = {};
private transient Object[] elementData;
我们在执行add();底层默认初始化了一个长度为10的数组
Object[] elementData=new Objct[10];
public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
*/
public static void main(String[] args) {
// 创建ArrayList
List arraylist = new ArrayList();
// 向集合中增加数据
arraylist.add(1);
arraylist.add("2");
arraylist.add(50.0);
arraylist.add(4);
arraylist.add(5);
arraylist.add(6);
arraylist.add(7);
arraylist.add(8);
arraylist.add(9);
arraylist.add(10);
System.out.println("============================");
arraylist.add(11);
// 输出集合的长度 集合中有几个数据
System.out.println("集合的长度==》" + arraylist.size());
// 在指定的位置新增数据
arraylist.add(1, "哈哈");
// 从集合中删除数据
arraylist.remove("哈哈");
// 判断集合是否为空
System.out.println("集合是否为空===》" + arraylist.isEmpty());
// 遍历集合中每一个元素
for (Object object : arraylist) {
System.out.println(object);
}
*************************************************
package com.xdf.list;
import java.util.ArrayList;
import java.util.List;
public class Demo {
/**
* 泛型集合:在创建集合的时候,规定了集合中能存放的数据类型!
* int [] num={"1"}; 错误
*
*/
public static void main(String[] args) {
// 集合中只能存放String类型的数据
List<String> list = new ArrayList<String>();
list.add("1.0");
// 创建5个User对象
User user1 = new User(10, "小黑1");
User user2 = new User(20, "小黑2");
User user3 = new User(30, "小黑3");
User user4 = new User(40, "小黑4");
User user5 = new User(50, "小黑5");
// 创建集合保存User
List<User> users = new ArrayList<User>();
users.add(user1);
users.add(user2);
users.add(user3);
users.add(user4);
users.add(user5);
for (User user : users) {
System.out.println(user.getName());
}
******************************************************
package com.xdf.list;
import java.util.Set;
import java.util.TreeSet;
public class HashSetDemo {
/**
*
* HashSet set = new HashSet();
set.add("小黑");
set.add("小黑");
set.add(new String("小黑"));
set.add(new String("小黑"));
System.out.println(set.size());
HashSet<String> set = new HashSet<String>();
set.add("小黑");
set.add("小黑");
System.out.println(set);
HashSet<User> set = new HashSet<User>();
User user1 = new User(1, "小黑");
User user2 = new User(1, "小黑");
set.add(user1);
set.add(user2);
System.out.println(set.size());
HashSet<String> set = new HashSet<String>();
set.add("小黑1");
set.add("小黑2");
set.add("小黑3");
set.add("小黑4");
set.add("小黑5");
for (String string : set) {
System.out.println(string);
}
*/
public static void main(String[] args) {
Set<Integer> set = new TreeSet<Integer>();
set.add(20);
set.add(10);
set.add(2);
set.add(200);
set.add(120);
for (Integer i : set) {
System.out.println(i);
}
System.out.println("===========================");
Set<String> set1 = new TreeSet<String>();
set1.add("a");
set1.add("n");
set1.add("z");
set1.add("b");
set1.add("abc");
for (String i : set1) {
System.out.println(i);
}
*******************************************
package com.xdf.list;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add(1);
list.add(10);
list.add(100);
// 在链表的最前面新增数据
list.addFirst(10000);
list.removeLast();
for (Object object : list) {
System.out.println(object);
}
}
}
******************************************
package com.xdf.list;
public class User { // 用户
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(int age, String name) {
super();
this.age = age;
this.name = name;
}
public User() {
super();
}
@Override
public String toString() {
return "User [age=" + age + ", name=" + name + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof User) {
User user = (User) obj;
return this.name.equals(user.getName())
&& this.age == user.getAge();
}
return false;
}
@Override
public int hashCode() {
return 100;
******************************************
package com.xdf.map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class MapDemo {
public static void main(String[] args) {
/**
* 创建一个Map集合
* 集合的key都是String类型
* 集合的value是Object类型
*/
Map<String, Object> map = new HashMap<>();
// 向map集合中增加数据
map.put("dog", "小狗");
map.put("cat", "小猫");
map.put("desk", "桌子");
// 从map集合中获取指定的元素 根据key 获取对应的value值
System.out.println("dog对应的value值是====》" + map.get("dog"));
Set<Entry<String, Object>> entrySet = map.entrySet();
Iterator<Entry<String, Object>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Entry<String, Object> entry = iterator.next();
System.out.println(entry.getKey() + "=====>" + entry.getValue());
}
}
********************************************
package com.xdf.tree;
/**
* 2叉排序树
*/
public class MyTree {
private int data; // 根或者是数据
private MyTree left; // 左子树
private MyTree right; // 右子树
public MyTree(int i) {
data = i;
}
private void add(MyTree tree) {
if (tree.data < this.data) {
if (left == null) {
left = tree; // 数据小放 左子树
} else {
left.add(tree);
}
} else {// 数据小放 右子树
if (right == null) {
right = tree;
} else {
right.add(tree);
}
}
}
// 遍历树的所有节点
private void show() {
if (left != null) {
left.show();
}
System.out.println(data);
if (right != null) {
right.show();
}
}
public static void main(String[] args) {
MyTree tree = new MyTree(100);
tree.add(new MyTree(80));
tree.add(new MyTree(30));
tree.add(new MyTree(70));
tree.add(new MyTree(10));
tree.show();
*********************************************
package com.xdf.tree;
import java.util.ArrayList;
import java.util.List;
public class TreeDemo {
private List<Node> list = new ArrayList<>(); // 所有子节点
class Node { // 节点
String data; // 数据
String parent; // 父级节点
}
private void add(String parent, String child) {
Node node = new Node();
node.data = child;
node.parent = parent;
list.add(node);
}
private void findParent(String child) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).data.equals(child)) {
System.out.println("父节点是==》" + list.get(i).parent);
}
}
}
/**
* 根据父节点查询所有子节点
*/
private void findChild(String parent) {
// 创建一个新集合 保存找到的所有子节点
List<String> childs = new ArrayList();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).parent.equals(parent)) { // 找到了父节点
childs.add(list.get(i).data);
}
}
for (String child : childs) {
System.out.println(child);
}
}
public static void main(String[] args) {
TreeDemo demo = new TreeDemo();
demo.add("班级", "1班");
demo.add("班级", "2班");
demo.add("班级", "3班");
demo.add("1班", "小黑1");
demo.add("1班", "小黑2");
demo.add("1班", "小黑3");
demo.add("2班", "小白1");
demo.add("2班", "小白2");
demo.add("2班", "小白3");
demo.add("3班", "小黄1");
demo.add("3班", "小黄2");
demo.add("3班", "小黄3");
demo.add("3班", "小黄4");
demo.findParent("小黑1");
demo.findChild("3班");