package com;
// if分支
public class Java01 {
????public static void main(String[] args) {
//实现m1方法:返回3个数中的最大值
????????int max = m1(2,3,4);
????????System.out.println(max);
????}
????public static int m1(int i, int i1, int i2) {
????????return 0;
????}
}
package com;
public class Java02 {
????public static void main(String[] args) {
//实现m2方法:返回数组中的最大值
????????int[] arr = {5,2,8,4,3};
// ???????int max = m2(arr);
// ???????System.out.println(max);
????}
}
package com;
public class Java03 {
????public static void main(String[] args) {
//实现m3方法:返回数组中年龄最大的人
Person[] arr = {new Person("张三", 30), new Person("李四", 40),new Person("王五", 50)};
// ???????Person max = m3(arr);
// ???????System.out.println(max);
????}
}
class Person {
????String name;
????int age;
????public Person(String name, int age) {
????????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 "Person{" +
????????????????"name='" + name + '\'' +
????????????????", age=" + age +
????????????????'}';
????}
}
package com;
public class Java04 {
????public static void main(String[] args) {
//实现m4方法:返回数组中年龄最大的年龄
Person[] arr = {new Person("张三", 30), new Person("李四", 40),new Person("王五", 50)};
// ???????int max = m4(arr);
// ???????System.out.println(max);
????}
}
package com;
public class Java05 {
????public static void main(String[] args) {
//实现m5方法:返回数组中年龄最大的人的名字
Person[] arr = {new Person("张三", 30), new Person("李四", 40),new Person("王五", 50)};
????????String max = m5(arr);
????????System.out.println(max);
????}
????private static String m5(Person[] arr) {
????????Person person = arr[0];
????????for (int i = 0; i < arr.length; i++) {
????????????if (arr[i].getAge() > person.getAge()) {
????????????????person = arr[i];
????????????}
????????}
????????return person.getName();
????}
}
package com;
import java.util.ArrayList;
import java.util.List;
public class Java06 {
????public static void main(String[] args) {
//实现m6方法:根据年龄返回人的对象、如果找不到返回空
????????List<Person> list = new ArrayList<>();
list.add(new Person("张三", 30));
list.add(new Person("李四", 40));
list.add(new Person("王五", 50));
????????Person person = m6(list, 40);
????????System.out.println(person);
????}
????private static Person m6(List<Person> list, int i) {
????????Person person = null;
????????for (Person per:list) {
????????????if (per.getAge() == i) {
????????????????person = per;
????????????}
????????}
????????return person;
????}
}
package com;
import java.util.ArrayList;
import java.util.List;
public class Java07 {
????public static void main(String[] args) {
//实现m7方法:根据名字和年龄返回人的对象、如果找不到返回空
????????List<Person> list = new ArrayList<>();
list.add(new Person("张三", 30));
list.add(new Person("李四", 40));
list.add(new Person("王五", 50));
// ???????Person person = m7(list, "王五", 50);
// ???????System.out.println(person);
????}
}
package com;
import java.util.ArrayList;
import java.util.List;
public class Java08 {
????public static void main(String[] args) {
//实现m8方法:返回年龄大于20岁的人的集合
????????List<Person> list = new ArrayList<>();
list.add(new Person("张三", 30));
list.add(new Person("李四", 40));
list.add(new Person("王五", 50));
// ???????List<Person> persons = m8(list, 20);
// ???????persons.forEach(System.out::println);
????}
}
package com;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Java09 {
????public static void main(String[] args) {
//实现m9方法:以名字为key、以对象为value返回map集合
????????List<Person> list = new ArrayList<>();
list.add(new Person("张三", 30));
list.add(new Person("李四", 40));
list.add(new Person("王五", 50));
????????Map<String, Person> map = m9(list);
????????map.forEach((k,v)->{
????????????System.out.println(k + v);
????????});
????}
????private static Map<String,Person> m9(List<Person> list) {
????????Map<String,Person> map = new HashMap<>();
????????list.forEach(person -> {
????????????map.put(person.getName(), person);
????????});
????????return map;
????}
}
package com;
import com.a.Company;
import java.util.List;
public class Java10 {
public static void main(String[] args) {
// 根据需求写出实现代码
? ? ? ? // 创建公司
? ? ? ? Company c =new Company("IBM");
// 创建员工
? ? ? ? Employee e1 =new Employee("张三",30,3000);
Employee e2 =new Employee("李四",40,4000);
Employee e3 =new Employee("王五",50,5000);
// 公司添加员工(集合)
? ? ? ? c.add(e1);
c.add(e2);
c.add(e3);
c.add(new Employee("赵六",60,6000));
// 打印员工信息
? ? ? ? c.printAll();
// 找出年龄最大员工
? ? ? ? Employee x =c.m1();
// 找出工资最低工资
? ? ? ? Employee y =c.m2();
// 找出李四员工
? ? ? ? Employee z =c.m3("李四");
// 求平均工资
? ? ? ? double d =c.m4();
// 找工资高于平均工资的员工
? ? ? ? Listlist =c.m5();
}
}
package com.a;
import com.Employee;
import java.util.ArrayList;
import java.util.List;
public class Company {
String name;
Listlist =new ArrayList<>();
public Company() {
}
public Company(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
? ? public String toString() {
return "Company{" +
"name='" +name +'\'' +
'}';
}
public void add(Employee e1) {
list.add(e1);
}
public void printAll() {
}
public Employee m1() {
return null;
}
public Employee m2() {
return null;
}
public Employee m3(String name) {
return null;
}
public double m4() {
double s =0;
for (Employee emp:list) {
s +=emp.getSalary();
}
return s /list.size();
}
public Listm5() {
Listx =new ArrayList<>();
list.forEach(employee -> {
if (employee.getSalary() > m4()) {
x.add(employee);
}
});
return x;
}
}