java - How to remove duplicate lines from a text file -
am new java! want remove duplicate lines file ,but i need keep lines more number of capital letters
which function can use achieve this
if text file contains following words
input
car
car
bus
bus
bike
expected output
car
bus
bike
try this.
static class str { final string origin; final int uppers; str(string origin) { this.origin = origin; this.uppers = (int)origin.chars() .filter(character::isuppercase) .count(); } } public static list<string> uniq(string file) throws ioexception { path path = paths.get(file); list<string> lines = files.readalllines(path); map<string, str> map = new linkedhashmap<>(); (string e : lines) { str n = new str(e); map.compute(e.tolowercase(), (k, v) -> v == null || n.uppers > v.uppers ? n : v); } return map.values().stream() .map(s -> s.origin) .collect(collectors.tolist()); }
and
system.out.println(uniq("test.txt"));
result:
[car, bus, bike]
Comments
Post a Comment