logo
  • Programming
  • Testing
  • AI
  • Devops
  • Data Science
  • Design
  • Blog
  • Crypto Tools
  • Dev Feed
  • Login
Story
Follow @devglan

Java Program to Find line with max character length in Descending Order

author-image   By Dhiraj Ray,   01 January, 2018 3K

Description

This java interview program is about finding lines with max character from a file in descending order.In this case, we will be using buffered reader to read a file and use java provided data structure TreeSet to hold the line and it's character length as it automatically maintains ascending order.Following is the program to find two lines with max characters in descending order.

MaxCharacterLine.java
package com.devglan;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class MaxCharacterLine {

    public static void main(String[] args) {

        BufferedReader br;
        String filePath = args[0];
        int topList;
        Set liSet = new TreeSet(new MyComp());
        try {
            br = new BufferedReader(new FileReader(new File(filePath)));
            String line = br.readLine();
            topList = Integer.parseInt(line.trim());
            while((line = br.readLine()) != null){
                line = line.trim();
                if(!"".equals(line)){
                    liSet.add(new Entries(line.length(), line));
                }
            }
            int count = 0;
            for(Entries ent:liSet){
                System.out.println(ent.line);
                if(++count == topList){
                    break;
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    public static class Entries{
        Integer length;
        String line;
        public Entries(Integer l,String line){
            length = l;
            this.line = line;
        }
    }

    public static class MyComp implements Comparator{

        @Override
        public int compare(Entries e1, Entries e2) {
            if(e2.length > e1.length){
                return 1;
            } else {
                return -1;
            }
        }

    }
}

Explanation

Click to Suggest Your Own Explanation

Read a file using BufferedReader and use TreeSet to hold the key value pair in the Entry set.Since, Entries is a custom object, implement Comparator for sorting the Entries by length property.Since, TreeSet by implmenetation maintains ascending order, the comparison in compare method is reversed.

Other Similar Java Programs:

1 Java Program to test if given number is Armstrong or not
2 Java Program to test if a given number is Fibonacci or not
3 java program to find distinct word list from a file
4 Java program to find duplicate character from a string
5 Java Program to find middle index of array where both ends sum is equal
6 Java Program to find max two numbers in an array
7 Java program to find max repeated words from a file
8 Java program to find sum of prime numbers
9 Java program to reverse a given number
10 Java program to find permutations of a given string
11 Java program to reverse a given string
12 Java program to find factorial of a given number
13 Java Program for Binary Search
14 Java Program to Add Two 2D Matrix
15 3 Ways to Check if Given Words are Anagram or not
16 Java Program to Find LCM of a Two Given Number
17 Check Given String is Rotation of Another String
18 Java Program To Check If A Given Number is A Perfect Number
19 Remove Common Characters From Given Strings
20 Find Second Largest Number in Array
21 Java Program To Find the Longest Palindrome Present in a String
22 Java Program to Reverse an Array in Place Without Using Any Second Array
23 Java Program to Print 1 To 10 Without Using Loop
24 Write a Java Program to Compare Files in Java
25 Java Program to Find missing Number in an Array
26 Java Program to Find First non Repeated Character in a String
27 Write a Java Program to Find Union and Intersection of Arrays in Java
28 Writing a Java program to rotate an array by d elements.
29 Write a Java program to rotate a matrix
30 Write a Java program to find the largest sum of the contiguous subarray in a given Array

If You Appreciate This, You Can Consider:

  • Like us at: Facebook or follow us at Twitter
  • Share this article on social media or with your teammates.

Suggest Explanation

The suggestion has been saved for review.Thanks for your effort.

{{errorMessage}}

Vertex

Devglan is one stop platform for all
programming tutorials and courses.

About Us

  • About Us
  • Contact Us
  • Submission Criteria
  • Privacy Policy

Quick Links

  • Home
  • Login / Join
  • Submit Your Story
  • Donate

Contact Us

Dhiraj
dhiraj@devglan.com

© 2020 Devglan. All rights reserved.