/** 集合 / stream 计算各公司年税总额,各公司缴税次数不同
* list<Map>
* [
* {title="粤嵌",no="123456",amount=850,month=1},
* {title="天平",no="123457",amount=1100,month=1},
* {title="粤嵌",no="123456",amount=980,month=2},
* {title="天平",no="123457",amount=1350,month=2},
* {title="钉钉",no="123458",amount=2850,month=2}
* ]
* @author Administrator
* js 练习 实现网页计算器
*
* 3. 实现选择排序
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
public class Test {
public static void main(String[] args) {
//计算各公司年税总额,各公司缴税次数不同
test1();
//排序
test3(new int[]{13,15,5,0,7,86,6,4,95,35,1,7});
}
// 计算各公司年税总额,各公司缴税次数不同
private static void test1() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
list.add(new HashMap<String, Object>() {
{
put("title", "粤嵌");
put("no", "123456");
put("quantity", 1230);
put("month", 1);
}
});
list.add(new HashMap<String, Object>() {
{
put("title", "天平");
put("no", "123457");
put("quantity", 9130);
put("month", 1);
}
});
list.add(new HashMap<String, Object>() {
{
put("title", "粤嵌");
put("no", "123456");
put("quantity", 1730);
put("month", 2);
}
});
list.add(new HashMap<String, Object>() {
{
put("title", "天平");
put("no", "123457");
put("quantity", 5530);
put("month", 2);
}
});
list.add(new HashMap<String, Object>() {
{
put("title", "天平");
put("no", "123457");
put("quantity", null);
put("month", 3);
}
});
list.add(new HashMap<String, Object>() {
{
put("title", "钉钉");
put("no", "123458");
put("quantity", 2850);
put("month", 1);
}
});
//stream操作数据
System.out.println("stream操作数据");
Map<String, Integer> companyQ = new HashMap<String, Integer>();
companyQ.putAll(streamOper(list,"粤嵌"));
companyQ.putAll(streamOper(list,"天平"));
System.out.println(companyQ);
//list集合操作数据
System.out.println("list集合操作数据");
Map<String, Integer> companyQ1 = new HashMap<String, Integer>();
companyQ1.putAll(listOper(list, "粤嵌"));
companyQ1.putAll(listOper(list, "天平"));
System.out.println(companyQ1);
}
//stream操作数据方法
private static Map<String, Integer> streamOper(List<Map<String, Object>> list, String string) {
Stream<Map<String, Object>> s = list.stream();
Stream<Map<String, Object>> company = s.filter(e -> e.get("title") == string);
Iterator<Map<String, Object>> iterator = company.iterator();
Map<String, Integer> companyQ = new HashMap<String, Integer>() {
{
put(string, 0);
}
};
while (iterator.hasNext()) {
Integer temp = (Integer) iterator.next().get("quantity");
if (temp != null)
companyQ.put(string, (companyQ.get(string) + temp));
}
return companyQ;
}
//list集合操作数据方法
private static Map<String, Integer> listOper(List<Map<String, Object>> list, String string) {
Map<String, Integer> companyQ = new HashMap<String, Integer>() {
{
put(string, 0);
}
};
for (Map<String, Object> map : list) {
if (map.get("title") == string) {
if (map.get("quantity") != null)
companyQ.put(string, (companyQ.get(string) + (Integer) map.get("quantity")));
}
}
return companyQ;
}
// 选择排序
private static void test3(int[] arr) {
System.out.println("原数组"+Arrays.toString(arr));
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println("排序后"+Arrays.toString(arr));
}
}
控制台截图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
input{
width: 646px;
height: 80px;
}
button{
width: 160px;
height: 60px;
}
.box{
width: 60%;
margin: 0px auto;
}
</style>
</head>
<body>
<div class="box">
<input type="text" id="output" value="">
<br>
<button onclick="cle()" id="" value="">清空</button>
<button onclick="bac()" id="" value="">删除最后一位</button>
<button onclick="del()" id="" value="">删除第一位</button>
<button onclick="pf()">平方</button>
<br>
<button onclick="num(this)" id="7" value="7">7</button>
<button onclick="num(this)" id="8" value="8">8</button>
<button onclick="num(this)" id="9" value="9">9</button>
<button onclick="num(this)" id="/" value="/">/</button>
<br>
<button onclick="num(this)" id="4" value="4">4</button>
<button onclick="num(this)" id="5" value="5">5</button>
<button onclick="num(this)" id="6" value="6">6</button>
<button onclick="num(this)" id="*" value="*">*</button>
<br>
<button onclick="num(this)" id="1" value="1">1</button>
<button onclick="num(this)" id="2" value="2">2</button>
<button onclick="num(this)" id="3" value="3">3</button>
<button onclick="num(this)" id="+" value="+">+</button>
<br>
<button onclick="num(this)" id="0" value="0">0</button>
<button onclick="num(this)" id="." value=".">.</button>
<button onclick="cou(this)" id="=" value="=">=</button>
<button onclick="num(this)" id="-" value="-">-</button>
</div>
<script>
function cle(){
document.getElementById("output").value = "";
};
function bac(){
var output = document.getElementById("output").value;
document.getElementById("output").value = output.substring(0,output.length-1);
};
function del(){
var output = document.getElementById("output").value;
document.getElementById("output").value = output.substring(1,output.length);
};
function num(n){
var output = document.getElementById("output").value;
document.getElementById("output").value = output + n.value;
}
function cou(n){
var output = document.getElementById("output").value;
document.getElementById("output").value = eval(output);
}
function pf(){
var output = document.getElementById("output").value;
document.getElementById("output").value = output*output;
}
</script>
</body>
</html>
web计算机界面