Projects name list in java

Working With a List of Lists in Java

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

We rely on other people’s code in our own work. Every day.

It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.

Читайте также:  Center buttons in div css

Lightrun is a new kind of debugger.

It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.

Learn more in this quick, 5-minute Lightrun tutorial:

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

We’re looking for a new Java technical editor to help review new articles for the site.

1. Overview

List is a pretty commonly used data structure in Java. Sometimes, we may need a nested List structure for some requirements, such as List>.

In this tutorial, we’ll take a closer look at this “List of Lists” data structure and explore some everyday operations.

2. List Array vs. List of Lists

We can look at the “List of Lists” data structure as a two-dimensional matrix. So, if we want to group a number of List objects, we have two options:

Next, let’s have a look at when to choose which one.

Array is fast for “get” and “set” operations, which run in O(1) time. However, since the array’s length is fixed, it’s costly to resize an array for inserting or deleting elements.

On the other hand, List is more flexible on insertion and deletion operations, which run in O(1) time. Generally speaking, List is slower than Array on “get/set” operations. But some List implementations, such as ArrayList, are internally based on arrays. So, usually, the difference between the performance of Array and ArrayList on “get/set” operations is not noticeable.

Therefore, we would pick the List> data structure in most cases to gain better flexibility.

Of course, if we’re working on a performance-critical application, and we don’t change the size of the first dimension – for instance, we never add or remove inner Lists – we can consider using the List[] structure.

We’ll mainly discuss List> in this tutorial.

3. Common Operations on List of Lists

Now, let’s explore some everyday operations on List>.

For simplicity, we’ll manipulate the List> object and verify the result in unit test methods.

Further, to see the change straightforwardly, let’s also create a method to print the content of the List of Lists:

private void printListOfLists(List> listOfLists) < System.out.println("\n List of Lists "); System.out.println("-------------------------------------"); listOfLists.forEach(innerList ->< String line = String.join(", ", innerList); System.out.println(line); >); > 

Next, let’s first initialize a list of lists.

3.1. Initializing a List of Lists

We’ll import data from a CSV file into a List> object. Let’s first look at the CSV file’s content:

Linux, Microsoft Windows, Mac OS, Delete Me Kotlin, Delete Me, Java, Python Delete Me, Mercurial, Git, Subversion

Let’s say we name the file as example.csv and put it under the resources/listoflists directory.

Next, let’s create a method to read the file and store the data in a List> object:

private List> getListOfListsFromCsv() throws URISyntaxException, IOException < Listlines = Files.readAllLines(Paths.get(getClass().getResource("/listoflists/example.csv") .toURI())); List> listOfLists = new ArrayList<>(); lines.forEach(line -> < ListinnerList = new ArrayList<>(Arrays.asList(line.split(", "))); listOfLists.add(innerList); >); return listOfLists; > 

In the getListOfListsFromCsv method, we first read all lines from the CSV file into a List object. Then, we walk through the lines List and convert each line (String) into List .

Finally, we add every converted List object to listOfLists. Thus, we’ve initialized a list of lists.

Curious eyes may have detected that we wrap Arrays.asList(..) in a new ArrayList<>(). This is because the Arrays.asList method will create an immutable List. However, we’ll make some changes to the inner lists later. Therefore, we wrap it in a new ArrayList object.

If the list of lists object is created correctly, we should have three elements, which is the number of lines in the CSV file, in the outer list.

Moreover, each element is an inner list, and each of those should contain four elements. Next, let’s write a unit test method to verify this. Also, we’ll print the initialized list of lists:

List> listOfLists = getListOfListsFromCsv(); assertThat(listOfLists).hasSize(3); assertThat(listOfLists.stream() .map(List::size) .collect(Collectors.toSet())).hasSize(1) .containsExactly(4); printListOfLists(listOfLists); 

If we execute the method, the test passes and produces the output:

 List of Lists ------------------------------------- Linux, Microsoft Windows, Mac OS, Delete Me Kotlin, Delete Me, Java, Python Delete Me, Mercurial, Git, Subversion

Next, let’s so make some changes to the listOfLists object. But, first, let’s see how to apply changes to the outer list.

3.2. Applying Changes to the Outer List

If we focus on the outer list, we can ignore the inner list at first. In other words, we can look at List> as the regular List.

Thus, it’s not a challenge to change a regular List object. We can call List‘s methods, such as add and remove, to manipulate the data.

Next, let’s add a new element to the outer list:

List> listOfLists = getListOfListsFromCsv(); List newList = new ArrayList<>(Arrays.asList("Slack", "Zoom", "Microsoft Teams", "Telegram")); listOfLists.add(2, newList); assertThat(listOfLists).hasSize(4); assertThat(listOfLists.get(2)).containsExactly("Slack", "Zoom", "Microsoft Teams", "Telegram"); printListOfLists(listOfLists); 

An element of the outer list is actually a List object. As the method above shows, we create a list of popular online communication utilities. Then, we add the new list to listOfLists in the position with index=2.

Again, after the assertions, we print the content of listOfLists:

 List of Lists ------------------------------------- Linux, Microsoft Windows, Mac OS, Delete Me Kotlin, Delete Me, Java, Python Slack, Zoom, Microsoft Teams, Telegram Delete Me, Mercurial, Git, Subversion 

3.3. Applying Changes to Inner Lists

Finally, let’s see how to manipulate the inner lists.

Since listOfList is a nested List structure, we need to first navigate to the inner list object we want to change. If we know the index exactly, we can simply use the get method:

List innerList = listOfLists.get(x); // innerList.add(), remove() . 

However, if we would like to apply a change to all inner lists, we can pass through the list of lists object via a loop or the Stream API.

Next, let’s see an example that removes all “Delete Me” entries from the listOfLists object:

List> listOfLists = getListOfListsFromCsv(); listOfLists.forEach(innerList -> innerList.remove("Delete Me")); assertThat(listOfLists.stream() .map(List::size) .collect(Collectors.toSet())).hasSize(1) .containsExactly(3); printListOfLists(listOfLists); 

As we’ve seen in the method above, we iterate each inner list via listOfLists.forEach and use a lambda expression to remove “Delete Me” entries from innerList.

If we execute the test, it passes and produces the following output:

 List of Lists ------------------------------------- Linux, Microsoft Windows, Mac OS Kotlin, Java, Python Mercurial, Git, Subversion 

4. Conclusion

In this article, we’ve discussed the list of lists data structure.

Further, we’ve addressed the common operations on the list of lists through examples.

As usual, the complete code of this article can be found over on GitHub.

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

Источник

In Java How to Find List of all Class Names from inside .jar File? – Jar Class Finder Utility with Java Reflection

This tutorial will be very interesting. Last week I was working on a Java Project which requires me to have list of Classes from .jar file. With the help of JarEntry and JarInputStream utility I was able to extract all classes from inside of .jar file.

JarInputStream creates a new JarInputStream and reads the optional manifest. If a manifest is present, also attempts to verify the signatures if the JarInputStream is signed.

Below solution will also work for your if you have any of below query:

  • In Java – How to find a class somewhere inside dozens of JAR files
  • How to get names of classes inside a jar file?
  • Looking for a Java class in a set of JARs with find

how-to-find-list-of-all-class-names-from-inside-jar-file

Let’s get started. We will perform below steps.

  1. Create class CrunchifyFindClassesFromJar.java
  2. Create utility method called getCrunchifyClassNamesFromJar(String jarFileName)
  3. Iterate through nextJarEntry and if found remove .class from name add it to JSON list
  4. Print JSONObject containing above class list
package com.crunchify.tutorial; import java.io.FileInputStream; import java.util.jar.JarEntry; import java.util.jar.JarInputStream; import org.json.JSONArray; import org.json.JSONObject; /** * @author Crunchify.com * */ public class CrunchifyFindClassesFromJar < @SuppressWarnings("resource") public static JSONObject getCrunchifyClassNamesFromJar(String crunchifyJarName) < JSONArray listofClasses = new JSONArray(); JSONObject crunchifyObject = new JSONObject(); try < JarInputStream crunchifyJarFile = new JarInputStream(new FileInputStream(crunchifyJarName)); JarEntry crunchifyJar; while (true) < crunchifyJar = crunchifyJarFile.getNextJarEntry(); if (crunchifyJar == null) < break; >if ((crunchifyJar.getName().endsWith(".class"))) < String className = crunchifyJar.getName().replaceAll("/", "\\."); String myClass = className.substring(0, className.lastIndexOf('.')); listofClasses.put(myClass); >> crunchifyObject.put("Jar File Name", crunchifyJarName); crunchifyObject.put("List of Class", listofClasses); > catch (Exception e) < System.out.println("Oops.. Encounter an issue while parsing jar" + e.toString()); >return crunchifyObject; > public static void main(String[] args) < JSONObject myList = getCrunchifyClassNamesFromJar("/Users//Documents/javax.servlet.jsp.jar"); System.out.println(myList); > >
< "List of Class": [ "javax.el.ArrayELResolver", "javax.el.BeanELResolver$BeanProperties", "javax.el.BeanELResolver$BeanProperty", "javax.el.BeanELResolver", "javax.el.CompositeELResolver$CompositeIterator", "javax.el.CompositeELResolver", "javax.el.ELContext", "javax.el.ELContextEvent", . . so many more. ], "Jar File Name": "/Users//Documents/javax.servlet.jsp.jar" >

Bonus point:

Check out how to find if particular jar contains specific method.

try < ClasscrunchifyClass = Class.forName(className); Method main = crunchifyClass.getDeclaredMethod("yourMethodName"); main.invoke(null); > catch (Exception e)

Hope this mini utility will help if you want to quickly achieve the same goal. Feel free to use it in your production project.

If you liked this article, then please share it on social media. Have a question or suggestion? Please leave a comment to start the discussion. 👋

Suggested Articles.

Источник

Оцените статью