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

Popular posts from this blog

delphi - Disable and change color of node in Treeview -

elasticsearch python client - work with many nodes - how to work with sniffer -

unity3d - Rotate an object to face an opposite direction -