Skip to content

Commit 3ab7efd

Browse files
committed
阿里面试题
1 parent ddb2885 commit 3ab7efd

2 files changed

Lines changed: 131 additions & 1 deletion

File tree

pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@
4545
<artifactId>commons-lang3</artifactId>
4646
<version>3.4</version>
4747
</dependency>
48-
48+
<dependency>
49+
<groupId>com.google.guava</groupId>
50+
<artifactId>guava</artifactId>
51+
<version>19.0</version>
52+
</dependency>
4953

5054
<dependency>
5155
<groupId>ch.qos.logback</groupId>
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.crossoverjie.actual;
2+
3+
import com.google.common.io.Files;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.nio.charset.Charset;
10+
import java.util.*;
11+
12+
/**
13+
* Function:读取文件
14+
*
15+
* @author crossoverJie
16+
* Date: 05/01/2018 14:11
17+
* @since JDK 1.8
18+
*/
19+
public class ReadFile {
20+
private final static Logger LOGGER = LoggerFactory.getLogger(ReadFile.class);
21+
22+
private static List<String> content ;
23+
private static String path ="/Users/chenjie/Desktop/test.log" ;
24+
25+
/**
26+
* 查找关键字
27+
*/
28+
private static final String KEYWORD = "login" ;
29+
30+
31+
private static Map<String,Integer> countMap ;
32+
/**
33+
* 去重集合
34+
*/
35+
private static Set<SortString> contentSet ;
36+
public static void main(String[] args) {
37+
contentSet = new TreeSet<>() ;
38+
countMap = new HashMap<>(30) ;
39+
File file = new File(path) ;
40+
try {
41+
//查找
42+
sortAndFindKeyWords(file);
43+
44+
LOGGER.info(contentSet.toString());
45+
46+
} catch (IOException e) {
47+
LOGGER.error("IOException",e);
48+
}
49+
}
50+
51+
/**
52+
* 查找关键字
53+
* @param file
54+
* @throws IOException
55+
*/
56+
private static void sortAndFindKeyWords(File file) throws IOException {
57+
content = Files.readLines(file, Charset.defaultCharset());
58+
LOGGER.info(String.valueOf(content));
59+
60+
for (String value : content) {
61+
62+
boolean flag = value.contains(KEYWORD) ;
63+
if (!flag){
64+
continue;
65+
}
66+
67+
if (countMap.containsKey(value)){
68+
countMap.put(value,countMap.get(value) + 1) ;
69+
} else {
70+
countMap.put(value,1) ;
71+
}
72+
}
73+
74+
List<SortString> sorts = new ArrayList<>() ;
75+
for (String key :countMap.keySet()){
76+
SortString sort = new SortString() ;
77+
sort.setKey(key);
78+
sort.setCount(countMap.get(key));
79+
sorts.add(sort) ;
80+
}
81+
82+
for (SortString sortString : sorts) {
83+
contentSet.add(sortString) ;
84+
}
85+
}
86+
87+
private static class SortString implements Comparable<SortString>{
88+
89+
private String key ;
90+
private Integer count ;
91+
92+
public String getKey() {
93+
return key;
94+
}
95+
96+
public void setKey(String key) {
97+
this.key = key;
98+
}
99+
100+
public Integer getCount() {
101+
return count;
102+
}
103+
104+
public void setCount(Integer count) {
105+
this.count = count;
106+
}
107+
108+
109+
@Override
110+
public int compareTo(SortString o) {
111+
if (this.getCount() >o.getCount()){
112+
return 1;
113+
}else {
114+
return -1 ;
115+
}
116+
}
117+
118+
@Override
119+
public String toString() {
120+
return "SortString{" +
121+
"key='" + key + '\'' +
122+
", count=" + count +
123+
'}';
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)